Search code examples
javaandroidxmlandroid-listview

Add items to ListView Android


I have an app that gets all the .mp3 files off of a storage device and I want to add them to a ListView when the app is created. I am lost on adding items to the list. I have googled it and I cannot a good defintion of what the dev is going and why they are adding what they are adding. I would like someone to tell me how to add items to a ListView and also, if they can, explain what they are doing as they are doing it so I understand and learn from it. I am a new Android Dev and looking to learn as much as I can and not just fix my code.

So, my current code is...

XML

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ListView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

and I do not know what to do on my main activity. I so far have...

File storageDir = new File("/mnt/extSdCard/");

ArrayList<String> listItems=new ArrayList<String>();

ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    walkdir(storageDir);

}

public void walkdir(File directory) {
    TextView songList=(TextView)findViewById(R.id.textView);

    String fileType = ".mp3";

    File[] listFile = directory.listFiles();

    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {

            if (listFile[i].isDirectory()) {
                walkdir(listFile[i]);
            } else {
                if (listFile[i].getName().endsWith(fileType)){

                    //I need to add the items to the ListView right here.
                    //listFile[i].toString() is the code to get the text, aka what i want to add


                }
            }
        }
    }    }

Solution

  • To start with, in your activity, you should extend ListActivity instead of Activity. This will give you access to a method called setListAdapter (there are other ways, but this is probably easiest).

    The setListAdapter method takes an adapter as its parameter, so we now need to create an adapter that we can pass it.

    To do so, write the following:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                                                            android.R.layout.simple_list_item_1,
                                                            listFile);
    

    I'll break that down a little bit:

    • The first part (this) is where we pass it the context.
    • The second part is the layout file that represents each individual list item. To save us having to create one, we are using one that android has built in.
    • Finally, we need to pass it the words you want it to display (in your case, a string array, although it could also be an ArrayList of strings.

    Once you've done that, we just need to pass the adapter to our setListAdapter method, like so

    setListAdapter(adapter);

    But before you run your code, we need to add one little line to the XML file. When you extend ListActivity, Android will be looking for a list whose id is android.R.id.list, so we need to set the id like so:

    <ListView     
        android:id="@android:id/list" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />