简体   繁体   中英

Deleting issue from Room Database

I have an issue deleting item from app. It,s Actually a notes app in which i have created a AlertDialog function that deletes notes. here is code for my function

 private void showDeleteDialog(int position) {
        AlertDialog.Builder alert = new AlertDialog.Builder(context)
                .setTitle("Delete view")
                .setMessage("Are you sure to delete")
                .setIcon(R.drawable.ic_baseline_delete_24)
                .setPositiveButton("yes", (dialogInterface, i) -> databaseHelper.notesDao().deleteNotes(new Notes(arrNotes.get(position).id, arrNotes.get(position).title, arrNotes.get(position).text)))
                .setNegativeButton("No", (dialogInterface, i) -> {

                });
        alert.show();
    }

Here i call it

        holder.llView.setOnLongClickListener(view -> {
            showDeleteDialog(position);
            return true;
        });

When i click yes then app crashed but it succuessfully delets notes from database.This is the error i got

    java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{a4d7d56 position=1 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{815f028 VFED..... ......I. 31,171-689,776 #7f090165 app:id/recycler_view}, adapter:com.example.keepnotes.RecyclerViewAdapter@773a141, layout:androidx.recyclerview.widget.StaggeredGridLayoutManager@a3a6e6, context:com.example.keepnotes.MainActivity@3065e55

All of this is in RecyclerView Adapter. The code for recyclerView adapter is following

package com.example.keepnotes;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;
    ArrayList<Notes> arrNotes;
    DatabaseHelper databaseHelper;
    private RecyclerView.RecyclerListener listener;

    RecyclerViewAdapter(Context context, ArrayList<Notes> arrNotes, DatabaseHelper databaseHelper) {
        this.context = context;
        this.arrNotes = arrNotes;
        this.databaseHelper = databaseHelper;
    }

    public RecyclerViewAdapter(ArrayList<Notes> arrNotes, RecyclerView.RecyclerListener listener) {
        this.arrNotes = arrNotes;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.single_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.title.setText(arrNotes.get(position).title);
        holder.body.setText(arrNotes.get(position).text);
        holder.index.setText(String.valueOf(position + 1));


        holder.llView.setOnClickListener(view -> {
            Intent iNext = new Intent(context, ViewActivity.class);
            iNext.putExtra("title", arrNotes.get(position).title);
            iNext.putExtra("text", arrNotes.get(position).text);
            context.startActivity(iNext);
        });

        holder.llView.setOnLongClickListener(view -> {
            showDeleteDialog(position);
            return true;
        });


    }

    @Override
    public int getItemCount() {
        return arrNotes.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView title, body, index;
        CardView llView;

        public ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.text_title_view);
            body = itemView.findViewById(R.id.text_text_view);
            index = itemView.findViewById(R.id.index);
            llView = itemView.findViewById(R.id.card_View);
            databaseHelper = DatabaseHelper.getDatabase(context);
        }
    }

    private void showDeleteDialog(int position) {
        AlertDialog.Builder alert = new AlertDialog.Builder(context)
                .setTitle("Delete view")
                .setMessage("Are you sure to delete")
                .setIcon(R.drawable.ic_baseline_delete_24)
                .setPositiveButton("yes", (dialogInterface, i) -> databaseHelper.notesDao().deleteNotes(new Notes(arrNotes.get(position).id, arrNotes.get(position).title, arrNotes.get(position).text)))
                .setNegativeButton("No", (dialogInterface, i) -> {

                });
        alert.show();
    }
}


How can i get rid of this error????

It is Solved, Actually, in Live Data, I was using notifyiteminserted, I changed that to notifyDataSetChnaged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM