0
My Appには、GridViewのフォルダの画像を示すフラグメントがあります。GridViewで画像データを更新する方法
グリッドビューのOnClickでは、ファイルパスのファイル名をインテントから別のアクティビティに渡します。
この活動は、フルサイズの画像を表示... 私は(SDカードから画像を削除するには、[削除]オプションを使用して、仕上げ)の活動を終了すると呼ばれ... 画像は削除されますが、GridViewのISN 't refresh ... まだサムネイルが表示されています!
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;
}
}
以下のように記述しますonDelete)、Iそのリクエストコード、結果コード&インテントデータなしでonActivityResultを使用する必要がありますか? RESULT_OKは何ですか? – DarShan
あなたのコードを共有してください。 –
updated que。コード: – DarShan