2017-12-03 2 views
0

사용자가 여러 연락처를 선택하여 목록에 추가 할 수있는 연락처 목록을 만들려고합니다. 지금 연락처를 클릭하면 선택한 연락처의 수, 사용자 ID 및 표시되는 목록이 표시됩니다. 사용자의 위치는입니다. 무엇 내가이하고 싶은 선택한 사용자의 ID를 표시하는 대신 위치의, 내 목록에 추가 입니다. 나는 this 자습서로 갈 것이다.Android에서 위치 대신 선택된 사용자 ID를 추가하십시오.

내 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; 
} 

답변

0

나는 해결책을 발견했다. 이것은 내가 코드에 추가해야 할 필요가있는 것이다.

어댑터 :

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); 
    } 
} 

나는이처럼 내 활동에 전화 :

ArrayList<String> selectedIds = adapter.selectedIds; 
관련 문제