Reputation: 1759
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.
Upvotes: 2
Views: 6967
Reputation: 1229
For others: It's okay to nest a ListView inside another layout, and it's also fine to use android:layout_height="wrap_content"
on Layouts surrounding the ListView. (So long as the outer-most Layout allows for match_parent)
What is not fine, however, is when the TextView being used to populate the ListView is defined with layout_height="match_parent"
. It will cover up all of the subsequent rows after the first.
Bad:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Good:
<TextView
android:id="@+id/infoRow_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Upvotes: 0
Reputation: 4969
Your Listview look something like this
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/foo_list"
android:layout_marginTop="10dp"/>
I have removed from your list view android:layout_below="@+id/total_activities"
and
android:layout_alignParentBottom="true"
Upvotes: 0
Reputation: 1759
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"/>
Upvotes: 12
Reputation: 30995
On any ListView
, if you use android:layout_height="wrap_content"
, you're only going to see one item. Try android:layout_height="match_parent"
. If the ListView
only takes up part of the layout, you'll need a LinearLayout
with weights or a RelativeLayout
with constraints. But wrap_content
won't work for a height on a ListView
, ever.
Upvotes: 5