I have a recyclerview and sql lite database. I keep city names in SQLite database and display them in recyclerview. When the city is setOnclickListener in the recyclerview, the city’s weather is shown and I do this by taking the city’s id from the sql lite database.
arrayList.get(position).id
it worked in setOnClickListener but it doesnt work setOnLongClickListener
I want to delete the city name in the recyclerview when I longclick but not deleted becasue it doesnt work.
This error appears in logcat : ” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 “ or something like that
How can I solve this issue ?
My Adapter class
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); } } }
Advertisement
Answer
you have in your code
arrayList.remove(position);
then few lines below
dbId = arrayList.get(position).id; // <---the error is in this code
your array list is empty after remove
call, but you are still trying to get this item at position
, thus you are getting IndexOutOfBoundsException: Index: 0, Size: 0
– you are trying to obtain item at position = 0
and array is empty
try to move obtaining dbId
BEFORE removing from array
dbId = arrayList.get(position).id; arrayList.remove(position); notifyItemRemoved(position); //notifyItemRangeChanged(position,arrayList.size()); // this line is unnecessary