框架:
通过Recyclerview上的onClicklistener,我将项目添加到Arraylist。归根结底,我使用以下三行来实现这一点:
int typeImage = mContext.getResources().getIdentifier("introcreeps", "drawable", mContext.getPackageName());
mDecklist.add(new Decklist(mData.get(position).getCardImage(),typeImage, "22", mData.get(position).getCardName(), "x1");
mRViewAdapterList.setCards(mDecklist);
问题:
使用另一个OnclickListener,我要编辑mDecklist
的元素。
我认为
positionToEdit = mDecklist.indexOf(mData.get(position).getCardName());
mDecklist.set(positionToEdit, new Decklist(mData.get(position).getCardImage(),typeImage, "22", mData.get(position).getCardName(), "x2" ));
我知道positionToEdit = mDecklist.indexOf(mData.get(position).getCardName());
显然是错误的。我必须通过哪种对象才能找到正确的位置?
哪些还行不通:
CardNames在我的Arraylist中将是不同的,因此我想使用它来查找位置。我还尝试使用整个ArraylistObject的东西,但是对我来说不起作用,例如:
positionToEdit = mDecklist.indexOf(Decklist(mData.get(position).getCardImage(),typeImage, "22", mData.get(position).getCardName(), "x1"));
其他信息:
以下是我的Arraylist(Decklist)的代码: 包com.example.chris.projectartifact.b_deckbuilderTap;
公开课摘要列表{
private int CardImage;
private int TypeImage;
private String Cost;
private String Name;
private String Number;
public Decklist(int cardImage, int typeImage, String cost, String name, String number) {
/* Why is there no this. ? */
CardImage = cardImage;
TypeImage = typeImage;
Cost = cost;
Name = name;
Number = number;
}
public Decklist(String cost, String name, String number) {
Cost = cost;
Name = name;
Number = number;
}
public int getCardImage() {
return CardImage;
}
public void setCardImage(int cardImage) {
CardImage = cardImage;
}
public int getTypeImage() {
return TypeImage;
}
public void setTypeImage(int typeImage) {
TypeImage = typeImage;
}
public String getCost() {
return Cost;
}
public void setCost(String cost) {
Cost = cost;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getNumber() {
return Number;
}
public void setNumber(String number) {
Number = number;
}
}
基于发帖问题的更多信息: 为了让您感觉自己想要做什么,我将发布使用mData和mDecklist的Recyclerviews的屏幕截图:
让我用颜色来解释:
非常感谢您的帮助! :)
答案 0 :(得分:0)
覆盖equals()
类中的hashCode()
和Declist
方法(android studio可以自动生成)。然后,mDecklist.indexOf(mData.get(position))
将返回正确的索引 ,并且仅当 时mData.get(position)
返回包含在Decklist
中的mDecklist
对象>
class Decklist {
private int CardImage;
private int TypeImage;
private String Cost;
private String Name;
private String Number;
public Decklist(int cardImage, int typeImage, String cost, String name, String number) {
/* Why is there no this. ? */
CardImage = cardImage;
TypeImage = typeImage;
Cost = cost;
Name = name;
Number = number;
}
public Decklist(String cost, String name, String number) {
Cost = cost;
Name = name;
Number = number;
}
public int getCardImage() {
return CardImage;
}
public void setCardImage(int cardImage) {
CardImage = cardImage;
}
public int getTypeImage() {
return TypeImage;
}
public void setTypeImage(int typeImage) {
TypeImage = typeImage;
}
public String getCost() {
return Cost;
}
public void setCost(String cost) {
Cost = cost;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getNumber() {
return Number;
}
public void setNumber(String number) {
Number = number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + CardImage;
result = prime * result + ((Cost == null) ? 0 : Cost.hashCode());
result = prime * result + ((Name == null) ? 0 : Name.hashCode());
result = prime * result + ((Number == null) ? 0 : Number.hashCode());
result = prime * result + TypeImage;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Decklist other = (Decklist) obj;
if (CardImage != other.CardImage)
return false;
if (Cost == null) {
if (other.Cost != null)
return false;
} else if (!Cost.equals(other.Cost))
return false;
if (Name == null) {
if (other.Name != null)
return false;
} else if (!Name.equals(other.Name))
return false;
if (Number == null) {
if (other.Number != null)
return false;
} else if (!Number.equals(other.Number))
return false;
if (TypeImage != other.TypeImage)
return false;
return true;
}
}
答案 1 :(得分:0)
Umer的建议自mData.get(position)
(第一个数组列表)以来不起作用,如上所述,它永远不会成为mDecklist
的对象。但是我能够找到解决我问题的另一种方法:
我正在使用mDecklist的Getter方法来解决我的问题。基本上,这全部归结为几行。它们不漂亮,但是可以解决我的问题:
for (int i = 0; i < mDecklist.size(); i++) {
if (mData.get(position).getCardName().equals(mDecklist.get(i).getName())){
Log.e("Print Out:", mDecklist.get(i).getName());
Log.e("Print Out:", "That worked");
mDecklist.get(i).setName("we can e.g. change the name");
}
}
无论如何,非常感谢您的建议和阅读!