从其他片段更新Listview

Mat*_*anb 2 android listview fragment

我正在使用Manishkpr上的教程来创建一个应用程序,你可以在其中滑动1)layoutOne:在这里你创建一个文件和2)layoutTwo:显示某个文件夹中所有创建文件的列表视图.

问题:如果您创建了一个文件,它不会立即显示在列表视图中.我发现我应该在LayoutOne.java中使用此代码:

   LayoutTwo fragment = (LayoutTwo) getFragmentManager().findFragmentByTag("TESTTWO");
            fragment.getAdapter().notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

在LayoutTwo.java中,我添加了:

private static final String TAG = "TESTTWO";

//and the function getAdapter:

public CustomArrayAdapter getAdapter() {

        return adapter;
    }
Run Code Online (Sandbox Code Playgroud)

但是,我得到一个nullpointer异常fragment.getAdapter().notifyDataSetChanged();.我怎么能解决这个问题,这实际上是最好的方法吗?

编辑

myList = new ArrayList<RecordedFile>();

        File directory = Environment.getExternalStorageDirectory();
        file = new File(directory + "/test/");

        File list[] = file.listFiles();

        for (int i = 0; i < list.length; i++) {
            if (checkExtension(list[i].getName()) == true) {

                RecordedFile q = new RecordedFile();
                q.setTitle(list[i].getName());
                q.setFileSize(readableFileSize(list[i].length()));


                myList.add(q);
            }
        }

        adapter = new CustomArrayAdapter(myContext,
                R.layout.listview_item_row, myList);
        listView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)

Luk*_*rog 8

我正在使用Manishkpr上的教程来创建一个应用程序,你可以在其中滑动1)layoutOne:在这里你创建一个文件和2)layoutTwo:显示某个文件夹中所有创建文件的列表视图.

问题:如果您创建了一个文件,它不会立即显示在列表视图中.

如果您只有两个布局可以滑动,这意味着它们将在内存中拥有视图并可供访问.然后你可以为ListView它分配一个id ,当它刷新数据的时候,只需在它ListViewActivity找到它,得到它的适配器并更新它,如下所示:

ListView list = (ListView) getActivity().findViewById(R.id.theIdOfTheList);
((BaseAdapter)list.getAdapter()).notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

无论你是否调用notifyDataSetChanged()适配器,ListView都不会更新,因为它没有看到新文件,从它的角度来看数据集是完整的.根据适配器的外观,您有两种选择:

重建数据ListView,基本上重做你在第ListView一次构造时所做的事情:检查该目录并重新列出所有文件.

// create the new file
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/test/");
File list[] = file.listFiles();
ListView list = (ListView) getActivity().findViewById(R.id.theIdOfTheList);
// I'm assuming your adapter extends ArrayAdapter(?!?)
CustomArrayAdapter caa = (CustomArrayAdapter) list.getAdapter();
caa.clear();
for (int i = 0; i < list.length; i++) {
     if (checkExtension(list[i].getName())) {
         RecordedFile q = new RecordedFile();
         q.setTitle(list[i].getName());
         q.setFileSize(readableFileSize(list[i].length()));
         caa.add(q);
     }
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以手动RecordFile为新创建的文件创建对象,并将其添加到现有适配器:

ListView list = (ListView) getActivity().findViewById(R.id.theIdOfTheList);
(CustomArrayAdapter) caa = (CustomArrayAdapter) list.getAdapter();
File newFile = new File("directory" + "/test/theNewFileName.extension");
RecordFile rf = new RecordFile();
rf.setTitle(newFile.getName());
rf.setFileSize(readableFileSize(newFile.length()));
// have a method to return a reference of the data list(or a method
// to add the data directly in the adapter)
List<RecordFile> data = caa.getListData();
data.add(rf);
caa.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

我不知道你的适配器是怎么样的,所以试试我说的,如果它不起作用,请发布适配器的代码.