简体   繁体   English

获取选定的recyclerView 项目的id android

[英]Getting id of a selected recyclerView item android

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.只是在 RecyclerView 列表中,当我选择一个或多个项目时,在菜单中我单击删除按钮,我的注释没有被删除。 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.我不知道如何将所选笔记的 id 与我的方法相关联,因为 deletNote 方法接收长值,但我的 selectedId 是 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.我意识到我的代码可以找到,但它不会刷新列表,并且还会从 ActionMode 返回到工具栏。 I tried adapter.notifyDataSetChanged();我试过adapter.notifyDataSetChanged();

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.在您的活动/片段你在哪里启动此adapter通过this为监听和实现接口,只要你点击recyclerview任何项目,那么你将这个功能里面你的整个记事本的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM