我的应用程序有一个片段,显示GridView中文件夹中的图像。
在gridview Item OnClick上,我传递了filepath,filename&通过意图定位到另一个活动。
此活动以全尺寸显示图片... 我使用Delete选项从SdCard中删除Image,然后调用finish()以退出活动... 图像被删除但Gridview没有刷新... 它仍然显示缩略图!
如何在调用finish();?
时刷新Gridview显示Gridview的片段 -
public class Downloaded extends Fragment{
// Declare variables
private String[] FilePathStrings;
private String[] FileNameStrings;
private File[] listFile;
GridView grid;
GridViewAdapter adapter;
File file;
TextView empty;
@Override
public void onActivityCreated( Bundle
savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getActivity().setTitle("Saved");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Inflating the Layout
View rootView = inflater.inflate(R.layout.gridview_main, container, false);
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(getActivity(), "Error! No SDCARD Found!", Toast.LENGTH_LONG)
.show();
} else {
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Sample");
// Create a new folder if no folder named SDImageTutorial exist
file.mkdirs();
}
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
// Locate the GridView in gridview_main.xml
grid = (GridView) rootView.findViewById(R.id.gridview);
empty = (TextView) rootView.findViewById(R.id.empty);
empty.setText("You haven't saved any Pic yet...!");
grid.setEmptyView(empty);
// Pass String arrays to LazyAdapter Class
adapter = new GridViewAdapter(getActivity(), FilePathStrings, FileNameStrings);
// Set the LazyAdapter to the GridView
grid.setAdapter(adapter);
// Capture gridview item click
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getActivity(), ViewImage.class);
// Pass String arrays FilePathStrings
i.putExtra("filepath", FilePathStrings);
// Pass String arrays FileNameStrings
i.putExtra("filename", FileNameStrings);
// Pass click position
i.putExtra("position", position);
startActivity(i);
}
});
return rootView;
}
}
&安培;这是删除图像的ViewImage类 -
public class ViewImage extends AppCompatActivity {
// Declare Variable
TextView text;
public String[] filepath;
public int position;
public String[] filename;
private TouchImageView imageview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from view_image.xml
setContentView(R.layout.img_act);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
// Retrieve data from MainActivity on GridView item click
Intent i = getIntent();
// Get the position
position = i.getExtras().getInt("position");
// Get String arrays FilePathStrings
filepath = i.getStringArrayExtra("filepath");
// Get String arrays FileNameStrings
filename = i.getStringArrayExtra("filename");
// Locate the TextView in view_image.xml
//text = (TextView) //findViewById(R.id.imagetext);
// Load the text into the TextView followed by the position
getSupportActionBar().setTitle(filename[position]);
// Locate the ImageView in view_image.xml
imageview = (TouchImageView) findViewById(R.id.full_image_view);
// Decode the filepath with BitmapFactory followed by the position
Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
// Set the decoded bitmap into ImageView
imageview.setImageBitmap(bmp);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.image_view, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()){
case R.id.vi;
File f = new File(filepath[position]);
if (f.exists())
{
f.delete ();
}
this.finish();
break;
case android.R.id.home:
onBackPressed();
break;
}
return true;
}
}
答案 0 :(得分:0)
删除并完成活动后,您必须在adapter.notifyDataSetChanged()
处的片段中调用onActivityResult
。
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(getActivity(), ViewImage.class);
// Pass String arrays FilePathStrings
i.putExtra("filepath", FilePathStrings);
// Pass String arrays FileNameStrings
i.putExtra("filename", FileNameStrings);
// Pass click position
i.putExtra("position", position);
startActivityForResult (i, 101);
}
});
ViewImage活动中的onDelete点击下面写一下
case R.id.vi;
File f = new File(filepath[position]);
if (f.exists())
{
f.delete ();
}
Intent i = new Intent();
i.putInt("POS", your_deleted_item_pos");
setResult(RESULT_OK, i);
finish();
break;
和片段
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 101:
if(data.containsKey("POS"){
int pos = data.getInt("POS");
/*here you have to Remove item from FilePathStrings and FileNameStrings for particular position.*/
adapter.notifyDataSetChanged(); // it only done when your deleted image is remove from list which you bind in gridview.
break;
}
}
}