Note that there are some explanatory texts on larger screens.

plurals
  1. POFragment with list view is not replacing the list when it should?
    primarykey
    data
    text
    <p>I'm fairly new to Android development so I'm sorry if I haven't given all the information you need. Basically I'm building a basic file browser but I wanted to try something different out by having a list view of all the files in a fragment.</p> <p>Here's what I have so far: </p> <p>In my main activity I have a listView for all the directories which has an onclick event that calls stackAFragment which does this:</p> <pre><code>public class Main extends Activity implements OnItemClickListener { private File RootDir; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /**Initialize the current directory when the main activity is created**/ RootDir = new File("/sdcard/Docs/"); Files_Main.setCurDir(RootDir); ListView l = (ListView) findViewById(R.id.dir_list); ArrayAdapter&lt;String&gt; directories = new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_list_item_1, Files_Main.getFolderList(RootDir)); l.setAdapter(directories); l.setOnItemClickListener(this); stackAFragment(RootDir.toString()); } /** * Add a Fragment to our stack */ private void stackAFragment(String dir) { File myFile = new File(dir); Fragment f = new FilesFragment(); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.files_frag, f); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); ft.addToBackStack(null); ft.commit(); } </code></pre> <p>My FilesFragment doesn't do anything partiularly cleaver, it just gets the current directory that I have saved in an arraylist and gets all the files in that directory and uses that list in the array adapter, like so:</p> <pre><code>@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) { Context c = getActivity().getApplicationContext(); File curDir = Files_Main.getCurDir(); Toast.makeText(getActivity(), (curDir.toString()), Toast.LENGTH_SHORT).show(); /** setup layouts **/ LinearLayout main = new LinearLayout(c); ListView list = new ListView(c); /**Get a list of files from the current directory we are viewing**/ String[] FilesList = Files_Main.getFileList(curDir); /**put list into a array adapter**/ ArrayAdapter&lt;String&gt; files = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, FilesList); /**Set list view to contain items from array adapter**/ list.setAdapter(files); list.setOnItemClickListener(this); /**Attach the list view to the LinearLayout view**/ main.addView(list); /**Return LinearLayout View with list view as the fragment to display**/ return main; } </code></pre> <p>But for some reason when I run this and go into another directory which should re-call the stackAFragment with a different directory, it doesn't replace the fragment which I'm very confused why this doesn't work. I also did this using the linearlayout and using buttons instead of a listview, which worked and replaced the fragment as you go into a new directory. </p> <p>Can anyone help with this? Thanks as I'm confused why this is not working.</p> <hr> <pre><code>public void onItemClick(AdapterView&lt;?&gt; parent, View view, int pos, long id) { File targetDir; ListView l = (ListView) findViewById(R.id.dir_list); if(parent.getItemAtPosition(pos).toString() == "...") { /** * This is going up a directory by deleting the last entry of the directory arraylist. * **/ targetDir = Files.getPreviousDir(); Files.setPreviousDir(); } else { /** * Sets the item clicked on as the target directory we want to browse and adds it * to the directory arraylist. * **/ targetDir = new File (Files.getCurDir() + "/" + parent.getItemAtPosition(pos).toString()); Files.setCurDir(targetDir); } ArrayAdapter&lt;String&gt; directories = new ArrayAdapter&lt;String&gt;(getApplicationContext(), android.R.layout.simple_list_item_1, Files.getFolderList(targetDir)); l.setAdapter(directories); l.setOnItemClickListener(this); stackAFragment(targetDir.toString()); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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