2013-11-23 48 views
0

我有一個佈局被用作視圖的模板。這個視圖正在多次產生,我想訪問一些複選框的值,它們是視圖的一部分。獲取視圖內的複選框狀態

這是我的XML模板:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/motor_block" 
android:layout_width="wrap_content" 
android:layout_height="30dp" 
android:orientation="horizontal" 
android:background="@drawable/motor_shape"> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|left" 
    android:layout_margin="5dp" 
    android:text="Motor" /> 

<CheckBox 
    android:id="@+id/checkBox1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:tag="A" 
    android:text="A" /> 
    <CheckBox 
    android:id="@+id/checkBox2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:tag="B" 
    android:text="B" /> 
     <CheckBox 
    android:id="@+id/checkBox3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:tag="C" 
    android:text="C" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="40dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:ems="10" 
    android:inputType="number" 
    android:maxLength="2" > 

    <requestFocus /> 
</EditText> 

我不能讓狀態做

CheckBox A = (CheckBox)findViewById(R.id.checkBox1); 

,因爲它會得到這是催生了第一個視圖的複選框的狀態。

如何獲取狀態的任何想法?

編輯

我定義它:

HashMap boxes = new HashMap(); 

然後我說的複選框的觀點作爲重點:

boxes.put(iv,(CheckBox)findViewById(R.id.checkBox1)); 

但是,當我這樣做:

CheckBox A = boxes.get(v); 

我得到這個:類型不匹配:不能從對象轉換爲複選框

回答

0

HashMap應與新代碼中的泛型一起使用。它的設計是與Java是不支持泛型的版本向後兼容的,但是這是你應該怎樣做一個新的(這有解決您的鑄鋼問題的好處):

HashMap<Integer, CheckBox> map = new HashMap<Integer, CheckBox>(); 
map.put(1, (CheckBox)findViewById(R.id.checkBox1)); 
... 
CheckBox a = map.get(1); 

你可以還需要輸入String或其他Object

請注意@Ascorbin如何在他們的建議中使用泛型創建列表。

編輯

爲了解決您的評論:

首先,不要讓每一個複選框一個單獨的地圖;做一個圖來加以描述:

HashMap<Integer, CheckBox> map = new HashMap<Integer, CheckBox>(); 
map.put(1, (CheckBox)findViewById(R.id.checkBox1)); 
map.put(2, (CheckBox)findViewById(R.id.checkBox2)); 
... 
CheckBox a = map.get(1); 
CheckBox b = map.get(2); 

爲了應對這樣的觀點被多次實例化,你有兩個選擇:

  1. 讓自己的自定義View子類,它的內部保存這些地圖之一併保留對您創建的視圖的每個實例的引用。然後,當你需要綁到一個特定的查看特定複選框,你可以做一些這樣的:

    CheckBox a = myView.getCheckBoxMap().get(1); 
    
  2. (更自足,但糟糕的設計IMO)在Activity創建一個醜陋的數據結構像下面這樣:

    HashMap<View, HashMap<Integer, CheckBox>> mainMap = new HashMap<View, HashMap<Integer, CheckBox>>(); 
    

    然後當你實例化的觀點:

    View view1 = createFirstView(); 
    CheckBox a = (CheckBox)view1.findViewById(R.id.checkBox1); 
    CheckBox b = (CheckBox)view1.findViewById(R.id.checkBox2); 
    HashMap<Integer, CheckBox> view1Map = new HashMap<Integer, CheckBox>(); 
    view1Map.put(1, a); 
    view1Map.put(2, b); 
    mainMap.put(view1, view1Map); 
    // rinse and repeat for every instantiated view, changing the variables each time - ie, view2/view2Map, etc. 
    
+0

我使用了HashMap,我知道它對每個盒子都使用獨立的地圖,但所有的複選框都對應於產生的第一個視圖,任何其他解決方案? – Sochimickox

+0

檢查編輯到我的文章;結果發表太長的評論。 – Josh

0

讓自己包含所有的複選框,然後你可以查看/稍後取消它們的引用一個ArrayList或者HashMap中。

ArrayList<CheckBox> boxes = new ArrayList<CheckBox>(); 

每當你創建你的佈局視圖你做

CheckBox A = (CheckBox)findViewById(R.id.checkBox1); 
boxes.add(A); 

然後,您可以稍後做

boxes.get(5).setChecked(true); 

你一定要想到,你在得到正確的索引,或者你可以使用HashMap並使用鍵。

+0

你能給我一個示例代碼嗎? – Sochimickox

+0

我試過使用HashMap,但它不工作,你能舉一個例子嗎? – Sochimickox

+0

你如何發佈你已經嘗試過,爲什麼/什麼不工作。 – FWeigl

0

試試這個:

CheckBox A = (CheckBox)findViewById(R.id.checkBox1); 
. 
. 
. 
if(boxes.get(v).isChecked()){ 
    //do something 
} 
else{ 
    //do another thing 
} 
相關問題