Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>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.</p> <p>Problem: if you create a file, it is not immediately shown in the listview.</p> </blockquote> <p>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 <code>ListView</code> and when it's time to refresh the data simply look for that <code>ListView</code> in the <code>Activity</code>, get its adapter and update it, something like this:</p> <pre><code>ListView list = (ListView) getActivity().findViewById(R.id.theIdOfTheList); ((BaseAdapter)list.getAdapter()).notifyDataSetChanged(); </code></pre> <p>No matter if you call <code>notifyDataSetChanged()</code> 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:</p> <p>Rebuild the data of the <code>ListView</code>, basically redo what you did when the <code>ListView</code> is first constructed: check that directory and re-list all files.</p> <pre><code>// 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 &lt; 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); } } </code></pre> <p>Or you could manually create a <code>RecordFile</code> object for the newly created file and add that to the existing adapter:</p> <pre><code>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&lt;RecordFile&gt; data = caa.getListData(); data.add(rf); caa.notifyDataSetChanged(); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload