0

我使用ExpandableListActivityExpandListAdapter組合。ExpandableListView每行有2個項目,獲取子值

我的佈局:

--------------------------------- 
| <TextView>     | 
|        | 
|-------------------------------| 
| <ExpandableList>   | | 
| group1      | | 
| child1: textView editText | | 
| child2: textView switch | | 
| group2      | | 
| child1: textView textView | | 
|-------------------------------| 
| <footerView>     | 
| [button]      | 
--------------------------------- 

這裏是我的child_layout(去除不必要的部分):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/tvChild" 
     android:layout_width="400px" 
     android:layout_height="wrap_content" 
     android:focusable="false" /> 

    <EditText 
     android:id="@+id/etChild" 
     android:layout_width="400px" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/tvChild2" 
     android:layout_width="600px" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <Switch 
     android:id="@+id/switchChild" 
     android:layout_width="400px" 
     android:layout_height="wrap_content" 
     android:textOff="NO" 
     android:textOn="YES" 
     android:visibility="gone" /> 
</LinearLayout> 

這是我怎麼添加按鈕,開關或TextView的每個孩子:

Switch swc = (Switch) view.findViewById(R.id.switchChild); 
    EditText etc = (EditText) view.findViewById(R.id.etChild); 
    TextView tvc = (TextView) view.findViewById(R.id.tvChild2); 

    if (groupPosition == 0) { 
     swc.setVisibility(View.GONE); 
     tvc.setVisibility(View.GONE); 
     etc.setVisibility(View.GONE); 
     if (childPosition == 0) 
      etc.setVisibility(View.VISIBLE); 
     if (childPosition == 1) 
      etc.setVisibility(View.VISIBLE); 
     if (childPosition == 2) { 
      tvc.setVisibility(View.VISIBLE); 
      tvc.setText(GlobalVariables.currentSelectedCar); 
     } 
    } 

因此,每個孩子都有一個TextView whi ch不會改變,加上用戶可編輯的EditText/TextView/Switch之一。當我點擊底部的按鈕時,我想要獲得每個孩子的可編輯項目值。

+0

有U實現了這個事情嗎?我正在尋找相同的,但得到問題,從兒童視角檢索數據 – 2013-06-13 05:42:23

+0

如果您仍然有問題,請聯繫我 – wervdon 2014-06-17 10:31:16

回答

2

我相信你可以用類似的方式處理這個問題,以便你能得到包含在常規ListView的一個項目中的信息。 ExpandableListView內置了方法,可以讓您找出哪個項目被點擊。一旦您知道列表中的哪個項目被點擊了,您可以簡單地從與該項目關聯的單個視圖中提取值。我會做以下幾點:

(1)setOnGroupClickListener爲您的ExpandableListViewonGroupClick聽衆可以獲得(除其他之外)與列表中的位置一起選擇的視圖。 (2)一旦你知道列表中的哪個項目被選中,你應該能夠以兩種方式之一提取數據。如果您正在查找的數據已修復,請使用位置指示器從支持列表的適配器中檢索該數據。如果您正在查找所選項目中的視圖 - 聽起來像是您 - 在返回的視圖上使用findViewById。例如 - 作爲onGroupClick方法提供視圖V作爲參數

String input = v.findViewById(R.id.etChild).getText().toString(); 

調用由onGroupClick提供應該讓你到該視圖爲您提供的ID的子視圖中查找該視圖findViewById。這樣你就可以從每個列表項中的輸入字段中提取數據。

+0

感謝您的回覆。我花了很多時間用'ExpandableListActivity'和'ExpandListAdapter',看起來你的建議是正確的。 – wervdon 2013-03-30 15:16:47

-1

想法來自#Rarw

TextView input = (TextView) v.findViewById(R.id.child_item_elv); 
Toast.makeText(getActivity().getApplicationContext(), "child clicked : " + input.getText() , Toast.LENGTH_SHORT).show(); 

這一個工程,裏面的OnGroupListener

相關問題