Android列表<>保存与先前对象相等的新对象

huangapple go评论91阅读模式
英文:

Android List<> Save new objects which are equal to previous object

问题

所以我尝试将自定义对象(成员)添加到

final List<Member[]> membersAdded = new ArrayList<>();

通过调用这个方法

public void AddMember(Member member) {
    member.setAmountInShare(bcAmount);
    Member[] x = new Member[]{member};
    membersAdded.add(x);
    membersAddedCounter++;
    addAdapter.notifyDataSetChanged();

    if(membersAdded.get(0)[0] == membersAdded.get(membersAdded.size()-1)[0]){
        Log.d("Test", "same "+(membersAdded.size()-1));
    }

    if (membersAdded.size() > 0) hintTextView.setVisibility(View.GONE);
    else hintTextView.setVisibility(View.VISIBLE);
}

该列表保存了一个对象(成员)的数组,所以我可以在需要时向数组中添加更多的对象。
假设我有5个对象,

Member member1 = new Member();
Member member2 = new Member();
Member member3 = new Member();
Member member4 = new Member();
Member member5 = new Member();

当我调用

AddMember(member1)

日志返回 "same",因为它们是相同的。但是当我执行以下操作时

AddMember(member1)
AddMember(member2)
AddMember(member3)
AddMember(member4)
AddMember(member5)
AddMember(member1)

日志返回
在第一次运行时
"same"
在最后一次运行时
"same"

这里有什么问题?

英文:

So I'm trying to add Custom Objects(Members) to

final List&lt;Member[]&gt; membersAdded = new ArrayList&lt;&gt;();

By calling this method

 public void AddMember(Member member) {
    member.setAmountInShare(bcAmount);
    Member[] x = new Member[]{member};
    membersAdded.add(x);
    membersAddedCounter++;
    addAdapter.notifyDataSetChanged();

    if(membersAdded.get(0)[0] == membersAdded.get(membersAdded.size()-1)[0]){
        Log.d(&quot;Test&quot;, &quot;same &quot;+(membersAdded.size()-1));
    }

    if (membersAdded.size() &gt; 0) hintTextView.setVisibility(View.GONE);
    else hintTextView.setVisibility(View.VISIBLE);
}

The list hold an Array of the object(Member) so I can add more objects in the array when I need to.
So Let's I have 5 objects,

Member member1 = new Member();
Member member2 = new Member();
Member member3 = new Member();
Member member4 = new Member();
Member member5 = new Member();

when I add call

AddMember(member1)

the log returns same because they're but when I do this

AddMember(member1)
AddMember(member2)
AddMember(member3)
AddMember(member4)
AddMember(member5)
AddMember(member1)

The log returns
on first run
same
and on the last run
same

What's wrong here?

答案1

得分: 0

我是通过从另一个列表中选择行项目来添加行,就像这样:

holder.cardView.setOnClickListener(v -> {
    Member x = memberList.get(position);
    x.setPositionInBc(counter);
    counter++;
    bcAddMembersFragment.AddMember(x);
    Toast.makeText(bcAddMembersFragment.getContext(), "Member Added", 
    Toast.LENGTH_SHORT).show();
});

由于某种原因,(Member x)正在从第二个列表中获取对象的引用,所以我为Member对象创建了一个新的构造函数,问题就这样解决了:

holder.cardView.setOnClickListener(v -> {
    Member x = memberList.get(position);
    Member tempMember = new Member(x.getId(), x.getName(), x.getContactNumber(), 
        x.getStatus().toString());
    tempMember.setPositionInBc(counter);
    counter++;
    bcAddMembersFragment.AddMember(tempMember);
    Toast.makeText(bcAddMembersFragment.getContext(), "Member Added", 
        Toast.LENGTH_SHORT).show();
});
英文:

I was adding rows by selecting row items from another list Like this,

    holder.cardView.setOnClickListener(v -&gt; {
        Member x = memberList.get(position);
        x.setPositionInBc(counter);
        counter++;
        bcAddMembersFragment.AddMember(x);
        Toast.makeText(bcAddMembersFragment.getContext(), &quot;Member Added&quot;, 
        Toast.LENGTH_SHORT).show();
    });

For some reason (Member x) is taking a reference of the object from the 2nd list so I made a new constructor of the Member object and vollah.

What I did to solve the problem is

    holder.cardView.setOnClickListener(v -&gt; {
        Member x = memberList.get(position);
        Member tempMember = new Member(x.getId(), x.getName(), x.getContactNumber(), 
            x.getStatus().toString());
        tempMember.setPositionInBc(counter);
        counter++;
        bcAddMembersFragment.AddMember(tempMember);
        Toast.makeText(bcAddMembersFragment.getContext(), &quot;Member Added&quot;, 
            Toast.LENGTH_SHORT).show();
    });

huangapple
  • 本文由 发表于 2020年10月1日 21:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64155964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定