将选定的用户 ID 添加到列表而不是 Android 中的位置
Add Selected Users Ids to List Instead of Position in Android
我正在尝试创建一个联系人列表,用户可以在其中 select 多个联系人并将他们添加到列表中。现在,当我点击一个联系人时,我会得到 selected 联系人的数量、用户 ID 和一个显示 selected 用户位置的列表。 我想要做的是显示selected用户的id并将其添加到我的列表,而不是位置。我正在学习 this 教程。
我的 ChooseContactsAdapter:
@Override
public void onBindViewHolder(final ChooseContactsAdapter.ChooseContactsViewHolder holder, final int position) {
final Contact contact = contactList.get(position);
holder.userName.setText(contact.getUserName());
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.withBorder(0)
.toUpperCase()
.endConfig()
.round();
ColorGenerator generator = ColorGenerator.MATERIAL;
// generate color based on a key (same key returns the same color), useful for list/grid views
int color = generator.getColor(contact.getUserId());
textDrawable = builder.build(contactList.get(position).getUserName().substring(0, 1), color);
holder.thumbNail.setImageDrawable(textDrawable);
// THIS IS WHERE I GET MY USER ID FROM THE HOLDER
holder.contactId = contact.getUserId();
// display profile image
applyProfilePicture(holder, contact);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// toggle selection
toggleSelection(position);
// Change background color of the selected items in list view
holder.itemView.setBackgroundColor(selectedItems.get(position) ? context.getResources().getColor(R.color.ppdColorOrangeSelection) : Color.TRANSPARENT);
// check if item still exists
if (position != RecyclerView.NO_POSITION) {
Contact contact = contactList.get(position);
// THIS TOAST IS WHERE I DISPLAY MY STUFF ON CLICK
Toast.makeText(v.getContext(), "You clicked " + holder.contactId + ", number of selected contacts is " + getSelectedItemCount() + ", contacts id's are " + getSelectedItems(), Toast.LENGTH_SHORT).show();
// applyIconAnimation(holder, position);
}
// change the row state to activated
//holder.itemView.setActivated(selectedItems.get(position, false));
// handle icon animation
applyIconAnimation(holder, position);
// apply click events
//applyClickEvents(holder, position);
// number of selected contacts
getSelectedItemCount();
// selected contacts
getSelectedItems();
// reset animation index
resetAnimationIndex();
// clear selections
//clearSelections();
// number of selected contacts
getSelectedItemCount();
}
});
}
private List<Integer> getSelectedItems() {
List<Integer> items =
new ArrayList<>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
items.add(selectedItems.keyAt(i));
}
return items;
}
// adding user ids to list
private List<Integer> getSelectedUserIds() {
List<Integer> items =
new ArrayList<>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
items.add(selectedItems.keyAt(i));
}
return items;
}
我找到了解决办法。这是我需要添加到我的代码中的内容。
适配器:
if (position != RecyclerView.NO_POSITION) {
Contact contact = contactList.get(position);
//Toast.makeText(v.getContext(), "You clicked " + holder.contactId + ", number of selected contacts is " + getSelectedItemCount() + ", contacts id's are " + getSelectedUserIds(holder.contactId = contact.getUserId(), position), Toast.LENGTH_SHORT).show();
applyIconAnimation(holder, position);
String userId = contact.getUserId();
// Adding/removing user ids to list
if (!selectedIds.contains(userId)) {
selectedIds.add(userId);
} else {
selectedIds.remove(userId);
}
}
我在 Activity 中这样称呼它:
ArrayList<String> selectedIds = adapter.selectedIds;
我正在尝试创建一个联系人列表,用户可以在其中 select 多个联系人并将他们添加到列表中。现在,当我点击一个联系人时,我会得到 selected 联系人的数量、用户 ID 和一个显示 selected 用户位置的列表。 我想要做的是显示selected用户的id并将其添加到我的列表,而不是位置。我正在学习 this 教程。
我的 ChooseContactsAdapter:
@Override
public void onBindViewHolder(final ChooseContactsAdapter.ChooseContactsViewHolder holder, final int position) {
final Contact contact = contactList.get(position);
holder.userName.setText(contact.getUserName());
TextDrawable.IBuilder builder = TextDrawable.builder()
.beginConfig()
.withBorder(0)
.toUpperCase()
.endConfig()
.round();
ColorGenerator generator = ColorGenerator.MATERIAL;
// generate color based on a key (same key returns the same color), useful for list/grid views
int color = generator.getColor(contact.getUserId());
textDrawable = builder.build(contactList.get(position).getUserName().substring(0, 1), color);
holder.thumbNail.setImageDrawable(textDrawable);
// THIS IS WHERE I GET MY USER ID FROM THE HOLDER
holder.contactId = contact.getUserId();
// display profile image
applyProfilePicture(holder, contact);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// toggle selection
toggleSelection(position);
// Change background color of the selected items in list view
holder.itemView.setBackgroundColor(selectedItems.get(position) ? context.getResources().getColor(R.color.ppdColorOrangeSelection) : Color.TRANSPARENT);
// check if item still exists
if (position != RecyclerView.NO_POSITION) {
Contact contact = contactList.get(position);
// THIS TOAST IS WHERE I DISPLAY MY STUFF ON CLICK
Toast.makeText(v.getContext(), "You clicked " + holder.contactId + ", number of selected contacts is " + getSelectedItemCount() + ", contacts id's are " + getSelectedItems(), Toast.LENGTH_SHORT).show();
// applyIconAnimation(holder, position);
}
// change the row state to activated
//holder.itemView.setActivated(selectedItems.get(position, false));
// handle icon animation
applyIconAnimation(holder, position);
// apply click events
//applyClickEvents(holder, position);
// number of selected contacts
getSelectedItemCount();
// selected contacts
getSelectedItems();
// reset animation index
resetAnimationIndex();
// clear selections
//clearSelections();
// number of selected contacts
getSelectedItemCount();
}
});
}
private List<Integer> getSelectedItems() {
List<Integer> items =
new ArrayList<>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
items.add(selectedItems.keyAt(i));
}
return items;
}
// adding user ids to list
private List<Integer> getSelectedUserIds() {
List<Integer> items =
new ArrayList<>(selectedItems.size());
for (int i = 0; i < selectedItems.size(); i++) {
items.add(selectedItems.keyAt(i));
}
return items;
}
我找到了解决办法。这是我需要添加到我的代码中的内容。
适配器:
if (position != RecyclerView.NO_POSITION) {
Contact contact = contactList.get(position);
//Toast.makeText(v.getContext(), "You clicked " + holder.contactId + ", number of selected contacts is " + getSelectedItemCount() + ", contacts id's are " + getSelectedUserIds(holder.contactId = contact.getUserId(), position), Toast.LENGTH_SHORT).show();
applyIconAnimation(holder, position);
String userId = contact.getUserId();
// Adding/removing user ids to list
if (!selectedIds.contains(userId)) {
selectedIds.add(userId);
} else {
selectedIds.remove(userId);
}
}
我在 Activity 中这样称呼它:
ArrayList<String> selectedIds = adapter.selectedIds;