【问题标题】:RecyclerView: why is wrong Item removed from SQLlite database?RecyclerView:为什么从 SQLite 数据库中删除了错误的项目?
【发布时间】:2016-08-03 06:02:17
【问题描述】:

我有一个RecyclerView,它使用SQLite database 来存储收集的用户输入,然后显示在CardView 上。我正在尝试添加一个 removeItem 方法以从 RecyclerView 列表中删除 CardViewremoveItem 方法由工作正常的适配器的onItemLongClick 触发。它会启动一个DialogFragment,要求用户确认删除。对话框正确启动,SQLite delete 方法可以从数据库中删除一行,但它正在删除错误的行。所以错误的CardView 被删除。我在这里错过了什么?

ContactListAdapter 文件:

public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder>{

private List<Contact> contactList;
private Context context;

private RecyclerItemClickListener recyclerItemClickListener;

public ContactListAdapter(Context context) {
    this.context = context;
    this.contactList = new ArrayList<>();
}


      public Contact getItem(int position) {
        return contactList.get(position);
      }  

    @Override
    public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);

    final ContactHolder contactHolder = new ContactHolder(view);


    // Attach a LongClick listener to the items's (row) view.
    contactHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            int adapterPos = contactHolder.getAdapterPosition(); // get the item position.
            if (adapterPos != RecyclerView.NO_POSITION) {
                if (recyclerItemClickListener != null) {
                    recyclerItemClickListener.onItemLongClick(adapterPos, contactHolder.itemView);
                }
            }
            return true;
        }
    });

    return contactHolder;
}

SQLite 文件:

public void delete(int id){
    SQLiteDatabase db = getReadableDatabase();
    String selection = ContactField.COLUMN_ID + " LIKE ?";        
    String[] selectionArgs = { String.valueOf(id) };
    db.delete(ContactField.TABLE_NAME, selection, selectionArgs);
}

来自 DialogFragment 文件:

    // When the user clicks "OK" on the dialog, delete the CardView's data 
    // from the database and then re-set the RecyclerViewAdapter.
    Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
    btnOK.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // Run the MainActivity removeItem method to:
            // 1) delete the onLongClicked item from the database,
            // 2) load the remaining database data into a new ArrayList,
            // 3) clear the Adapter and then
            // 4) set the Adapter and LayoutManager.
            **((MainActivity)getActivity()).removeItem(position);**

            dismiss();
        }
    });

来自 MainActivity 文件:

@Override
public void onItemClick(int position, View view) {
    ActActivity.start(this, contactListAdapter.getItem(position));
}

@Override
public void onItemLongClick(int position, View view) {        
    android.app.FragmentManager fm = getFragmentManager();
    DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
    delCardViewDialog.show(fm,"delcardview dialog");
}

// The DeleteCardViewFragment runs this method when the user confirms via a
// dialog that they want to delete an item from the RecyclerView list.
public void removeItem(int position) {
    **sqLiteDB.delete(contactListAdapter.getItem(position).getId());**
    contactListAdapter.clear();
    loadData();
    lvContact.setLayoutManager(linearLayoutManager);
    lvContact.setAdapter(contactListAdapter);
}

【问题讨论】:

  • getId() 返回什么?您确定它的值与数据库中的行 ID 相同吗?
  • 是的,getId() 返回模型类中设置的_id。
  • 您应该使用调试器遍历整个过程,并查看在所有方法之间传递的值。例如,知道 DialogFragment 中的位置取自哪里会很有趣。
  • 同意。我是 Android 新手,对调试器的经验为零,但分解测试应该有助于解决问题。
  • 好的,如果您对调试器不满意,您可以使用 Log.d() 将调试消息打印到 LogCat。尝试在每一步打印 ID,看看哪些值被删除,哪些值存在于列表中。

标签: android sqlite android-recyclerview


【解决方案1】:

尝试在onBindViewHolder 方法而不是onCreateViewHolder 中设置您的点击侦听器。如果您这样做,您可以轻松获得点击的位置和项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 2021-04-11
    • 2021-10-27
    • 2019-05-16
    相关资源
    最近更新 更多