Note that there are some explanatory texts on larger screens.

plurals
  1. POSet onClickListener for every single listview element
    text
    copied!<p>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:</p> <pre><code>//filename is a variable for the name of the file, these lines execute everytime I record a new file ArrayList&lt;String&gt; fileNames = new ArrayList&lt;String&gt;(); fileNames.add(filename.toString()); listView = (ListView) findViewById (R.id.mainListView); listAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.simplerow, fileNames); listView.setAdapter(listAdapter); </code></pre> <p>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.</p> <p>The code now:</p> <pre><code>//LISTVIEW fileNames.add(filename.toString()); //adding each filename to ArrayList&lt;String&gt; listView = (ListView) findViewById (R.id.mainListView); listAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.simplerow, fileNames); listView.setAdapter(listAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { Toast toast = Toast.makeText(getApplicationContext(), filename, Toast.LENGTH_SHORT); toast.show(); } }); </code></pre> <p>This is the code that executes each time new file is recorded</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload