以下是我的应用程序的工作原理。用户录制声音,然后给它一个特定的名称。在这个例子中,我将录制两个声音文件。 Test1.mp3和Test2.mp3。第一次完成录音后,出现一个对话框,我输入'Test1',第二次录音也一样。 Test1.mp3和Test2.mp3现在被添加到列表视图。这是代码:为每个单一列表视图元素设置onClickListener
//filename is a variable for the name of the file, these lines execute everytime I record a new file
ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add(filename.toString());
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
所以,我录的这两个文件,他们正在添加到列表视图后,我想onClickListeners设置为两个列表视图元素。但我该怎么做?我该如何做到这一点,以便每当新的录制文件已被添加到列表视图,它也会自动生成onclick方法。这不会那么复杂,但是每个录制的文件都有不同的路径。
此刻的代码:
//LISTVIEW
fileNames.add(filename.toString()); //adding each filename to ArrayList<String>
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT);
toast.show();
}
});
这是每个新文件记录时执行的代码
你的意思是像['onItemClickListener'](http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html)? – Geobits