如何显示音频文件在Android的列表视图? [英] How to show audio files in a listview in Android?
本文介绍了如何显示音频文件在Android的列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在对 SD卡特定目录中的一些音频文件。我需要表现出他们的名字在Android的列表视图,如果我要玩或删除特定文件,我可以做到这一点通过列表视图?我该怎么办呢?
公共类MyPerformanceArrayAdapter扩展ArrayAdapter<字符串> {
私人最终上下文的背景下;
私人最终的String []值;
公共MyPerformanceArrayAdapter(上下文的背景下,的String []值){
超(背景下,R.layout.main,价值观);
this.context =背景;
this.values =值;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
LayoutInflater充气=(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看rowView = inflater.inflate(R.layout.main,父母,假);
TextView中的TextView =(TextView中)rowView.findViewById(R.id.label);
ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.icon);
textView.setText(值[位置]);
//更改图标的Windows和iPhone
字符串s =数值[立场]
如果(s.startsWith(Windows7的)|| s.startsWith(iPhone)
|| s.startsWith(中的Solaris)){
//imageView.setImageResource(R.drawable.no);
} 其他 {
//imageView.setImageResource(R.drawable.ok);
}
返回rowView;
}
}
公共类SimpleListActivity扩展ListActivity {
公共无效的onCreate(包冰柱){
super.onCreate(冰柱);
的String []值=新的String [] {机器人,iPhone,WindowsMobile的,
黑莓,WebOS的,Ubuntu的,Windows7的,最大OS X,
Linux的,OS / 2};
MyPerformanceArrayAdapter适配器=新MyPerformanceArrayAdapter(这一点,值);
setListAdapter(适配器);
}
}
这code是一个简单的列表视图。我需要从到适配器类特定的目录获取他们的名字。我该怎么办呢?
如果我做任何更改到特定的文件,这些都应该反映在列表视图。
解决方案
公共类ReadAllFilesFromPathActivity延伸活动{
/ **第一次创建活动时调用。 * /
私人列表<字符串> myList中;
档案文件;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
ListView控件的ListView =(ListView控件)findViewById(R.id.mylist);
myList上=新的ArrayList<字符串>();
文件目录= Environment.getExternalStorageDirectory();
文件=新的文件(目录+/测试);
文件列表[] = file.listFiles();
的for(int i = 0; I< list.length;我++)
{
myList.add(名单[I] .getName());
}
ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,
android.R.layout.simple_list_item_1,android.R.id.text1,myList中);
listView.setAdapter(适配器); //在列表中设置的所有文件。
}
}
I have some audio files in a particular directory on the SD card. I need to show their names in a listview in Android, and if I want to play or delete that particular file, can I do this through that list view? How can I do it?
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyPerformanceArrayAdapter(Context context, String[] values) {
super(context, R.layout.main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
//imageView.setImageResource(R.drawable.no);
} else {
//imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}
public class SimpleListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, values);
setListAdapter(adapter);
}
}
This code is a simple listview. I need to fetch their names from a particular directory to adapter class. How can I do it?
If I do any changes to that particular file, these should be reflected in the listview.
解决方案
public class ReadAllFilesFromPathActivity extends Activity {
/** Called when the activity is first created. */
private List<String> myList;
File file;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Test" );
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, myList);
listView.setAdapter(adapter); //Set all the file in the list.
}
}
这篇关于如何显示音频文件在Android的列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文