2016-05-17 6 views
1

Ich möchte alle Elemente eines ArrayList<String> auf einem ListView anzeigen. Ich benutze dafür eine ArrayAdapter<String>. Das Problem ist, dass nur das erste Element ('foo' in meinem Beispiel) der ArrayList angezeigt wird. Kann mir jemand sagen warum? Fehle ich etwas?ListView zeigt nur das erste Element

Ich habe ein minimales Beispiel mit dem generierten Code (Tabbed Aktionsleiste Spinner Aktivität) von Android Studio 2.

Mein FooActivity gemacht:

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(); 
      } 
    } 
} 

Mein 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> 

Und die 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> 

Wenn es hilft kann ich die generierte activity_foo.xml (ich habe dort keine Änderungen vorgenommen) und die komplette FooActivity Klasse auch posten.

Antwort

5

Das Problem war eigentlich meine activity_foo.xml, die eine NestedScrollView hatte. Ich habe android:fillViewport="true" hinzugefügt und jetzt zeigt es jedes Element.

Mein NestedScrollView sieht nun wie folgt aus:

<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"/> 
3

Auf jedem ListView, wenn Sie android:layout_height="wrap_content" verwenden, werden Sie nur ein Element sehen. Versuchen Sie android:layout_height="match_parent". Wenn der ListView nur einen Teil des Layouts einnimmt, benötigen Sie einen LinearLayout mit Gewichten oder einen 10 mit Einschränkungen. Aber wrap_content funktioniert nicht für eine Höhe auf einem ListView, überhaupt.

0

Ihre Listview in etwa so aussehen

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/foo_list" 
    android:layout_marginTop="10dp"/> 

ich aus der Liste Ansicht entfernt android:layout_below="@+id/total_activities"

und

android:layout_alignParentBottom="true"