使用资产文件夹中的项目名称填充Android ListView
我正在构建一个基于别人代码的媒体播放器。 媒体播放器播放资产文件夹中的mp3。 我已将ListView添加到media player layout.xml中,并希望用资产文件夹的内容填充此列表。 目前,主java中有一些代码采用mp3名称,并将其显示在弹出窗口中。如何将这些信息加载到列表中。此外,列表中的ietm必须与曲目播放相对应 下面是java代码使用资产文件夹中的项目名称填充Android ListView,android,android-listview,assets,Android,Android Listview,Assets,我正在构建一个基于别人代码的媒体播放器。 媒体播放器播放资产文件夹中的mp3。 我已将ListView添加到media player layout.xml中,并希望用资产文件夹的内容填充此列表。 目前,主java中有一些代码采用mp3名称,并将其显示在弹出窗口中。如何将这些信息加载到列表中。此外,列表中的ietm必须与曲目播放相对应 下面是java代码 //Generate a String Array that represents all of the files found privat
//Generate a String Array that represents all of the files found
private String[] getTracks(){
if(type == 0){
try {
String[] temp = getAssets().list("");
return temp;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
} else if(type == 1){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)){
path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
path2 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String[] temp = path.list();
return temp;
} else{
Toast.makeText(getBaseContext(), "SD Card is either mounted elsewhere or is unusable", Toast.LENGTH_LONG).show();
}
}
return null;
}
//Adds the playable files to the trackNames List
private void addTracks(String[] temp){
if(temp != null){
for(int i = 0; i < temp.length; i++){
//Only accept files that have one of the extensions in the EXTENSIONS array
if(trackChecker(temp[i])){
trackNames.add(temp[i]);
trackArtworks.add(temp[i].substring(0, temp[i].length()-4));
}
}
Toast.makeText(getBaseContext(), "Loaded " + Integer.toString(trackNames.size()) + " Tracks", Toast.LENGTH_SHORT).show();
}
}
//Plays the Track
private void playTrack(){
if(isTuning && track != null){
track.play();
Toast.makeText(getBaseContext(), "Playing " + trackNames.get(currentTrack).substring(0, trackNames.get(currentTrack).length()-4), Toast.LENGTH_SHORT).show();
}
}
还有我的xml
<ListView
android:id="@+id/trackNames"
android:layout_width="match_parent"
android:layout_height="98dp"
android:layout_weight="0.74" >
</ListView>
如何填充列表?如何使用我已有的代码来执行此操作?您需要创建ArrayAdapter或ArrayAdapter的子类,将数据添加到其中,并在ListView中进行设置
这是一个这是一个SimpleAdapter版本…非常类似于ArrayAdapter
HashMap<String,String> item;
for (int i=0;i<count; i++){
item = new HashMap<String,String>();
item.put( "line1", trackNames.get(i));
list.add( item );
}
sa = new SimpleAdapter(getActivity(),
list, android.R.layout.simple_list_item_activated_1, new String[] { "line1" }, new int[] {android.R.id.text1});
setListAdapter( sa );
嗨,是的,我今天搜索了在线教程。我希望这里有人能给我解释一下。因为我喜欢讨论,而不是像整个下午一样阅读静态教程。讨论有助于我学习此主题。@Will StackOverflow是一个问答网站,不是在线讨论。阅读常见问题解答。是的,问答是双向对话=讨论。因此,让我们继续回答这个问题,帮助我理解它,而不是这个。这真的是一个令人厌烦的互联网。你的链接不再工作了。谢谢你的评论。这是使用我提供的部分代码,还是从新代码开始?