英文:
Why is my RecyclerView showing Incorrect order of CardView items?
问题
我有一个包含食物(类)对象的CardViews的RecyclerView。一个奇怪的情况是,当我使用一个方法将新的食物对象添加到我正在使用的ArrayList中,并通知适配器已添加一个值时,Card就会被添加到屏幕上,但它具有不同的一组值。
我使用2个占位符食物对象在默认情况下显示在屏幕上,按下“添加”按钮应该添加一个新的对象,名称为“Name”,但实际上我得到的是另一个第一个占位符的副本。
当屏幕上添加了太多的卡片时,上下滑动会改变卡片的顺序,突然之间,新项目(带有正确数据)会出现!
出了什么问题?
顺便说一下,应用程序没有崩溃。
添加新的食物对象到ArrayList 'foods' 的函数:
public void addFood(){
foods.add(new Food("Name", 0, 3, 2)); // 一个构造函数
foodAdapter.notifyItemInserted(0);
}
英文:
I have a RecyclerView of CardViews of Food (class) objects. A weird thing that is happening is that when I'm using a method to add a new Food object to the ArrayList that I'm using, and notifying the adapter that a value has been added, the Card gets added on screen, but it has a different set of values.
I'm using 2 placeholder Food objects to show on screen at default, and pressing the Add button is supposed to add a new object with name "Name", but instead I get another copy of the first placeholder.
When too many Cards are added to screen, swiping up and down changes the order of the cards, and suddenly, the new items (with the correct data) appear!
What's wrong?
The app doesn't crash btw.
Function to add new Food object to ArrayList called 'foods':
public void addFood(){
foods.add(new Food("Name",0,3,2)); //a constructor
foodAdapter.notifyItemInserted(0);
}
答案1
得分: 1
Apparently, the issue was that calling .add(Object) on the ArrayList item 'foods' actually appended the new Object to the end of the list, meanwhile, Adapter.notifyItemInserted(0) made the Recycler view think that Position 0 got the new Card, whereas it was actually Position last.
This made the wrong item show up at the RecyclerView.
So, as obvious, the fix is to use the ArrayList.add(position,object) constructor instead of .add(object), where position shows where the new element was inserted.
So, the correct code is:
public void addFood(){
foods.add(0, new Food("Name", 0, 3, 2)); //a constructor
foodAdapter.notifyItemInserted(0);
}
If you want the new object to be inserted at a different position, simply pass that position in place of the 0 above.
英文:
Apparently, the issue was that calling .add(Object) on the ArrayList item 'foods' actually appended the new Object to the end of the list, meanwhile, Adapter.notifyItemInserted(0) made the Recycler view think that Position 0 got the new Card, whereas it was actually Position last.
This made the wrong item show up at the RecyclerView.
So, as obvious, the fix is to use the ArrayList.add(position,object) constructor instead of .add(object), where position shows where the new element was inserted.
So, the correct code is:
public void addFood(){
foods.add(0, new Food("Name", 0, 3, 2)); //a constructor
foodAdapter.notifyItemInserted(0);
}
If you want the new object to be inserted at a different position, simply pass that position in place of the 0 above.
答案2
得分: 0
你只需要将foodAdapter.notifyItemInserted(0)
修改为foodAdapter.notifyDataSetChanged()
或者foodAdapter.notifyItemInserted(foods.size() - 1)
。
英文:
you just need to change foodAdapter.notifyItemInserted(0)
into foodAdapter.notifyDataSetChanged()
or foodAdapter.notifyItemInserted(foods.size() - 1)
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论