我需要帮助找出此代码中的错误。我已经搜索了如何实现ListView OnItemClickListeners,但我无法做到。当我点击listitem时,应用程序崩溃并且媒体播放器无效。
File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Record" );
File list[] = file.listFiles();
for( int i=0; i<5; 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.
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final String res = new String(
Environment.getExternalStorageDirectory() + "/Record/");
playSong(res+myList.get(position));
}
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
});
}
}