我正在尝试从SQLite数据库中删除数据,但它无法正常工作。
请参阅SQlite数据库 DELETE
查询:
public void deleteCompany(long company_id) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete(COMPANY, KEY_ID + " = ?", new String[] { String.valueOf(company_id) });
}
我在哪里删除数据 CompaniesFragment.java
:
rvCompanies_view.addOnItemTouchListener(new RecyclerTouchListener(getActivity().getApplicationContext(), rvCompanies_view, new AddAffiliatedCompanyActivity.ClickListener() {
@Override
public void onLongClick(final View child, final int childPosition) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog);
alertDialog.setTitle("Confirm Delete...");
alertDialog.setMessage("Are you sure you want delete this Company?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
myDb.deleteCompany(childList.get(childPosition).getId());
childList.remove(childPosition);
adapterRecycler.notifyDataSetChanged();
Toast.makeText(getActivity().getApplicationContext(), "Company Deleted", Toast.LENGTH_SHORT).show();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onClick(View child, int childPosition) {
}
}));
下面的代码可以在RecyclerView上找到,但是我将单独定义以便了解击球手:
myDb.deleteCompany(childList.get(childPosition).getId());
childList.remove(childPosition);
adapterRecycler.notifyDataSetChanged();
Toast.makeText(getActivity().getApplicationContext(), "Company Deleted", Toast.LENGTH_SHORT).show();
更新
我将更改我的代码,见下文:
Log.e("TAG", "Size Of Array :" + childList.size());
Log.e("TAG", "Id" + childList.get(childPosition).getId());
myDb.deleteCompany(childList.get(childPosition).getId());
childList.remove(childPosition);
adapterRecycler.notifyItemRemoved(childPosition);
//adapterRecycler.notifyDataSetChanged();
//Log.d("ADebugTag", "Value: " + childList);
Toast.makeText(getActivity().getApplicationContext(), "Company Deleted", Toast.LENGTH_SHORT).show();
并且它给我带来了错误: java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
这只是一个Toast消息,只删除数据或任何东西
答案 0 :(得分:1)
尝试使用
adapterRecycler.notifyItemRemoved(childPosition)
adapterRecycler.notifyItemRangeChanged(childPosition, getItemCount());
而不是
adapterRecycler.notifyDataSetChanged();
答案 1 :(得分:0)
解决问题:
问题是数据位置
第一个问题在线下:
List<Company> childList = new ArrayList<Company>();
上面的行创建了一个问题,我的数组列表becoze我会使用这个适配器变量becoz,我不能得到任何数组大小
所以我打印 LOG
并查看数组大小:
Log.e("TAG", "Size Of Array :" + childList.size() + "Position :" + childList);
第二个我打印 LOG
我的数据ID:
Log.e("TAG", "Id" + childList.get(childPosition).getId());
然后我理解数据是以DESC顺序显示的,我将改变我的上一行,如我的下线:
Log.e("TAG", "Id" + childList.get(childPosition - 1 ).getId());
然后我使用下面的行删除我的记录:
myDb.deleteCompany(childList.get(childPosition).getId());
上面一行删除一条记录,但如果在数组1记录中假设,那么就生成错误。
然后我将更改代码:
myDb.deleteCompany(childList.get(childPosition - 1 ).getId());
我的问题已解决
请参阅我的解决方案的完整代码:
Log.e("TAG", "Size Of Array :" + childList.size() + "Position :" + childList);
Log.e("TAG", "Id" + childList.get(childPosition - 1 ).getId());
myDb.deleteCompany(childList.get(childPosition - 1 ).getId());
rvCompanies_view.removeViewAt(childPosition);
Log.e("TAG", "Size Of Array :" + childList.size() + "Position :" + childList);
adapterRecycler.notifyDataSetChanged();
Toast.makeText(getActivity().getApplicationContext(), "Company Deleted", Toast.LENGTH_SHORT).show();
答案 2 :(得分:0)
将接口设置为回收器视图适配器,用于点击句柄,如下面的代码..
onItemClickListner onItemClickListner;
public void setOnItemClickListner(RecyclerViewAdpater.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick(String str);//pass your object types.
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
String data = mStringList.get(position); // if you pass object of class then create that class object.
// below code handle click event on recycler view item.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick(data);
}
});
然后在活动之后将适配器绑定到Recycler视图后面放下代码..
recyclerViewAdpater.setOnItemClickListner(new RecyclerViewAdpater.onItemClickListner() {
@Override
public void onClick(String str) {
// here you show your alert dialog.
// below code remove item from list
stringsList.remove(stringsList.indexOf(str));
recyclerViewAdpater.notifyDataSetChanged();
}
});