【问题标题】:Getting id of a selected recyclerView item android获取所选recyclerView项目android的ID
【发布时间】:2020-06-17 06:25:04
【问题描述】:

我制作了一个笔记应用程序,一切正常。只是在 RecyclerView 列表中,当我选择一个或多个项目时,在菜单中我单击删除按钮,我的笔记没有被删除。虽然 delete 方法在另一个地方有效。 这是菜单中删除按钮的方法:

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

我的数据库类中删除笔记的方法:

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

我不知道如何将所选笔记的 id 与我的方法相关联,因为 deleteNote 方法接收长值但我的 selectedId 是 List。 提前致谢。

更新

我意识到我的代码可以找到,但它不会刷新列表并且还会从 ActionMode 返回到 Toolbar。我试过了 adapter.notifyDataSetChanged();

【问题讨论】:

  • onActionItemClicked 函数在适配器中吗?
  • @OhhhThatVarun 实际上我在 MainActivity 中使用了实现 ActionMode.Callback。
  • 使用可以使用从适配器到活动/片段的回调。
  • 那么你能帮我做吗?因为除了这部分,一切正常。
  • 它删除笔记但不从适配器的内部列表中删除,在适配器中创建一个函数来提供笔记列表而不是通过适配器构造函数添加它,并在删除后调用它新列表的方法

标签: java android sqlite android-recyclerview


【解决方案1】:

使用此适配器。我用过接口。

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

在您启动此adapter 的活动/片段中,将this 传递给侦听器并实现接口,每当您单击recyclerview 上的任何项目时,您都会将整个记事本对象放入此函数中。

【讨论】:

  • 很抱歉,如果它有一些语法错误。我用 Kotlin 编码。
  • 没问题,非常感谢您抽出宝贵时间。再问一个问题。我现在如何执行删除?
  • 嗯..删除?你的recyclerview 项目中有删除按钮吗?
  • 在我的 actionMode 菜单中是的
  • 我不明白。你有没有按钮?您可以在回调中使用NotePad 传递您的视图,然后使用findViewById 来查看哪个视图已被点击。并在删除按钮上设置onClickListener
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-04
相关资源
最近更新 更多