2013-04-08 8 views
17

Ich setze die ArrayList auf 12 Elemente, aber das ArrayAdapter gibt nur ein Element auf dem Bildschirm zurück, warum ist das? Es soll 12 Einträge auf der Liste zeigen.ArrayAdapter gibt nur eine Position zurück, Android

public class MainActivity extends Activity { 

ArrayList<CheckBoxInfo> cfo = new ArrayList<CheckBoxInfo>(); 
CheckBoxInfo cbr; 
private ListAdapter MyAdapter; 
ListView listview; 
MyAdapter myAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    cbr = new CheckBoxInfo(); 
    cbr.checkBoxName = "dfdjklfjdkljf"; 
    cbr.checkBoxState = true; 

     for(int i = 0; i <12; i++){ 
      cfo.add(cbr); 
     } 

     Toast.makeText(MainActivity.this, "size: " + cfo.size(), Toast.LENGTH_SHORT).show(); 

    listview = (ListView) findViewById(R.id.listView); 
    myAdapter = new MyAdapter(cfo, this); 
    listview.setAdapter(myAdapter); 
} 

public class MyAdapter extends ArrayAdapter<CheckBoxInfo> { 

    private List<CheckBoxInfo> checkBoxList; 
    private Context context; 

    public MyAdapter(List<CheckBoxInfo> infoList, Context context) { 
     super(context, R.layout.row_layout, infoList); 
     this.checkBoxList = infoList; 
     this.context = context; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     // First let's verify the convertView is not null 
     if (convertView == null) { 
      // This a new view we inflate the new layout 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.row_layout, parent, false); 
     } 
      // Now we can fill the layout with the right values 
      TextView tv = (TextView) convertView.findViewById(R.id.textView1); 
      CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      CheckBoxInfo cbi = checkBoxList.get(position); 

       Toast.makeText(MainActivity.this, "position: " + position, Toast.LENGTH_SHORT).show(); 


      tv.setText(cbi.checkBoxName); 

     return convertView; 
    } 

} // end MyAdapter 
    } 

row_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=" " /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

Antwort

63

fand heraus, dass das Problem war das Listview innerhalb eines Scroll im xml-Layout war, sobald ich die äußere Scroll alle Elemente des Listview entfernt auftauchten.

aus irgendeinem Grunde, wenn Sie ein Listview verschachtelt in mehreren Schichten kapseln wird es nur eine Position Artikel zeigen, wenn das Listview in innerhalb eines Scroll

+5

Sehr, sehr, wahr. Und leider undokumentiert. Vielen Dank. –

+1

Danke, es begann mich verrückt zu machen. – Simon

+0

Toller Fund. Wo ist die Dokumentation dafür ???? – toobsco42

2

Aufschalten getCount in Ihrem MyAdapter

public int getCount() { 
    return infoList == null ? 0 : infoList.size(); 
} 
+1

Soll das wirklich so sein? Warum um alles in der Welt müsste ich den getCount selbst implementieren? Ich habe die ArrayList im Super (...) ?? Darüber hinaus habe ich ein anderes Projekt für Android, das ich vor 1 Jahr entwickelt habe ... Und dann gibt es keine Notwendigkeit für diese Außerkraftsetzung ... – Ted

+1

... auch das Überschreiben dieser Methode ändert nichts für mich. – Ted

1

Bitte zeigen Code von row_layout, ich glaube, Ihr Fehler ist for Schleife:

Versuchen 2 erstellen Kontrollkästchen und fügen Sie manuell zu Tes hinzu t:

cbr1 = new CheckBoxInfo(); 
cbr1.checkBoxName = "checkbox1"; 
cbr1.checkBoxState = true; 
cfo.add(cbr1); 

cbr2 = new CheckBoxInfo(); 
cbr2.checkBoxName = "checkbox2"; 
cbr2.checkBoxState = true; 
cfo.add(cbr2); 
1

Ich habe dieses Problem, weil meine notifyDataSetChanged() in einer Schleife war und nicht war sein unter bestimmten Umständen (wenn keine Daten vorhanden waren).

Stellen Sie sicher, notifyDataSetChanged() wird aufgerufen, wenn es sein sollte!

0

Android: FillViewport = "True" auf ScrollView wird das Problem lösen.