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?