在 Getter/Setter(多维)Arraylist 中使用 IndexOf
Use IndexOf within Getter/Setter (mulitdimensional) Arraylist
框架:
通过 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;
public class 牌组 {
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;
}
}
更多信息基于 Post 个问题:
为了让您了解我想做什么,我将 post 使用 mData 和 mDecklist 的 Recyclerviews 的屏幕截图:
让我用颜色来解释一下:
- orange Recyclerview 使用 mData 来扩充 Recyclerview。基于 orange Recyclerview 中带圆圈的 green 按钮,我们将 mData 的值赋给一个名为 mDecklist 的新 Arraylist。
- mDecklist 用于绿色 Recyclerview 以创建新的值,因此我们可以将项目从第一个 Recyclerview(橙色)添加到第二个 Recyclerview(绿色)
- 但它变得更复杂了!当我们在 Yellow 中使用 Spinners 时,Orange Recyclerview 从数据库中获取一个全新的 mData 文件. 这意味着位置(基本上)被覆盖。
- 因此,get(position) 是不够的!#
- 每个 mDecklist 都会使用与 mData 中的卡片相同的名称(例如 CardName 是 Bloodseeker,它也将在 mDecklist 中使用)。所以我希望可以通过使用名称作为搜索参考来找到对象在mDecklist中的位置。
非常欢迎任何帮助! :)
重写Declist
class中的equals()
和hashCode()
方法(androidstudio可以自动生成)。然后 mDecklist.indexOf(mData.get(position))
将 return 正确索引 当且仅当 mData.get(position)
returns a 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;
}
}
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");
}
}
总之非常感谢您的建议和阅读!
框架:
通过 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;
public class 牌组 {
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;
}
}
更多信息基于 Post 个问题:
为了让您了解我想做什么,我将 post 使用 mData 和 mDecklist 的 Recyclerviews 的屏幕截图:
让我用颜色来解释一下:
- orange Recyclerview 使用 mData 来扩充 Recyclerview。基于 orange Recyclerview 中带圆圈的 green 按钮,我们将 mData 的值赋给一个名为 mDecklist 的新 Arraylist。
- mDecklist 用于绿色 Recyclerview 以创建新的值,因此我们可以将项目从第一个 Recyclerview(橙色)添加到第二个 Recyclerview(绿色)
- 但它变得更复杂了!当我们在 Yellow 中使用 Spinners 时,Orange Recyclerview 从数据库中获取一个全新的 mData 文件. 这意味着位置(基本上)被覆盖。
- 因此,get(position) 是不够的!#
- 每个 mDecklist 都会使用与 mData 中的卡片相同的名称(例如 CardName 是 Bloodseeker,它也将在 mDecklist 中使用)。所以我希望可以通过使用名称作为搜索参考来找到对象在mDecklist中的位置。
非常欢迎任何帮助! :)
重写Declist
class中的equals()
和hashCode()
方法(androidstudio可以自动生成)。然后 mDecklist.indexOf(mData.get(position))
将 return 正确索引 当且仅当 mData.get(position)
returns a 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;
}
}
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");
}
}
总之非常感谢您的建议和阅读!