英文:
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<Member[]> membersAdded = new ArrayList<>();
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("Test", "same "+(membersAdded.size()-1));
}
if (membersAdded.size() > 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 -> {
Member x = memberList.get(position);
x.setPositionInBc(counter);
counter++;
bcAddMembersFragment.AddMember(x);
Toast.makeText(bcAddMembersFragment.getContext(), "Member Added",
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 -> {
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();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论