0

I'm trying to make a listview in Android the way you can see your chats in whatsapp. I have a listview in my Fragment and I get data from Firebase to put in the list. The first 10 items are perfectly fine but after those 10, the getview() function is not called anymore. I get buttons who have wrong text (name from other group) but if I click on them, a toast shows up with the correct name in it.

Code snippets:
Fragment:

rowItems = new ArrayList<RowItem>();

final CustomAdapter adapter = new CustomAdapter(getContext(), rowItems);
mylistview = (ListView) rootView.findViewById(R.id.LVGroups);
mylistview.setAdapter(adapter);
mylistview.setOnItemClickListener(this);  
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String name = dataSnapshot.child("name").getValue(String.class);
                Log.d("TAG", "name: " + name);
                //Create new item in the rowItems arraylist to add to the listview
                RowItem item = new RowItem(name, R.mipmap.ic_challenger_black, "placeholder");
                rowItems.add(item);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };  

RowItem:

public class RowItem {

    private String member_name;
    private int profile_pic_id;
    private String status;

    public RowItem(String member_name, int profile_pic_id, String status) {
        this.member_name = member_name;
        Log.d("TAG", "member_name: " + this.member_name);
        this.profile_pic_id = profile_pic_id;
        this.status = status;
    }

    public String getMember_name() {
        return this.member_name;
    }

    public void setMember_name(String member_name) {
        this.member_name = member_name;
    }

    public int getProfile_pic_id() {
        return profile_pic_id;
    }

    public void setProfile_pic_id(int profile_pic_id) {
        this.profile_pic_id = profile_pic_id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
} 

CustomAdapter:

public class CustomAdapter extends BaseAdapter {
    Context context;
    List<RowItem> rowItems;

    CustomAdapter(Context context, List<RowItem> rowItems) {
        this.context = context;
        this.rowItems = rowItems;
    }

    @Override
    public int getCount() {
        return rowItems.size();
    }

    @Override
    public Object getItem(int position) {
        return rowItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return rowItems.indexOf(getItem(position));
    }

    /* private view holder class */
    private class ViewHolder {
        ImageView profile_pic;
        TextView member_name;
        TextView status;
    }

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

        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.list_item_groups, null);
            holder = new ViewHolder();

            holder.member_name = (TextView) convertView
                    .findViewById(R.id.name);
            holder.profile_pic = (ImageView) convertView
                    .findViewById(R.id.pic);
            holder.status = (TextView) convertView.findViewById(R.id.sta);

            RowItem row_pos = rowItems.get(position);

            holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
            holder.member_name.setText(row_pos.getMember_name());
            Log.d("TAG", "holder.member_name: " + row_pos.getMember_name() + " " + position);
            holder.status.setText(row_pos.getStatus());

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }
}

The print in the customadapter prints until the 10th element (index 9). For the last 2, it's not called.

DeGoosseZ
  • 628
  • 5
  • 20

1 Answers1

2

You need to set textView text and imageView source out of the if else statement. You are setting them only in the initialization of views. They don't change after scrolling because convertView isn't null after scroll. Your adapter will use past convertViews and past values. You only need the set contents of views.

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

    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);


    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.list_item_groups, null);
        holder = new ViewHolder();

        holder.member_name = (TextView) convertView
                .findViewById(R.id.name);
        holder.profile_pic = (ImageView) convertView
                .findViewById(R.id.pic);
        holder.status = (TextView) convertView.findViewById(R.id.sta);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

RowItem row_pos = rowItems.get(position);

        holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
        holder.member_name.setText(row_pos.getMember_name());
        Log.d("TAG", "holder.member_name: " + row_pos.getMember_name() + " " + position);
        holder.status.setText(row_pos.getStatus());

    return convertView;
}

Edit: One more suggestion. Define this variable in your class:

LayoutInflater mInflater;

Call this in the constructor of the your custom adapter:

mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

and delete initializing of the inflater in getView method. ListView is so much important class. Don't tire your getView method because this is called every list item is on the screen.

Look link below. This is how ListView recycle works:

How ListView's recycling mechanism works

Efe AYDIN
  • 193
  • 12