Reputation: 53
I'm creating an audio recording application, I've managed to populate a listview with the audio files from my chosen directory but I'd also like to get their length, type etc. Also upon clicking on them I would like it to open a new activity with information about that audio file and the ability to play it. Attached screenshot to show what I'm talking about.
This is the code I'm using to populate the listview:
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.
Any help would be appreciated, thank you! http://i62.tinypic.com/rb9zja.png
Upvotes: 1
Views: 1044
Reputation: 30985
You must have a custom adapter.
Apologies in advance for any errors; I typed this straight into the answer and didn't proof it with an IDE first.
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
}
}
Upvotes: 1