【问题标题】:Android: ListView of files doesn't update file name after rename of fileAndroid:文件的ListView在重命名文件后不更新文件名
【发布时间】:2017-01-04 04:01:11
【问题描述】:

我有一个文件列表视图,我希望刷新/更新列表视图 当我在 ListView 中重命名文件时。

我的删除部分有效(if (item.getTitle() == getString(R.string.delete))) 但不是 ListView 更新部分 (else if (item.getTitle() == getString(R.string.rename))) 在我重命名一个文件之后。

重命名方法有效 (f.renameTo(to);) 但要查看新名称 ListView 中的文件我必须关闭并重新打开我的应用程序,但我想查看 重命名文件后直接在 ListView 中重命名文件名。

@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();
    }

重命名文件后,我该怎么做才能直接在 ListView 中看到重命名的文件?

【问题讨论】:

    标签: android listview rename


    【解决方案1】:

    我认为最简单的方法是更新附加到适配器的数据结构(可能是您的 ArrayList)。

    当您更改文件名时,请确保更改 ArrayList 中的相应名称(只需使用新文件名更新字符串值),然后调用

    yourAdapter.notifyDataSetChanged();
    

    例如,如果您要更改与列表中第 3 位相对应的文件名,请执行以下操作:

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

    其中 2 是列表中第 3 个元素的索引。

    然后,如果您调用 notifyDataSetChanged 新名称将在屏幕上弹出。

    当您退出应用程序时,您的列表会更新,因为所有文件名都是从头开始读取的。 祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2014-06-07
      • 2021-06-03
      • 2014-09-09
      • 1970-01-01
      相关资源
      最近更新 更多