有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何阻止Recyclerview生成重复项并停止洗牌项目?

我是这里的新手,正在开发notes应用程序。我注意到我在recyclerview中的笔记在滚动时被复制并移动位置。我花了很多时间修改代码,但运气不好。希望你们能通读代码。谢谢你抽出时间

**相关适配器代码为**

                public void setdata(List<NotesEntry> notesEntries) {
                    notesEntryList=notesEntries;  //i used your code here .it gives a null pointer exception
                    notifyDataSetChanged();
                }

                public List<NotesEntry> getnotesentries() {
                    return notesEntryList;
                }

                @Override
                public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                    try {

                        int i = v1.getAdapterPosition();
                        NotesEntry notesEntries = notesEntryList.get(position);
                        id = notesEntries.getId();
                        text = notesEntries.getText();
                        Log.d("idies", text);
                        b1.setText(String.valueOf(id));
                        textview1.setText(text);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }



                public interface Itemlistener {
                    void clicked(int clickedposition, View v, int id, String text);

                }

                class Viewholder1 extends RecyclerView.ViewHolder implements View.OnClickListener {
                    public Viewholder1(@NonNull View itemView) {
                        super(itemView);
                        textview1 = (TextView) itemView.findViewById(R.id.textView2);
                        b1 = (Button) itemView.findViewById(R.id.button2);

                        b1.setOnClickListener(this);
                        textview1.setOnClickListener(this);

                        Log.d("ViewHolder1", "h");
                    }

                    @Override
                    public void onClick(View view) {

                        itemlistener.clicked(getAdapterPosition(), view, notesEntryList.get(getAdapterPosition()).getId(), notesEntryList.get(getAdapterPosition()).getText());

                    }
                }

            }

这就是我在主要活动中实现onclick方法的方式

@Override
public void clicked(int clickedposition, View v, int iditem, String textitem) {
    try {
        switch (v.getId()) {
            case R.id.button2:
                entries = adapter.getnotesentries();// entries of notes in my list
                int n = db.Dao().delete(entries.get(clickedposition));
                retrievetasks();

                break;
            case R.id.textView2:
                Intent intent = new Intent(MainActivity.this, Notes.class);
                intent.putExtra("newnotes", defaultnotes);
                intent.putExtra("id", iditem);
                intent.putExtra("text", textitem);
                startActivity(intent);
                break;
        }

    } catch (Exception E) {
        E.printStackTrace();
    }

}

}`

如果需要,我会提供额外的代码


共 (1) 个答案

  1. # 1 楼答案

    从数据库中删除项目后,您应该:

    • 重新加载整个列表并重置适配器(更改列表 和notifyDataSetChanged()中的setData()

    • 适配器的列表中删除该项,然后调用notifyItemRemoved()

    这两种操作都会混淆适配器,因为它认为您正在从刚刚设置的列表中删除项目(该列表不包含已删除的项目)