【发布时间】:2014-05-18 16:26:44
【问题描述】:
我有一个应用程序可以从存储设备中获取所有 .mp3 文件,我想在创建应用程序时将它们添加到 ListView。我在向列表中添加项目时迷失了方向。我已经用谷歌搜索了它,我无法很好地定义开发人员正在做什么以及他们为什么要添加他们正在添加的内容。我希望有人告诉我如何向 ListView 添加项目,如果可以的话,请解释他们在做什么,以便我理解并从中学习。我是一名新的 Android 开发人员,希望尽可能多地学习,而不仅仅是修复我的代码。
所以,我当前的代码是...
XML
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我不知道在我的主要活动中该做什么。到目前为止,我...
File storageDir = new File("/mnt/extSdCard/");
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
walkdir(storageDir);
}
public void walkdir(File directory) {
TextView songList=(TextView)findViewById(R.id.textView);
String fileType = ".mp3";
File[] listFile = directory.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(fileType)){
//I need to add the items to the ListView right here.
//listFile[i].toString() is the code to get the text, aka what i want to add
}
}
}
} }
【问题讨论】:
-
您只需填写一个适配器并将其分配给 ListView...
标签: java android xml android-listview