1. Are you ready for the Galaxy S20? Here is everything we know so far!

Creating simple File Browser

Discussion in 'Android Development' started by James Mountain, May 9, 2020.

  1. James Mountain

    Thread Starter

    I am trying to create a simple file browser for my mediaplayer because the android standard file browser returns URI media but I want the filepaths in string format of the actual filepath. However, I cannot seem to get the ArrayAdapter to work properly with the List View
    I have created the List item view:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    4.     android:layout_height="match_parent">
    5.  
    6.     <TextView
    7.         android:layout_width="match_parent"
    8.         android:layout_height="wrap_content"
    9.         android:id="@+id/songPath">
    10.     </TextView>
    11.  
    12. </LinearLayout>
    and the List View

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout
    3.     xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    4.     android:layout_height="match_parent">
    5.  
    6.         <ListView
    7.             android:id="@+id/listView"
    8.             android:layout_width="match_parent"
    9.             android:layout_height="match_parent">
    10.         </ListView>
    11.  
    12. </LinearLayout>
    and have implemented a new FileBrowser Class which has a method the is implemented in my MainActivity Class:

    Code (Text):
    1. public class FileBrowser extends ListActivity {
    2.  
    3.     //second attempt
    4.     void ListDir(File f) {
    5.         List<String> listFiles = new ArrayList<String>();
    6.         File[] files = f.listFiles();
    7.         listFiles.clear();
    8.         for (File file : files) {
    9.             listFiles.add(file.getName());
    10.             TextView SongName = (TextView) findViewById(R.id.songName);
    11.             SongName.setText(file.getName());
    12.         }
    13.         ArrayAdapter<String> dir = new ArrayAdapter<String>(this, R.layout.simple_list_item, listFiles);
    14.         ListView listView = (ListView) findViewById(R.id.listView);
    15.         listView.setAdapter(dir);
    16.     }
    17. }
    But in the listView.setAdapter(dir) method It is getting Error: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

    I think it is because the ArrayAdapter is not a valid Adapter for listView.setAdapter(dir)

    Any help I could get on this matter would be greatly appreciated as I have spent over two days trying different configurations and this is the closest I have come to getting it to work properly
     


    #1 James Mountain, May 9, 2020
    Last edited: May 9, 2020

    1. Download the Forums for Android™ app!


      Download

       
  2. markolino

    markolino Newbie

Loading...
Similar Threads - Creating simple File
  1. Viktram Sai Simha
    Replies:
    0
    Views:
    269
  2. July Hoang
    Replies:
    15
    Views:
    637
  3. CCCKP
    Replies:
    0
    Views:
    501
  4. Anmol hira
    Replies:
    1
    Views:
    599
  5. Kuldip Somwanshi
    Replies:
    0
    Views:
    808
  6. Davide Vergnani
    Replies:
    0
    Views:
    718
  7. androinewbi
    Replies:
    6
    Views:
    1,387
  8. ChristopheA
    Replies:
    4
    Views:
    1,197
  9. nullshxt
    Replies:
    3
    Views:
    728
  10. Tony Me
    Replies:
    0
    Views:
    3,684

Share This Page

Loading...