2014-04-16 25 views
0

代碼編程創建了I HV用於創建複選框的選擇如何獲得的值檢查其在安卓

try{ 
for (int i = 0; i < Utstyr.size(); i++) { 
cb = new CheckBox(getApplicationContext()); 
cb.setText(""+Utstyr.get(i)); 
cb.setTextColor(Color.parseColor("#000000")); 
cb.setTag(""+list_sted.get(i)); 
cb.setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle); 
checkbox_lay.addView(cb); 
}} 
catch(Exception e){ 
System.out.println("ohh i got busted...!!!"); 
} 

如何獲得這些被選中的複選框值箱..我想^ h名的複選框

回答

2
CheckBox[] chkArray = new CheckBox[Utstyr.size()];// 
for (int i = 0; i < Utstyr.size(); i++) { 
    chkArray[i] = new CheckBox(getApplicationContext()); 
    chkArray[i].setText(""+Utstyr.get(i)); 
    chkArray[i].setTextColor(Color.parseColor("#000000")); 
    chkArray[i].setTag(""+Utstyr.get(i)); 
    chkArray[i].setTextAppearance(getBaseContext(), android.R.attr.checkboxStyle); 
    checkbox_lay.addView(chkArray[i]); 
} 
for (int k = 0; k < Utstyr.size(); k++){ 
    if(chkArray[k].isChecked()){ 
     //Do something 
    } 
} 

希望這有助於.. :)

+0

看我的編輯。不是說它的確切原因,但它可能是一個原因。 – Rashad

+0

if(chkArray [k] .isChecked()== true)給出java.lang.NullPointerException – Gattsu

+0

@ManishYadav>我可以看到它有多糟糕。 :P可能是你downvoted。順便說一下,我可以知道複選框數組的目的嗎?因爲有一個更好的東西叫做simple_list_item_single_choice。 – Rashad

-1

那麼你需要參考這些複選框。 開創全省複選框的數組,並添加它每次你創造一個,比你可以讓你從他們需要什麼......

+0

我能得到二維碼PLSS? – Gattsu

0

要在生成複選框循環,你不需要設置全局變量。

for (int i = 0; i < Utstyr.size(); i++) { 
cb = new CheckBox(getApplicationContext()); 
...... 
} 

而不是上面的代碼,你必須像下面初始化。

for (int i = 0; i < Utstyr.size(); i++) { 
    CheckBox cb = new CheckBox(getApplicationContext()); 
    ...... 
    } 

以獲得選中的複選框,你必須使用下面的代碼

int childcount = checkbox_lay.getChildCount(); 
for (int i=0; i < childcount; i++){ 
     View v = checkbox_lay.getChildAt(i); 

    if(v instanceof Checkbox){ 

    Checkbox ck=(Checkbox)v; 
     boolean isSelected = ch.isChecked(); 
     } 

}