Nolly J Nolly J - 5 months ago 17
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;
}

@Override
public int getCount() {
return dataStorage.size();
}

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

@Override
public long getItemId(int position) {
return position;
}

@Override
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(R.id.displayed_image);
listViewHolder.deleteIcon = (ImageView)convertView.findViewById(R.id.delete_image);
listViewHolder.imageFilename = (TextView)convertView.findViewById(R.id.downloaded_filename);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder) convertView.getTag();
}

listViewHolder.imageFilename.setText(dataStorage.get(position).getName());
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() {
@Override
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);
if(deleteFile.exists()){
deleteFile.delete();
Toast.makeText(context, "The image has been deleted", Toast.LENGTH_LONG).show();
}
}
});
return convertView;
}
public void refreshDataStorage(List<EntityObject> listObject) {
this.dataStorage = listObject;
this.notifyDataSetChanged();
}

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.notifyDataSetChanged();
}


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);
listAllImages.setAdapter(listImageAdapter);

listImageAdapter.refreshDataStorage(workingDataSource);


Any kind of help is appreciated

BNK BNK
Answer

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

listObject.remove(...); 
adapter.notifyDataSetChanged();
Comments