-1

I have a PhotosFragment class which extends Fragment. In that class I have a gridview of images. When I delete image it will delete successfully, but the fragment couldn't refresh. So how to refresh the fragment ?

gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                imageId = imageArry.get(position).getID();
                deletePhoto(imageId);
                return true;
            }
        });

// method to deletePhoto

   protected void deletePhoto(int id) {

    final int t_id = id;
     AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
     alert.setTitle("Delete");
     alert.setMessage("Do you want to delete this image ?");

        alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                db.deleteContact(new Contact(t_id));
                //HERE I want to REFRESH THE FRAGMENT
                }
        });

        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });

        alert.show();

    }

Thanks in advance.

Sailendra
  • 1,318
  • 14
  • 29

3 Answers3

1

You need to call notifyDataSetChanged method from the adapter of the GridView to see the changes

Omar Albelbaisy
  • 827
  • 8
  • 15
0

Try to invalidate your layout View

rootView.findViewById(R.id.your_layout_id).invalidate();
Harald Hoerwick
  • 132
  • 1
  • 1
  • 8
0

I don't think you should refresh all fragment. You can either do something like this imageView.setVisibility(View.GONE) or, better, remove item from your gridView's adapter:

adapter.remove(object);
adapter.notifyDataSetChanged();

For more, I suggest refer to official documentation

Marius
  • 810
  • 7
  • 20