0

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?

Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
K_uu_K
  • 1

1 Answers1

0

I think the easiest way to do this is to update the data structure (maybe your ArrayList) attached to your adapter.

When you change the file name make sure you change the corresponding name inside of your ArrayList (just update the string value with the new file name) and then call

yourAdapter.notifyDataSetChanged();

For example if you are changing the file name corresponding to your 3-th position inside your list just make something like :

yourArrayList.get(2) = "new file name";

where 2 is the index of the 3-th element inside your list.

Then if you call notifyDataSetChanged the new name will popup on the screen.

Your list is updated when you exit the app because of all file names are read from the beginning. Goodluck.

Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27