I have a ListView of Files and I want that the ListView is refreshed/updated when I rename a file in the ListView.
My deletion part works (if (item.getTitle() == getString(R.string.delete))
)
but not the ListView update part (else if (item.getTitle() == getString(R.string.rename))
)
after I renamed a File.
The rename method works (f.renameTo(to);
) but to see the new name
of file in the ListView I have to close and reopen my app, but I want to see the
renamed file name in the ListView directly after I renamed the file.
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
pos_glob = info.position;
if (item.getTitle() == getString(R.string.delete)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.confirm));
builder.setMessage(getString(R.string.really_delete));
builder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { File f = listAdapter.getItem(pos_glob);
f.delete();
listAdapter.remove(f);
listAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
else if (item.getTitle() == getString(R.string.rename))
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
File f = listAdapter.getItem(pos_glob);
File directory = new File(Environment.getExternalStorageDirectory(), "my_app_ext_sto_dir");
File to = new File(directory, m_Text+".3gp");
f.renameTo(to);
//It works to colour the renamed ListView entry, but thats only a test and not what I want:
//listAdapter.getItem(pos_glob);
int pos_of_renamed_file_in_list = listAdapter.getPosition(f);
// http://stackoverflow.com/questions/2953381/android-is-it-possible-to-refresh-just-one-item-in-a-listview
View v = lv.getChildAt(pos_of_renamed_file_in_list - lv.getFirstVisiblePosition());
//v.setBackgroundColor(Color.GREEN);
// I commented in and out this commands to test but never reached that the
//File name is updated directly after I renamed a file, I have always to reopen the app
//to see the renamed file name in the ListView:
//v.postInvalidate();
v.invalidate();
lv.invalidateViews();
lv.invalidateViews();
lv.invalidate();
listAdapter.notifyDataSetChanged();
//DM_ListAdapter listAdapter_temp = new DM_ListAdapter(getApplicationContext());
//listAdapter.clear();
//listAdapter = listAdapter_temp;
//lv.setAdapter(listAdapter_temp);
//lv.setAdapter(listAdapter);
//lv.invalidateViews();
//lv.invalidate();
//listAdapter.notifyDataSetChanged();
notifyDataSetChanged_method();
//lv.post();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
What have I to do to see the renamed file in the ListView directly after I renamed the file?