2011-11-21 82 views
0

我有一個列表視圖,我從基礎適配器獲取值。我希望點擊按鈕時,其屬性不可見的複選框變得可見並且出現在列表視圖的每一行前面。我能夠去做這件事,但只有幾行前的複選框。我厭倦了搜索這個東西。我覺得問題是在獲取列表視圖的某個地方,它只獲取當時在屏幕上可見的行的視圖。因爲我在這個領域很新,我無法獲得我確切的問題。任何幫助將是非常可觀的。如何把複選框放在listview的每一行中?

///複選框兩行的缺口後出現在四行然後再等..///

 button.setOnClickListener(this); 
      public void onClick(View v) { 
         // TODO Auto-generated method stub 

     //relative layout in which button is declared   
       RelativeLayout relative=(RelativeLayout)v.getParent(); 

     //listview is first child of relative..    
     ListView lv=(ListView)relative.getChildAt(1); 

         for(int i=0;i<lv.getChildCount();i++){ 

    ////////relative layout in whichcheckbox is declared       
    RelativeLayout newrelative=(RelativeLayout)lv.getChildAt(i); 

    //checkbox is forth child of newrelative.. 

    CheckBox cb=(CheckBox)newrelative.getChildAt(4); 
    cb.setVisibility(0); 
         } 

       }//method 
+1

可能重複的[如何添加一個複選框在列表視圖?](http://stackoverflow.com/questions/2538539/how-to-add-a-checkbox-in-a-listview) –

+0

嗨有發現這個任何解決方案..如果是,然後重播我.. –

回答

2

ListView部件回收自己的觀點,這就是爲什麼你看到的變化,當你上下滾動。 要解決它,你應該覆蓋你的列表適配器,並在其中決定是否顯示Checkbox。 這樣做的最好方法可能是將這些數據保存在數據庫中。 那麼getView方法中,你應該添加 -

if (*i should see a checkbox next to this item*) { 
    checkbox.setVisibility(View.VISIBLE); 
} else { 
    checkbox.setVisibility(View.INVISIBLE); 
} 

的「其他」是很重要的,因爲否則會重用可見enter code here複選框與你不想有一個Checkbox的項目。

希望它有幫助。

0

你可以一步教程複選框列表視圖的詳細步驟在.. checkbox listview tutorial

+0

以及我已經閱讀這篇文章,並已完成在教程中描述的方式,但我希望所有這一切點擊按鈕。 –

相關問題