2011-05-08 41 views
1

我有一個ListView有649個條目。列表中的每個視圖都有兩個LinearLayout,一個ImageView,幾個TextViews和一個CheckBox。我目前有代碼來檢查所有的CheckBoxes並對它們進行計數,還有更多的代碼來確定哪些檢查框會被檢查,以便我可以使用它們。但是,無論何時我檢查其中一個CheckBox,它上下的每個CheckBox都會被奇蹟般地檢查。計算檢查框的方法返回實際檢查的數字,但獲取所有選中框的索引的方法只是返回檢查的第一個x,無論我檢查它們還是離開它們7。例如,如果我支票號碼21,則該方法返回索引3.我已經包括一些相關的代碼段:使用ListView中的CheckBox檢查每個第七個方框

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    android:id="@+id/id" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:textSize="20sp"/> 

<LinearLayout 
    android:layout_width="180dp" 
    android:layout_height="50dp" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/name"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textColor="#777777" 
     android:id="@+id/type"/> 

</LinearLayout> 
<CheckBox 
    android:layout_gravity="right|center_horizontal" 
    android:id="@+id/check" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 
: 爲列表中的每個視圖佈局代碼

檢查計數方法:

public int[] whichAreChecked() //TODO: this should work. 
{ 
    int listItemCount = ChooserList.getChildCount(); 
    ArrayList<Integer> ints=new ArrayList<Integer>(); 
    for(int i=0; i<listItemCount; i++) 
    { 
     CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check); 
     if(cbox.isChecked()) 
      ints.add(i); 
    } 
    Log.e("durr", ""+ints.size()); 
    int[] result=new int[ints.size()]; 
    for(int i=0; i<result.length; i++) 
     result[i]=ints.get(i); 
    return result; 
} 

檢查計數方法:

public int countChecks() 
{ 
    int checked=0; 
    int listItemCount = ChooserList.getChildCount(); 
    for(int i=0; i<listItemCount; i++) 
    { 
     CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check); 
     if(cbox.isChecked()) 
      checked++; 
    } 
    Log.e("countChecks()", ""+checked); 
    return checked; 
} 

如果我可以添加任何額外的信息,請諮詢!

回答

1

嗯,我想你錯誤地理解了getChildCount()getChildAt()方法的作用。那些實際上只會獲得呈現的視圖。除非我在這裏誤讀了一些東西,如果你有650行,你不能完成你想要渲染的視圖。相反,你應該實現一個onItemClickListener(或者,也可以選擇點擊複選框時觸發的內容),並在複選框被選中後,將該狀態保存到模型中。

+0

感謝您的回覆!我爲自定義適配器中的每個CheckBox添加了一個'onCheckChangedListener',並且將它的索引添加到活動中的靜態ArrayList ,並更新了相應的讀取方法。然而,每當我檢查一個盒子時,每七個盒子都會被檢查,這是造成這個問題的原因。 – 2011-05-08 15:00:03

+0

啊,修好了!我的適配器並不總是在應該創建新的視圖時。 '這一切都修好了!謝謝! – 2011-05-08 15:14:34

+0

你能否詳細說明它的固定方式。 – m4n07 2011-06-20 04:44:22

1

我也面臨着問題,類似的國王,所以大量的閱讀後,我解決了這個問題是這樣的:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.listview, null); 
      holder = new ViewHolder(); 
      holder.nameView = (TextView)convertView.findViewById(R.id.textView1); 
      holder.numberView = (TextView)convertView.findViewById(R.id.textView2); 
      holder.cb = (CheckBox)convertView.findViewById(R.id.checkBox1); 
      convertView.setTag(holder);     
     } else { 
      holder = (ViewHolder)convertView.getTag(); 
     } 
     holder.nameView.setText(mData.get(position).toString()); 
     holder.numberView.setText(mNumber.get(position).toString()); 
     holder.cb.setChecked(false); 
     holder.cb.setTag(position); 



     if(selected.indexOf(mNumber.get(position).toString()) >= 0) 
     { 

     holder.cb.setChecked(true); 
     } 

     return convertView; 
    } 

} 

這裏我在做什麼,關於getView()我取消選中所有的複選框,再次手動檢查我需要根據其對應的文本視圖進行檢查。因此,如果用戶在檢查第一個複選框後向下滾動,則視圖中的所有複選框都將被取消選中,如果他再次滾動,則所有複選框都將被取消選中,但之前單擊的複選框將被再次複查。

相關問題