RecyclerView:为什么错误的项目从SQLlite数据库中删除?

时间:2016-08-03 06:02:17

标签: android sqlite android-recyclerview

我有RecyclerView使用SQLite database来存储收集的用户输入,然后显示在CardView上。我正在尝试添加 removeItem 方法,以便从CardView列表中删除RecyclerView removeItem 方法由适配器中的onItemLongClick触发,该工作正常。它会启动DialogFragment,要求用户确认删除。该对话框正确启动,SQLite删除方法用于从数据库中删除一行,但它正在删除错误的行。因此删除了错误的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);
}

1 个答案:

答案 0 :(得分:0)

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