Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use an ArrayAdapter to return and show a ListView
    text
    copied!<p>I am writing code for an mp3 player I am trying to create. I am only at the start of the project and want to be able to read and show all of the mp3 files that is on my sd card. I don't want to use the direct path method. Now this code I have written gathers all of the mp3 files for me but the only problem is that it does not view them on screen for me. The app shows a blank screen, but doesn't crash.</p> <p>I got help and advice off of a tutor and I was told to use an ArrayAdapter to view the results but I can't find any help to show it. If anyone could please help that would be great.</p> <p>Here is my code in the onCreate method;</p> <pre><code>ListView list; Cursor cursor; int columnIndex; int count; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //list = (ListView) findViewById(R.id.list); //A array is created that display the 3 fields String[] displayMusic = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE}; //the cursor displays all of the audio in the sd card, just to limit to .mp3 files now cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, displayMusic, null, null, null); //this is the loop that gather all of the .mp3 files int pos = 1; ArrayList&lt;String&gt; listOfMp3s = new ArrayList&lt;String&gt;(); while (cursor.moveToNext()) { if(cursor.getString(pos).endsWith("mp3")) { listOfMp3s.add(cursor.getString(pos)); } } String[] displayFields = new String[] {MediaStore.Audio.Media.DISPLAY_NAME}; int[] displayViews = new int[] {android.R.id.text1}; //setListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, displayFields, displayViews); ArrayAdapter&lt;String&gt; musicAdapter = new ArrayAdapter&lt;String&gt;(getBaseContext(), R.layout.list_songs, listOfMp3s); //list.setAdapter(listOfMp3s); } </code></pre>
 

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