Nolly J Nolly J - 1 year ago 82
Android Question

I want to update /refresh a ListView when a List item is deleted

I want to refresh /update ListView data when an ImageView is click in the ListView item row.

This is my Adapter class

public class ListImageAdapter extends BaseAdapter {

private LayoutInflater layoutInflater;
private List<EntityObject> dataStorage;

private Context context;

public ListImageAdapter(Context context, List<EntityObject> customizedListView) {
this.layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.dataStorage = customizedListView;

public int getCount() {
return dataStorage.size();

public Object getItem(int position) {
return dataStorage.get(position);

public long getItemId(int position) {
return position;

public View getView(int position, View convertView, final ViewGroup parent) {
final ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.list_images, parent, false);

listViewHolder.downloadedImage = (ImageView)convertView.findViewById(;
listViewHolder.deleteIcon = (ImageView)convertView.findViewById(;
listViewHolder.imageFilename = (TextView)convertView.findViewById(;
listViewHolder = (ViewHolder) convertView.getTag();

Uri imageUri = Uri.fromFile(new File(Helper.PATH_TO_EXTERNAL_STORAGE + File.separator + dataStorage.get(position).getImage()));
Glide.with(context).load(imageUri).override(90, 90).centerCrop().into(listViewHolder.downloadedImage);

listViewHolder.deleteIcon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String deleteImageName = listViewHolder.imageFilename.getText().toString();
String deletePath = Helper.PATH_TO_EXTERNAL_STORAGE + File.separator + deleteImageName;
File deleteFile = new File(deletePath);
Toast.makeText(context, "The image has been deleted", Toast.LENGTH_LONG).show();
return convertView;
public void refreshDataStorage(List<EntityObject> listObject) {
this.dataStorage = listObject;

static class ViewHolder{

ImageView downloadedImage;

ImageView deleteIcon;

TextView imageFilename;

I have tried to call this method in the Activity class but it did not update the ListView data after deletion

public void refreshDataStorage(List<EntityObject> listObject) {
this.dataStorage = listObject;

This method is called in onCreate() method of the host Activity class

/// add an adapter for the listView
workingDataSource = returnDataSource(mainStorageDirectory);
listImageAdapter = new ListImageAdapter(ListImagesActivity.this, workingDataSource);


Any kind of help is appreciated

Answer Source

As my comment, you have deleted the file, however, the listObject not updated (its item not removed). So, please call something like the following

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download