JSNinja
JSNinja

Reputation: 715

Unable to populate ListView with ArrayList

I'm trying to make an audio recorder for which I want to display a list of recordings done till now.

I'm able to get the list recorded files from my SD card in to an ArrayList, but my app crashes when it tries to populate the list view.

ListRecordings.java is called using an Intent from MainActivity.java.

Here is the for ListRecordings.java:

public class ListRecordings extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.listrecordings);
    ListView lv;
    String path = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/MyAudioRecorder";

    ArrayList<String> FilesInFolder = GetFiles(path);

    lv = (ListView) findViewById(R.id.filelist);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.textlayout,
            FilesInFolder); 

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // Clicking on items
        }
    });
}



public ArrayList<String> GetFiles(String DirectoryPath) {
    ArrayList<String> MyFiles = new ArrayList<String>();
    File f = new File(DirectoryPath);

    f.mkdirs();
    File[] files = f.listFiles();

    if (files.length == 0)
        return null;
    else {
        for (int i = 0; i < files.length; i++)
            MyFiles.add(files[i].getName());
    }

    return MyFiles;
}
}

Here's the code for listrecordings.xml

<?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"
    android:orientation="vertical" >

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

</LinearLayout>

Here's the code for textlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp"
    android:textStyle="bold" >
</TextView>

Any help is appreciated.

I've just started with android so excuse me if i made any lame mistakes.

Thanks

Upvotes: 1

Views: 133

Answers (2)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

You missing this line in onCreate()

super.onCreate(savedInstanceState);

Upvotes: 1

Mayur Raval
Mayur Raval

Reputation: 3275

Add something like this

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.textlayout, R.id.textview_id_in_textlayout,FilesInFolder);

I hope this will help.

Upvotes: 0

Related Questions