我有一個佈局被用作視圖的模板。這個視圖正在多次產生,我想訪問一些複選框的值,它們是視圖的一部分。獲取視圖內的複選框狀態
這是我的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);
我得到這個:類型不匹配:不能從對象轉換爲複選框
我使用了HashMap,我知道它對每個盒子都使用獨立的地圖,但所有的複選框都對應於產生的第一個視圖,任何其他解決方案? – Sochimickox
檢查編輯到我的文章;結果發表太長的評論。 – Josh