我正在创建一个录音应用程序,我已经设法使用我选择的目录中的音频文件填充列表视图,但我也希望得到它们的长度,类型等。 点击它们之后,我希望它能打开一个新活动,其中包含有关该音频文件的信息以及播放它的能力。 附上截图以显示我在说什么。
这是我用来填充列表视图的代码:
ListView listView = (ListView) findViewById(R.id.song_list);
myList = new ArrayList<String>();
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Audio" );
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.
任何帮助将不胜感激,谢谢! http://i62.tinypic.com/rb9zja.png
答案 0 :(得分:0)
您必须拥有自定义适配器。
提前为任何错误道歉;我直接输入了答案,并没有先用IDE证明它。
ListView listView = (ListView) findViewById(R.id.song_list);
myList = new ArrayList<File>(); // note, list of Files, not Strings
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Audio" );
File list[] = file.listFiles();
// you could also use Arrays.asList() instead of a loop
for( int i=0; i< list.length; i++)
{
myList.add( list[i] ); // add the whole File instance to the list
}
MyFileArrayAdapter adapter = new MyFileArrayAdapter(this,
R.layout.my_file_List_item, myList);
listView.setAdapter(adapter); //Set all the file in the list.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.putExtra("file", (Serializable) parent.getAdapter().getItem(position));
// in your target activity, get the file like this:
// File file = (File) getIntent().getSerializableExtra("file");
startActivity(intent, FileDetailActivity.class);
}
});
public static class MyFileArrayAdapter extends ArrayAdapter<File> {
private int mLayoutId;
public MyFileArrayAdapter(Context context, int layoutId, List<File> files) {
super(context, layoutId, files);
mLayoutId = layoutId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(mLayoutId, parent, false);
}
File file = (File) getItem(position);
int fileSize = file.length();
Date fileDateTime = new Date(file.lastModified());
// set up your list item views here and fill in the size and date/time
}
}