Avery Vine
Avery Vine

Reputation: 174

Contents of ListView not showing - Android

I am building a ListView in my MainActivity, which contains a list of users. I initialize the ArrayAdapter and ListView using this:

adapter = new ArrayAdapter<String>(this, R.layout.activity_main_component, array);

listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);

I am filling the contents of an ArrayList using the following function:

public void usersUpdated(Message mes) {
    try {
        JSONObject jsonObj = new JSONObject(mes.body.getField(Fields.BODY).toString());
        JSONArray jsonArr = (JSONArray) jsonObj.get("listOfUsers");
        array = new ArrayList<>();
        for (int i = 0; i < jsonArr.length(); i++) {
            System.out.println("Added : " + jsonArr.get(i).toString());
            System.out.println(jsonArr.get(i).toString().getClass().getName());
            array.add(jsonArr.get(i).toString());
        }
        System.out.println(array);
        runOnUiThread(new Runnable() {
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here is the relevant portion of my activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<ListView
    android:layout_width="match_parent"
    android:layout_height="332dp"
    android:id="@+id/list"
    android:layout_weight="0.70" />

</LinearLayout>

And here is my activity_main_component.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>

The ArrayList is being properly filled, as the print statement just before the runOnUiThread in the usersUpdated() function is telling me. However, the list shows up blank. Any ideas on what could be going wrong?

Upvotes: 0

Views: 82

Answers (2)

Pavneet_Singh
Pavneet_Singh

Reputation: 37414

Either rename your TextView id as android:id="@android:id/text1" because the adapter will look for the TextView id as android:id="@android:id/text1" to set data

<TextView xmlns:android="http://schemas.android.com/apk/res/android"      
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip"
    android:textSize="16dip"
    android:textStyle="bold" >
</TextView>

or you can mention TextView id to tell the adapter to look for the TextView

mention your TextView id while creating adapter

ArrayAdapter (Context context,int resource,int textViewResourceId,List<T> objects)

   new ArrayAdapter<String>(this, R.layout.activity_main_component,R.id.label, array);

also follow the advice of @Cricket and @Selvin to complete the answer and use array.clear() instead of creating new ArrayList object

For clarity read Is Java “pass-by-reference” or “pass-by-value”?

Upvotes: 2

OneCricketeer
OneCricketeer

Reputation: 192023

Instead of this

array = new ArrayList<>();

You need to clear the list instead.

array.clear();

Otherwise, the Adapter loses the reference to its list

(you can also add directly to an ArrayAdapter rather than through the list; adapter.add(String) as well as adapter.clear())


And also replace

android:id="@+id/label"

With

android:id="@android:id/text1"

Because this is the id that ArrayAdapter of String looks for by default

Or use this constructor

ArrayAdapter (Context context, 
            int resource,   // set to R.layout.activity_main_component
            int textViewResourceId,  // set to R.id.label 
            List<T> objects)

Upvotes: 2

Related Questions