简体   繁体   中英

ListView only printing first value from ArrayList

I've tried searching other questions, and most of them are to do with having the listview inside a scrollview, but I have no scrollview so it wasn't of much help. I'm sure it's something small I'm missing but I can't seem to pinpoint it. The below is supposed to print out the 3 animalNames to the listview, but is only printing the first.

JSONArray mainNode = new JSONArray(loadJSONFromAsset()); // call the connection to json
        ArrayList<String> animalsArray = new ArrayList<String>();

        if(mainNode != null) //puts the values into an array
        {
            for(int i=0;i<mainNode.length();i++)
            {
                JSONObject eachObject = mainNode.getJSONObject(i);
                animalsArray.add(eachObject.getString("animalName"));
            }

            //prints array to check
            for(String stuff : animalsArray)
            {
                Log.i("name", stuff);
            }

            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, animalsArray);
            animalsList = (ListView) findViewById(R.id.animalList);
            animalsList.setAdapter(arrayAdapter);
        }

This prints out the 3 values within my JSON to logcat, so I'm sure it's getting the values, I'm just unsure as to why it's only printing the first position into the list.

In case it's of any use, my current layout is below.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/animalList"/>

Any ideas as to why it's only printing the first would be greatly appreciated.

Thanks.

It's because the height of your linear layout correspond to the height of one listView item :

android:layout_height="?android:attr/listPreferredItemHeight"

Set the height with other value (like match_parent if you want full screen height)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM