2017-12-03 8 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; 
関連する問題