我正在尝试制作一个录音机,想要显示到目前为止已经完成的录音列表。

我可以将SD卡中记录的文件列表存储到ArrayList中,但是当我尝试填充列表视图时,我的应用程序崩溃了。

使用MainActivity.java中的Intent调用ListRecordings.java。

这是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;
}
}


这是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>


这是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>


任何帮助表示赞赏。

我刚开始使用android,所以如果我犯了任何la脚的错误,请原谅。

谢谢

最佳答案

您在onCreate()中错过了这一行

super.onCreate(savedInstanceState);

关于java - 无法使用ArrayList填充ListView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24948817/

10-11 22:22
查看更多