views:

33

answers:

1

I have created a listActivity with my own ListAdapter. The problem is that the list is viewed in order once launched. But When I scroll down, or go back from another activity, the listView is completely out of order.

I thought the problem was in ArrayList but no, the list is sorted and i'm sure of it because when I loop over all elements in the ArrayList they are printed in the log the same way I inserted them.

I pasted the adapter code below in case anyone would like to check it.

package com.anubis.mail;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class EmailAdapter extends BaseAdapter {

    private ArrayList<EmailModel> elements;
    private Context c;

    public EmailAdapter(Context c, ArrayList<EmailModel> Emails) {

        this.elements = Emails;
        this.c = c;
    }
    public int getCount() {
        return elements.size();
    }

    public Object getItem(int position) {
        return elements.get(position);
    }

    public long getItemId(int id) {
        return id;
    }

    public void Remove(int id) {
        notifyDataSetChanged();
    }

    public void Add(EmailModel email) {

        this.elements.add(email);

        for (EmailModel e : elements){

            Log.v("EmailAdapter", e.getSubject());  
        }

        notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LinearLayout rowLayout;
        EmailModel email = elements.get(position);

        if (convertView == null) {

            rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false); 

            TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
            subject_textview.setText(email.getSubject());

            String body_hint = " - " + email.getBodyHint();

            TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
            bodyhint_textview.setText(body_hint);


            String sender_name = get_sender_name(email.getSender());

            TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
            sender_name_textview.setText(sender_name);

            TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview); 
            date_time_textview.setText(email.getTime());

        } else {
            rowLayout = (LinearLayout) convertView;
        }
        return rowLayout;
    }

    private String get_sender_name(String from) {

        String[] sender = from.split("<");
        String sender_name;

        try {
            sender_name = sender[0];
        } catch (Exception e) {
            sender_name = sender[1];
        }
        return sender_name;
    }
}
+1  A: 

You need to move code after the IF

if (convertView == null) {
        rowLayout = (LinearLayout) LayoutInflater.from(c).inflate (R.layout.inbox_item, parent, false); 
} else {
        rowLayout = (LinearLayout) convertView;
}
TextView subject_textview = (TextView)rowLayout.findViewById(R.id.subject_textview);
subject_textview.setText(email.getSubject());

String body_hint = " - " + email.getBodyHint();

TextView bodyhint_textview = (TextView)rowLayout.findViewById(R.id.body_hint_textview);
bodyhint_textview.setText(body_hint);


String sender_name = get_sender_name(email.getSender());

TextView sender_name_textview = (TextView)rowLayout.findViewById(R.id.sender_textview);
sender_name_textview.setText(sender_name);

TextView date_time_textview = (TextView)rowLayout.findViewById(R.id.date_time_textview); 
date_time_textview.setText(email.getTime());
return rowLayout;
Pentium10
Thanks a lot man, your a life saver.
Seif Sallam