• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Creating simple File Browser

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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/songPath">
    </TextView>

</LinearLayout>

and the List View

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>

</LinearLayout>

and have implemented a new FileBrowser Class which has a method the is implemented in my MainActivity Class:

Code:
public class FileBrowser extends ListActivity {

    //second attempt
    void ListDir(File f) {
        List<String> listFiles = new ArrayList<String>();
        File[] files = f.listFiles();
        listFiles.clear();
        for (File file : files) {
            listFiles.add(file.getName());
            TextView SongName = (TextView) findViewById(R.id.songName);
            SongName.setText(file.getName());
        }
        ArrayAdapter<String> dir = new ArrayAdapter<String>(this, R.layout.simple_list_item, listFiles);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(dir);
    }
}

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
 
Last edited:
Back
Top Bottom