更新列表视图中从其他片段 [英] Update Listview in from other fragment

查看:150
本文介绍了更新列表视图中从其他片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的教程 Manishkpr 创建一个应用程序,你刷卡之间1)layoutOne:在某个文件夹中显示所有创建的文件的列表视图

:在这里您将创建一个文件,2)layoutTwo。

问题:如果你创建了一个文件,它不立即在列表视图显示。我发现,我应该用这个code在我LayoutOne.java:

  LayoutTwo片段=(LayoutTwo)getFragmentManager()findFragmentByTag(TESTTWO)。
            fragment.getAdapter()notifyDataSetChanged()。
 

在LayoutTwo.java我说:

 私有静态最后字符串变量=TESTTWO;

//和功能getAdapter:

公共CustomArrayAdapter getAdapter(){

        返回适配器;
    }
 

不过,我得到的 fragment.getAdapter()一个空指针异常notifyDataSetChanged(); 。我怎样才能解决这个问题,这实际上是最好的方法是什么?

修改

  myList上=新的ArrayList< RecordedFile>();

        文件目录= Environment.getExternalStorageDirectory();
        文件=新的文件(目录+/测试/);

        文件列表[] = file.listFiles();

        的for(int i = 0; I< list.length;我++){
            如果(checkExtension(名单[I] .getName())==真){

                RecordedFile Q =新RecordedFile();
                q.setTitle(名单[I] .getName());
                q.setFileSize(readableFileSize(名单[I] .length()));


                myList.add(q)的;
            }
        }

        适配器=新CustomArrayAdapter(myContext,
                R.layout.listview_item_row,myList中);
        listView.setAdapter(适配器);
 

解决方案
  

我使用的教程Manishkpr创建一个应用程序,你刷卡   1)layoutOne。在这里您将创建一个文件,2)layoutTwo:显示   所有的列表视图在某个文件夹中创建的文件。

     

问题:如果你创建了一个文件,它不立即在所示   列表视图。

如果你只是有两个布局刷卡,这意味着双方将有自己的内存和可用视图访问。然后,您可以分配一个ID给的ListView 时,它的时间来刷新数据简单地看为的ListView 活动,得到它的适配器和更新,是这样的:

 的ListView列表=(ListView控件)getActivity()findViewById(R.id.theIdOfTheList)。
((BaseAdapter)list.getAdapter())notifyDataSetChanged()。
 

不管你叫 notifyDataSetChanged()在ListView将不会更新,因为它没有看到新的文件,从它的角度数据集的适配器上是否完好。根据您的适配器看起来你有两种选择:

重建的ListView的数据,基本上是重做你做了第一次构建了的ListView 时的内容:检查目录中并重新列表中的所有文件。

  //创建新的文件
文件目录= Environment.getExternalStorageDirectory();
文件=新的文件(目录+/测试/);
文件列表[] = file.listFiles();
ListView控件列表=(ListView控件)getActivity()findViewById(R.id.theIdOfTheList)。
//我假设你的适配器扩展ArrayAdapter(?!?)
CustomArrayAdapter CAA =(CustomArrayAdapter)list.getAdapter();
caa.clear();
的for(int i = 0; I< list.length;我++){
     如果(checkExtension(名单[I] .getName())){
         RecordedFile Q =新RecordedFile();
         q.setTitle(名单[I] .getName());
         q.setFileSize(readableFileSize(名单[I] .length()));
         caa.add(q)的;
     }
}
 

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

 的ListView列表=(ListView控件)getActivity()findViewById(R.id.theIdOfTheList)。
(CustomArrayAdapter)CAA =(CustomArrayAdapter)list.getAdapter();
文件NEWFILE =新的文件(目录+/test/theNewFileName.extension);
RecordFile RF =新RecordFile();
rf.setTitle(newFile.getName());
rf.setFileSize(readableFileSize(newFile.length()));
//有一个方法来返回数据列表的参考(或方法
//直接在适配器添加数据)
名单< RecordFile>数据= caa.getListData();
data.add(RF);
caa.notifyDataSetChanged();
 

我不知道该怎么适配器看起来像这样试试我说的话,如果它不能正常工作,请张贴code适配器。

I am using the tutorial on Manishkpr to create a app where you swipe between 1) layoutOne: here you create a file and 2) layoutTwo: shows a listview of all created files in a certain folder.

Problem: if you create a file, it is not immediately shown in the listview. I found that I should use this code in my LayoutOne.java:

   LayoutTwo fragment = (LayoutTwo) getFragmentManager().findFragmentByTag("TESTTWO");
            fragment.getAdapter().notifyDataSetChanged();

In LayoutTwo.java I added:

private static final String TAG = "TESTTWO";

//and the function getAdapter:

public CustomArrayAdapter getAdapter() {

        return adapter;
    }

However, I am getting a nullpointer exception on fragment.getAdapter().notifyDataSetChanged();. How can I solve this, and is this the best way actually?

EDIT

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);

解决方案

I am using the tutorial on Manishkpr to create a app where you swipe between 1) layoutOne: here you create a file and 2) layoutTwo: shows a listview of all created files in a certain folder.

Problem: if you create a file, it is not immediately shown in the listview.

If you just have two layouts to swipe that means both will have their views in memory and available to access. You could then assign an id to the ListView and when it's time to refresh the data simply look for that ListView in the Activity, get its adapter and update it, something like this:

ListView list = (ListView) getActivity().findViewById(R.id.theIdOfTheList);
((BaseAdapter)list.getAdapter()).notifyDataSetChanged();

No matter if you call notifyDataSetChanged() on the adapter the ListView will not update because it doesn't see the new file, from its point of view the data set is intact. Depending on how your adapter looks like you have two options:

Rebuild the data of the ListView, basically redo what you did when the ListView is first constructed: check that directory and re-list all files.

// 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);
     }
}

Or you could manually create a RecordFile object for the newly created file and add that to the existing adapter:

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

I don't know how your adapter looks like so try what I said and if it doesn't work please post the code for the adapter.

这篇关于更新列表视图中从其他片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆