删除某些文件后,我遇到更新gridView
的问题。我一直在寻找解决方案几天但仍然无法解决问题。代码工作正常,所有缩略图都非常流畅,除非删除某些文件,gridView
上没有任何更改。通过一些阅读,必须使用notifyDataSetChange()
。但我不太清楚如何使用它以及在哪里使用它。
这就是我所拥有的:
public class ImageGridActivity extends Activity{
Cursor cursor;
private GridView mGridView;
ImageAdapter adapter;
private ArrayList<ImageViewInfo> videoRows;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images_layout);
String[] thumbColumns = { MediaStore.Images.Thumbnails.DATA,
MediaStore.Images.Thumbnails.IMAGE_ID };
String[] mediaColumns = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.MIME_TYPE };
cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);
videoRows = new ArrayList<ImageViewInfo>();
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
Log.v("", newVVI.title);
newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
Log.v("", newVVI.mimeType);
videoRows.add(newVVI);
} while (cursor.moveToNext());
}
adapter = new ImageAdapter(this, videoRows);
mGridView = (GridView)findViewById(R.id.gridview5);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, final int position, long id) {
//Toast.makeText(ImageGridActivity.this, "" + position, Toast.LENGTH_SHORT).show();
/*
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE);
String videoFilePath = cursor.getString(fileColumn);
String mimeType = cursor.getString(mimeColumn);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File newFile = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(newFile), mimeType);
startActivity(intent);
}
*/
final CharSequence[] items = {
"Open", "Upload","Delete", "Properties"
};
AlertDialog.Builder builder = new AlertDialog.Builder(ImageGridActivity.this);
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Do something with the selection
//mDoneButton.setText(items[item]);
if(items[item].equals("Open"))
{
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
int mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE);
String videoFilePath = cursor.getString(fileColumn);
String mimeType = cursor.getString(mimeColumn);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File newFile = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(newFile), mimeType);
startActivity(intent);
}
}
else if(items[item].equals("Delete"))
{
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String videoFilePath = cursor.getString(fileColumn);
File newFile = new File(videoFilePath);
newFile.delete();
}
//ImageAdapter adapter = new ImageAdapter(ImageGridActivity.this,videoRows);
//adapter.notifyDataSetChanged();
mGridView.invalidateViews();
mGridView.setAdapter(null);
String[] thumbColumns = { MediaStore.Images.Thumbnails.DATA,
MediaStore.Images.Thumbnails.IMAGE_ID };
String[] mediaColumns = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.MIME_TYPE };
cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);
ArrayList<ImageViewInfo> videoRows = new ArrayList<ImageViewInfo>();
if (cursor.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Cursor thumbCursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Images.Thumbnails.IMAGE_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
newVVI.thumbPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
Log.v("", newVVI.thumbPath);
}
newVVI.filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
newVVI.title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
Log.v("", newVVI.title);
newVVI.mimeType = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.MIME_TYPE));
Log.v("", newVVI.mimeType);
videoRows.add(newVVI);
} while (cursor.moveToNext());
}
mGridView.invalidateViews();
mGridView.setAdapter(new ImageAdapter(ImageGridActivity.this,videoRows));
}
else if (items[item].equals("Properties"))
{
if (cursor.moveToPosition(position)) {
int fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//int fileSize = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);
String filePath = cursor.getString(fileColumn);
//String fileSizes = cursor.getString(fileSize);
File newFile = new File(filePath);
AlertDialog.Builder builder = new AlertDialog.Builder(ImageGridActivity.this);
builder.setTitle("File Details :");
builder.setMessage("Name: "+newFile.getName());
builder.setMessage("Path: "+filePath);
//builder.setMessage("Size: "+fileSizes);
builder.setCancelable(true);
AlertDialog alert = builder.create();
alert.show();
}
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
class ImageViewInfo {
String filePath;
String mimeType;
String thumbPath;
String title;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private List<ImageViewInfo> videoItems;
LayoutInflater inflater;
public ImageAdapter(Context _context,
ArrayList<ImageViewInfo> _items) {
context = _context;
videoItems = _items;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return videoItems.size();
}
public Object getItem(int position) {
return videoItems.get(position);
}
public long getItemId(int position) {
return position;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
/*
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(getBaseContext());
imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
*/
//View videoRow = inflater.inflate(R.layout.row, null);
final ImageView videoThumb = (ImageView) getLayoutInflater().inflate(R.layout.rows, parent, false);
if (videoItems.get(position).thumbPath != null) {
videoThumb.setImageURI(Uri
.parse(videoItems.get(position).thumbPath));
}
else
{
videoThumb.setImageResource(R.drawable.ic_empty);
}
/*
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.row, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageURI(Uri.parse(videoItems.get(position).thumbPath));
//Log.v("", "file://"+videoItems.get(position).thumbPath);
//imageView.setImageURI(Uri.parse("file://"+videoItems.get(position).filePath));
*/
return videoThumb;
}
}
答案 0 :(得分:0)
您需要从适配器中的imageList中删除该文件。这样,文件路径就会从列表中删除,它只显示列表中的图像。
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path){
itemList.add(path);
}
void remove(String path){
itemList.remove(path);
}
}
public void function_delete(int id){
File file = new File(path);
if(file.exists())
{
boolean deleted = file.delete();
}
myImageAdapter.remove(path);
myImageAdapter.notifyDataSetChanged();
Toast.makeText(this, "Deleted", Toast.LENGTH_SHORT).show();
}