Android Set On CLick Update & Delete ImageButton SQLite data from Adapter not working

Issue

I already made the update and delete button inside item_report.xml, and the function in my Adapter.class. I put my recyclerview inside fragment which i made 2 tab in tablayout inside MainActivity, but when i pressed Delete or Edit/Update it’s not working.

But the toast “This should be working” i put inside Update worked but not with alertdialog for update, and AlertDialog in Delete appeared & worked. I think something wrong with my Data Update & Delete but idk where cuz i thought it’s no error in Run Log.

Here is my Edit & Delete in my Adapter.class:

    @Override
    public void onBindViewHolder(@NonNull Holder holder, @SuppressLint("RecyclerView") int position) {

        ReportDraft reportDraft  arrayList.get(position);

        final String id        reportDraft.getId();
        final String token     reportDraft.getToken();
        final String email     reportDraft.getEmail();


        holder.judul.setText(judul);



        holder.editbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(), "This should be working", Toast.LENGTH_SHORT).show();
                editDialog(
                        ""+position,
                        ""+id,
                        ""+token,
                        ""+email


                );

            }
        });

        holder.deletebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteDialog(
                        ""+id
                );
            }
        });


    }

    private void deleteDialog(final String id){

        AlertDialog.Builder builder  new AlertDialog.Builder(context);
        builder.setTitle("Delete");
        builder.setMessage("Are you sure to delete it?");
        builder.setCancelable(false);
        builder.setIcon(R.drawable.ic_delete);

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                databaseHelper.deleteInfo(id);
                //FragmentDraft frd  (FragmentDraft) ;
                ((MainActivity)context).onResume();
                Toast.makeText(context, "Aw", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });

        builder.create().show();

    }

    private void editDialog(String position, String id, String token, String email) {

        AlertDialog.Builder builder  new AlertDialog.Builder(context);
        builder.setTitle("Update");
        builder.setMessage("Update data?");
        builder.setCancelable(false);

        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                Intent intent  new Intent(context, EditReportActivity.class);
                intent.putExtra("ID", id);
                intent.putExtra("TOKEN", token);
                intent.putExtra("EMAIL", email);


                intent.putExtra("editMode", true);
                context.startActivity(intent);
            }
        });

        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                dialogInterface.cancel();
            }
        });
    }

is this bcuz my fragment or main activity or what? cuz i put recyclerview inside fragment, the fragemnt is inside tablayout in my MainActivity.

UPDATE

The delete button success to delete but the list is not changed unless i open other activity and go back

it SOLVED, sorry i forgot to set builder.create().show(). Thank you

Solution

Your deletion, while removing the item from your database, is not removing the item from the Adapter’s data source (your arrayList) and also not notifying the Adapter that there was a change in data.

Pass the selected item position to your deleteDialog method:

holder.deletebtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        deleteDialog(
                position,
                ""+id
        );
    }
});

Add a deletion of the item from your data source:

private void deleteDialog(final int position, final String id){

    AlertDialog.Builder builder  new AlertDialog.Builder(context);
    builder.setTitle("Delete");
    builder.setMessage("Are you sure to delete it?");
    builder.setCancelable(false);
    builder.setIcon(R.drawable.ic_delete);

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            databaseHelper.deleteInfo(id);

            arrayList.remove(position);
            notifyDatasetChanged();

            Toast.makeText(context, "Aw", Toast.LENGTH_SHORT).show();
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });

    builder.create().show();

}

The added lines are:

arrayList.remove(position);

which removes the item from the Adapter’s data source

And:

notifyDatasetChanged();

Which tells the Adapter that the data was changed and it needs to reload.

As per your edit dialog not showing, it looks like you are missing the show() call:

Add this at the end of editDialog method:

builder.show()

Answered By – gioravered

Leave a Comment