Getting id of a selected recyclerView item android

Sam Atkinson :

I have made a note app and everything works fine. Just in RecyclerView List when I choose an item or items and in menu I click delete button I the note is not deleted. Although delete method works in another place. Here's the method of delete button in menu :

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    if (item.getItemId() == R.id.delete_selected) {
        for (NotePad notePad : notes) {
            if (selectedIds.contains(notePad.getId())) {
                database = new Database(this);
                database.deleteNote(notePad.getId());
            }
        }
        adapter.notifyDataSetChanged();
        return true;
    }
    return false;
} 

And method of delete note in my database class:

   void deleteNote(long id) {
    SQLiteDatabase database = this.getWritableDatabase();
    database.delete(DATABASE_TABLE, ID + "=?", new String[] {String.valueOf(id)});
    database.close();
}

I don't know how to relate id of selected note to my method cause deletNote method receives long value but my selectedId is List. Thanks in advance.

UPDATED

I realized my code works find but it doesn't refresh the list and also goes back from ActionMode to Toolbar. I tried adapter.notifyDataSetChanged();

OhhhThatVarun :

Use this adapter. I have used interface.

public class NotePadAdapter extends RecyclerView.Adapter < NotePadAdapter.ViewHolder > {

    private Context context;
    private LayoutInflater inflater;
    private List < NotePad > notes;
    private List < Long > selectedIds = new ArrayList < > ();
    private NoteClickListener listener;

    NotePadAdapter(Context context, NoteClickListener listener, List < NotePad > notes) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.notes = notes;
        this.listener = listener;
    }

    public interface NoteClickListener {
        public void onNoteClick(NotePad notePad);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.notes_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String title = notes.get(position).getTitle();
        String date = notes.get(position).getDate();
        String time = notes.get(position).getTime();

        holder.setOnClickListener() {
            listener.onNoteClick(notes.get(position))
        }

        holder.noteTitle.setText(title);
        holder.noteTitle.setText(title);
        holder.noteDate.setText(date);
        holder.noteTime.setText(time);

        long id = notes.get(position).getId();
        if (selectedIds.contains(id)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.rooView.setForeground(new ColorDrawable(ContextCompat.getColor(context, R.color.colorControlActivated)));
            }
        } else {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.rooView.setForeground(new ColorDrawable(ContextCompat.getColor(context, android.R.color.transparent)));
            }
        }

    }

    @Override
    public int getItemCount() {
        return notes.size();
    }

    public NotePad getItem(int position) {
        return notes.get(position);
    }

    public void setSelectedIds(List < Long > selectedIds) {
        this.selectedIds = selectedIds;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView noteTitle, noteDate, noteTime;
        ConstraintLayout rooView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);

            noteTitle = itemView.findViewById(R.id.note_title);
            noteDate = itemView.findViewById(R.id.note_date);
            noteTime = itemView.findViewById(R.id.note_time);
            rooView = itemView.findViewById(R.id.root_view);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), ContentActivity.class);
                    i.putExtra("ID", notes.get(getAdapterPosition()).getId());
                    v.getContext().startActivity(i);
                }
            });
        }
    }
}

In your activity/fragment where you are initiating this adapter pass this for the listener and implement the interface and whenever you click any item on the recyclerview then you will your whole NotePad object inside this function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25367&siteId=1