Reputation: 95
Here's how my app works. User records a sound and then gives it a certain name. In this example I'll be recording two sound files. Test1.mp3 and Test2.mp3. After I'm done recording for the first time, a dialog appears and I type in 'Test1', same goes for the second recording. Test1.mp3 and Test2.mp3 were now added to the listview. This is the code:
//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);
So, after I've recorded both files and they're added to listview, I want to set onClickListeners to both listview elements. But how do I do it? How do I make it so that everytime a new recorded file has been added to the listview, it also automatically generates the onclick method. This wouldn't be as complicated but every recorded file, ofcourse, has a different path.
The code now:
//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();
}
});
This is the code that executes each time new file is recorded
Upvotes: 2
Views: 1764
Reputation: 4092
You don't have to add a new listener every time you add an element to the list.
You can use a OnItemClickListener
set once and for all, and you will be able to find the item that has been clicked by the index in the callback function
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
which is, parameter int position
.
Inside onItemClick implementation, you can then retrieve the element which has been clicked by
arg0.getItemAtPosition(position)
Also, you don't have to add the onItemClickListener every time, you can just prepare it once in the onCreate method of your Activity and never change it.
What you will need to do instead, is making a new adapter (or adding a new item to the adapter) when your new file recording has terminated.
I prepared an almost full sample to show how to use the ListView:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> fileNames = new ArrayList<String>();
fileNames.add("Test1.mp3");
fileNames.add("Test2.mp3");
final ListView listView = (ListView) findViewById (R.id.mainListView);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileNames);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Toast toast = Toast.makeText(getApplicationContext(),
(String)arg0.getItemAtPosition(position), Toast.LENGTH_SHORT);
toast.show();
}
});
}
This is enough if the items in the list do not change.
When a recording of your next mp3 file terminates, and you want to add an item to the list, you may just do:
ListView listView = (ListView) findViewById (R.id.mainListView);
fileNames.add("Test3.mp3");
((ArrayAdapter<String>)listView.getAdapter()).notifyDataSetChanged();
Replace "Test3" with your new recorded mp3's filename, and it will show up in the list.
Upvotes: 4
Reputation:
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
blablbla
break;
case1 1:
blabla
break;
/....
}
}):
Upvotes: 0