Search code examples
androidlistviewandroid-fragmentsandroid-listview

Refreshing the fragment after deleting a row from a list view


I've managed to created a custom list view and added a delete button to every row. The delete button is working well, when I click on the button the row gets deleted from the db. The problem is that when I delete a row it remains in the list until I refresh the fragment by moving to another fragment and come back and then it disappears from the list. What I want is that when I delete a row, it immediately disappears from the list view. I tried some solutions for refreshing the whole fragment or the list view after deleting is done but they didn't work!

This is the code I wrote for the delete button inside the adapter class

public View getView(final int position, View convertView, ViewGroup parent) {

        View theView = super.getView(position, convertView, parent);     // It took me three days to fix :) I posed a question for it in StackOverFlow
        Button deleteButt = (Button) theView.findViewById(R.id.deleteButton);
        deleteButt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // This code to delete a row from the class Orders was taken from Back4app help https://www.back4app.com/docs/android/parse-objects/crud
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Orders");
                query.whereEqualTo("objectId", mainObjectsArray.get(position)[3]);  // mainObjectsArray.get(position)[3] We fetched the object id and stored it in mainObjectArray. position is the position of the item in the list which is the same position of the the same item in the mainObjectArray. Smart isn't it?! :)
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                public void done(final List<ParseObject> object, ParseException e) {
                    if (e == null) {

                        // The dialog 
                        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
                        alert.setMessage("Are you sure you want to delete this order?")
                                .setNegativeButton("No", null)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        //Delete based on the position
                                        object.get(0).deleteInBackground(new DeleteCallback() {
                                            @Override
                                            public void done(ParseException e) {
                                                if (e == null) {
                                                    Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();
                                                    
                                                } else {
                                                    Toast.makeText(
                                                            getApplicationContext(),
                                                            e.getMessage().toString(),
                                                            Toast.LENGTH_LONG
                                                    ).show();
                                                }
                                            }
                                        });

                                    }
                                        });

                        alert.create().show();


                    } else {
                        Toast.makeText(
                                getApplicationContext(),
                                e.getMessage().toString(),
                                Toast.LENGTH_LONG
                        ).show();
                    }
                };
            });

            }
        });

        return theView;
    }

After

Toast.makeText(getActivity(), "Deleted!", Toast.LENGTH_SHORT).show();

I should write a code to refresh the fragment in order to grab the new data from the db. Any help would be approached! Many thank.


Solution

  • As soon as you click on delete button remove the item from your adapter list and use notifyDataSetChanged() and it will update your list items. You don't need to update full fragment for this.

    EDIT :- You can user notifyItemRemoved() instead of using notifyDataSetChanged() as this will update you whole recyclerView which is a heavy operation. Here you can find more details on notifyItemRemoved().