2013-04-08 24 views
17

Tôi đặt arrayList thành 12 mục, tuy nhiên arrayAdapter chỉ trả về một mục trên màn hình, tại sao lại là? Nó được cho là hiển thị 12 mục trong danh sách.arrayAdapter chỉ trả lại một vị trí, 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> 

Trả lời

63

phát hiện ra rằng vấn đề là ListView nằm trong ScrollView trong bố cục xml, khi tôi xóa ScrollView bên ngoài tất cả các mục của ListView xuất hiện.

vì một lý do nếu bạn đóng gói một ListView lồng trong một vài lớp nó sẽ hiển thị chỉ có một mục vị trí nếu ListView trong bên trong một scrollview

+5

Rất, rất, đúng. Và đáng buồn là không có giấy tờ. Cảm ơn. –

+1

Cảm ơn, nó đã bắt đầu khiến tôi phát điên. – Simon

+0

Tìm thấy tuyệt vời. Tài liệu này ở đâu ???? – toobsco42

2

Override getCount trong bạn MyAdapter

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

Điều này thực sự có ý nghĩa như thế không? Tại sao tôi lại bị buộc phải tự mình thực hiện getCount? Tôi đã vượt qua arrayList trong siêu (...) ?? Hơn nữa, tôi có một dự án khác cho Android, mà tôi đã phát triển như 1 năm trước ... Và sau đó không cần phải ghi đè ... – Ted

+1

... cũng, ghi đè phương pháp đó không thay đổi điều gì cho tôi. – Ted

1

Hãy thể hiện quy tắc ứng row_layout, tôi nghĩ lỗi của bạn là for loop:

Cố gắng tạo ra 2 hộp kiểm và thêm hướng dẫn sử dụng cho tes 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

Tôi có vấn đề này vì notifyDataSetChanged() tôi là bên trong một vòng lặp và không bị được gọi trong một số trường hợp nhất định (nếu không có các mục dữ liệu).

Đảm bảo notifyDataSetChanged() được gọi khi cần!

0

android: fillViewport = "true" trên ScrollView sẽ giải quyết được sự cố.

Các vấn đề liên quan