在Getter / Setter(多维)Arraylist中使用IndexOf

时间:2018-11-12 16:48:03

标签: android android-studio object arraylist indexof

框架:

通过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的屏幕截图: RecyclerViews

让我用颜色来解释:

  • 橙色 Recyclerview使用mData扩展Recyclerview。基于橙色 Recyclerview中带圆圈的绿色按钮,我们将mData的值添加到名为mDecklist的新Arraylist中。
  • mDecklist而不是在绿色Recyclerview中用于创建新值,因此我们可以将第一个Recyclerview(橙色)中的项目添加到第二个Recyclerview(绿色)中
  • 但是它变得更加复杂!当我们在 Yellow 中使用Spinners时, Orange Recyclerview是从数据库中获取全新的mData 文件。 这意味着职位(基本上)被覆盖。
  • 因此,get(position)不够!
  • 每个mDecklist都将使用与mData中的卡片相同的名称(例如CardName为Bloodseeker,也将在mDecklist中使用)。因此,我希望可以通过使用名称作为搜索参考来在mDecklist中找到对象的位置。

非常感谢您的帮助! :)

2 个答案:

答案 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");
                    }
                }

无论如何,非常感谢您的建议和阅读!

相关问题