ListView items repeat in BaseAdapter

790 views Asked by At

I'm pulling data and loading it into a BaseAdapter for my List View. Currently, the items in the listview are the same as the last item added, so the list looks like this:

Item 3
Item 3
Item 3

Instead of:

Item 1
Item 2
Item 3

I've checked over the data sources, and the data that's loaded from the source is unique each time. I think there's somehting wrong with my BaseAdapter, but I'm not sure what it is. Here's my BaseAdapter:

BaseAdapter.java

ArrayList<ThreadListData> myList = new ArrayList<>();
LayoutInflater inflater;
Context context;

public ThreadBaseAdapter(Context context, ArrayList<ThreadListData> myList) {
    this.context = context;
    inflater = LayoutInflater.from(this.context);
    this.myList = myList;
}

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

@Override
public ThreadListData getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

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

    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(this.context);
        convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);

    }

    ThreadListData listData = getItem(position);

    TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
    TextView username = (TextView)convertView.findViewById(R.id.username_newthread);

    name.setText(listData.getName());
    username.setText(listData.getName());

    ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);

    BitmapTask task = new BitmapTask(listData.getImage());

    task.execute(profile);

    return convertView;
}

I'll start with my BaseAdapter, and can show more code if requested. All Help Is Appreciated!

2

There are 2 answers

0
N J On

You missed to return the position

Modifiy your getItemId as

@Override
public long getItemId(int position) {
    return position;
}
0
Phuc Tran On

There are 2 things you need to change:

1. You return wrong item position

@Override
public long getItemId(int position) {
    return 0;
}

It should be

@Override
public long getItemId(int position) {
    return position;
}

2. Your declared a LayoutInflater and create new instance in constructor. But in the getView(), you create another LayoutInflater. Look at updated code below

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

    if (convertView == null) {
        // LayoutInflater inflater = LayoutInflater.from(this.context);
           inflater = LayoutInflater.from(this.context);
        convertView = inflater.inflate(R.layout.listitem_newthread, parent, false);

    }

    ThreadListData listData = getItem(position);

    TextView name = (TextView)convertView.findViewById(R.id.name_newthread);
    TextView username = (TextView)convertView.findViewById(R.id.username_newthread);

    name.setText(listData.getName());
    username.setText(listData.getName());

    ParseImageView profile = (ParseImageView)convertView.findViewById(R.id.profilepicture);

    BitmapTask task = new BitmapTask(listData.getImage());

    task.execute(profile);

    return convertView;
}