Peter Peter - 1 month ago 12
Android Question

ListView only shows first item

I want to display all items of an

ArrayList<String>
on a
ListView
. I use an
ArrayAdapter<String>
for this. The problem is that only the first item ('foo' in my example) of the
ArrayList
is shown. Can someone tell me why? Am I missing something?

I've made an minimal example using the generated code (Tabbed Action Bar Spinner Activity) from Android Studio 2.

My
FooActivity
:

public class FooActivity extends AppCompatActivity {
...

public static class PlaceholderFragment extends Fragment {

private ListView fooListView;
private ArrayAdapter<String> mAdapter;
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_foo, container, false);

fooListView = (ListView) rootView.findViewById(R.id.foo_list);
updateList();
return rootView;
}

private void updateList() {
ArrayList<String> strings = new ArrayList<>();
strings.add("foo");
strings.add("bar");

if (mAdapter == null) {
mAdapter = new ArrayAdapter<>(getContext(),
R.layout.item_foo,
R.id.foo_string,
strings);
fooListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(strings);
mAdapter.notifyDataSetChanged();
}
}
}


My
fragment_foo.xml
:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.foo.activities.FooActivity$PlaceholderFragment">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/foo_list"
android:layout_below="@+id/total_activities"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>


And the
item_foo.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">
<TextView
android:id="@+id/foo_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some_string"
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>


If it helps I can post the generated
activity_foo.xml
(I didn't made any changes there) and the complete
FooActivity
class too.

Answer

The Problem actually was my activity_foo.xml which had a NestedScrollView. I've added android:fillViewport="true" and now it shows every item.

My NestedScrollView now looks like this:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:fillViewport="true"/>