[英]ListView items repeat in 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開始,如果需要,可以顯示更多代碼。 感謝所有幫助!
您錯過了返回position
將您的getItemId
為
@Override
public long getItemId(int position) {
return position;
}
您需要更改兩件事:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.