我想在ListView项目行中单击ImageView时刷新/更新ListView数据。
这是我的Adapter类
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;
}
我试图在Activity类中调用此方法,但它在删除后没有更新ListView数据
public void refreshDataStorage(List<EntityObject> listObject) {
this.dataStorage = listObject;
this.notifyDataSetChanged();
}
此方法在宿主Activity类
的onCreate()方法中调用 /// add an adapter for the listView
workingDataSource = returnDataSource(mainStorageDirectory);
listImageAdapter = new ListImageAdapter(ListImagesActivity.this, workingDataSource);
listAllImages.setAdapter(listImageAdapter);
listImageAdapter.refreshDataStorage(workingDataSource);
感谢任何形式的帮助
答案 0 :(得分:1)
请转到
public void refreshDataStorage(List<EntityObject> listObject) {
this.dataStorage.clear();
this.dataStorage.addAll(listObject);
this.notifyDataSetChanged();
}
答案 1 :(得分:1)
作为我的评论,您已删除该文件,但listObject
未更新(其项目未删除)。所以,请拨打以下内容
listObject.remove(...);
adapter.notifyDataSetChanged();
答案 2 :(得分:0)
你走了,
@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.setTag(position);
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();
dataStorage.remove((Integer) v.getTag());
notifyDataSetChanged();
}
}
});
return convertView;
}