2013-04-08 4 views
17

मैंने सरणी सूची को 12 आइटम पर सेट किया है, हालांकि सरणी एडाप्टर केवल स्क्रीन पर एक आइटम लौटाता है, यह क्यों है? यह सूची में 12 आइटम दिखाना है।सरणी एडाप्टर केवल एक स्थिति लौटा रहा है, एंड्रॉइड

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> 

उत्तर

63

पता चला कि समस्या ListView एक्सएमएल लेआउट में एक scrollview के अंदर था, एक बार मैं बाहरी scrollview सभी ListView के आइटम दिखाया हटा दिया गया था।

किसी कारण से

यदि आप एक ListView कई परतों में नेस्ट संपुटित यह केवल एक ही स्थिति आइटम दिखा देंगे अगर एक scrollview

+5

बहुत, बहुत, सच। और दुख की बात है कि अनियंत्रित। धन्यवाद। –

+1

धन्यवाद, यह मुझे पागल ड्राइव शुरू कर रहा था। – Simon

+0

ग्रेट फाइंड। इस के लिए दस्तावेज कहां है???? – toobsco42

2

अवहेलना getCount अपने MyAdapter

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

क्या यह वास्तव में ऐसा होना है? पृथ्वी पर क्यों मुझे खुद को getCount लागू करने के लिए मजबूर किया जाएगा? मैंने सुपर (...) में सरणी सूची पारित की है ?? इसके अलावा, मेरे पास एंड्रॉइड के लिए एक और प्रोजेक्ट है, जिसे मैंने 1 साल पहले विकसित किया था ... और फिर उस ओवरराइड की कोई आवश्यकता नहीं है ... – Ted

+1

... भी, उस विधि को ओवरराइड करने से मेरे लिए कोई चीज़ नहीं बदली है। – Ted

1

कृपया दिखाने row_layout के कोड में, मुझे लगता है अपने त्रुटि for पाश है:

कोशिश 2 बनाने चेकबॉक्स और टीएस में मैनुअल जोड़ें टी:

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

अंदर में ListView मैं इस समस्या थी क्योंकि मेरे notifyDataSetChanged() एक पाश के अंदर था और नहीं किया जा रहा था कुछ परिस्थितियों में बुलाया जाता है (यदि कोई डेटा आइटम नहीं था)।

सुनिश्चित करें कि notifyDataSetChanged() यह होना चाहिए जब यह होना चाहिए!

0

एंड्रॉइड: स्क्रॉलव्यू पर fillViewport = "true" समस्या को हल करेगा।

संबंधित मुद्दे