ListView项在BaseAdapter中重复

时间:2015-06-19 03:12:02

标签: android listview baseadapter

我正在提取数据并将其加载到我的列表视图的BaseAdapter中。目前,列表视图中的项目与添加的最后一项相同,因此列表如下所示:

Item 3
Item 3
Item 3

而不是:

Item 1
Item 2
Item 3

我已检查过数据来源,每次从源加载的数据都是唯一的。我认为我的BaseAdapter有些不对劲,但我不确定它是什么。这是我的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;
}

我将从我的BaseAdapter开始,如果需要可以显示更多代码。感谢所有帮助!

2 个答案:

答案 0 :(得分:0)

您错过了返回position

将您的getItemId修改为

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

答案 1 :(得分:0)

您需要更改两件事:

<强> 1。您返回错误的商品位置

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

应该是

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

<强> 2。您声明了一个LayoutInflater并在构造函数中创建新实例。但是在getView()中,您创建了另一个LayoutInflater。请查看下面的更新代码

@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;
}