Update Listview in from other fragment
android, fragment, listview
Solution
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.
Problem
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); ```