我有一個 recyclerview 和 sql lite 資料庫。我將城市名稱保存在 SQLite 資料庫中并在 recyclerview 中顯示它們。當城市在 recyclerview 中設定為 setOnclickListener 時,城市的天氣就會顯示出來,我通過從 sql lite 資料庫中獲取城市的 id 來做到這一點。
arrayList.get(position).id
它在 setOnClickListener 中作業,但在 setOnLongClickListener 中不起作用
我想在我長按時洗掉 recyclerview 中的城市名稱但沒有洗掉,因為它不起作用。
此錯誤出現在 logcat 中:“ java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 ” 或類似的內容
我該如何解決這個問題?
我的配接器類
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
ArrayList<City> arrayList;
Context context;
SQLiteDatabase db ;
Cursor cursor;
int dbId;
public Adapter(ArrayList<City> arrayList ,Context context ){
this.arrayList = arrayList;
this.context = context;
db = context.openOrCreateDatabase("City",MODE_PRIVATE,null);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerviewRowBinding recyclerviewRowBinding =
RecyclerviewRowBinding.inflate(LayoutInflater.from(parent.getContext()),
parent,
false);
return new MyViewHolder(recyclerviewRowBinding);
}
@Override
public void onBindViewHolder(@NonNull Adapter.MyViewHolder holder, int position) {
holder.binding.MytxtCities.setText(arrayList.get(position).cityName);
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.warningicon);
builder.setMessage("Are you sure that you want to delete " arrayList.get(position).cityName);
builder.setPositiveButton("Ok", (dialog, which) -> {
arrayList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,arrayList.size());
dbId = arrayList.get(position).id; // <---the error is in this code
System.out.println("dbId" dbId);
//db.execSQL("DELETE FROM city WHERE id ='" arrayList.get(position).id "'");
cursor = db.rawQuery("SELECT * FROM city WHERE id=?",new String[]{String.valueOf(dbId)});
Result(cursor);
}).setNegativeButton("Cancel", (dialog, which) -> {
//doing nothing
}).show();
return true;
}
});
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(holder.itemView.getContext(),MainActivity.class);
intent.putExtra("citId",arrayList.get(position).id); // <-- it works very well here and sends with intent
holder.itemView.getContext().startActivity(intent);
});
}
private void Result(Cursor cursor){
if(cursor.getCount() > 0){
db.delete("city","id=?",new String[]{String.valueOf(dbId)});
notifyDataSetChanged();
}
else{
Toast.makeText(context,"something went wrong !",Toast.LENGTH_LONG).show();
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView Mytxt_cities;
private RecyclerviewRowBinding binding;
public MyViewHolder(@NonNull RecyclerviewRowBinding binding) {
super(binding.getRoot());
this.binding = binding;
Mytxt_cities = itemView.findViewById(R.id.Mytxt_cities);
}
}
}
uj5u.com熱心網友回復:
你的代碼中有
arrayList.remove(position);
然后下面幾行
dbId = arrayList.get(position).id; // <---the error is in this code
你的陣列串列是空后remove呼叫,但你仍然試圖讓這個專案position,所以你得到IndexOutOfBoundsException: Index: 0, Size: 0-你試圖在獲得專案position = 0和陣列為空
嘗試dbId在從陣列中洗掉之前移動獲取
dbId = arrayList.get(position).id;
arrayList.remove(position);
notifyItemRemoved(position);
//notifyItemRangeChanged(position,arrayList.size()); // this line is unnecessary
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382588.html
