lundi 16 mars 2015

How to pass in a filename from a SQLite database in Android?

I am having trouble figuring out a solution to my problem. I have an ExpandableListView that creates a new MediaPlayer instance when the child view is clicked. Currently, this is done with a hard coded link to the file in my raw folder, but I want to switch it to take the file name from the database, and then use that filename to load the corresponding audio file from the raw folder.


My database is set up something like this:



_id|English|Hanzi|Pinyin|Filename

1 |Hello! |你好 |nǐ hǎo|hello


The cursor is already loaded and fills the ExpandableListView. How do I go about loading this instance of mediaPlayer with the filename in the database, so that every child will load it's own audio file?



//load cursor
public void fillData() {
Cursor mGroupsCursor = mydb.fetchColorsGroup();
startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();

//Basic ExpandableListView Adapter
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,
R.layout.group_item, // Group layout
R.layout.child_item, // Child layout
new String[]{"English"}, // Group fields
new int[]{R.id.english_text}, // Widget ids for group data
new String[]{"Hanzi", "Pinyin"}, // Child fields
new int[]{R.id.foreign_text, R.id.romanization}); // Widget ids for child data
final ExpandableListView elv = (ExpandableListView) this.findViewById(R.id.shell_expList);
elv.setAdapter(mAdapter); // Set the list adapter



// Set the listener for child clicks
elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {


// Stop the media player
if (mediaPlayer != null) {
mediaPlayer.release();
}


// Start the media player
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shi);
mediaPlayer.start();
return true;
}
});

Aucun commentaire:

Enregistrer un commentaire