2012-06-13 17 views
0

我在每個項目中都有ExpandableListActivityText。 如何獲得點擊的listitem的文本?ExpandableListActivity獲取文本

Here`s我該怎麼辦:

 groupData = application.getFirstLayer(); 
     String groupFrom[] = new String[] {"groupName"}; 
     int groupTo[] = new int[] {android.R.id.text1}; 

     childData = application.getSecondLayer(); 
     String childFrom[] = new String[] {"levelTwoCat"}; 
     int childTo[] = new int[] {android.R.id.text1}; 

     adapter = new SimpleExpandableListAdapter(
      this, 
      groupData, 
      android.R.layout.simple_expandable_list_item_1, 
      groupFrom, 
      groupTo, 
      childData, 
      android.R.layout.simple_list_item_1, 
      childFrom, 
      childTo); 


public boolean onChildClick(android.widget.ExpandableListView parent, 
      View v, int groupPosition, int childPosition, long id) {} 

我有onChildClick寫才能看到當前項目的文本?

+0

考慮組從父數據信息列表中,groupFrom [groupPosition]會給你父母的字符串。 –

回答

1

最簡單的方法是直接從您點擊的視圖中獲取。你還沒有顯示你的行XML,所以下面的代碼將假設你有一個LinearLayout,裏面有一個TextView作爲你的行。

public boolean onChildClick(android.widget.ExpandableListView parent, 
     View v, int groupPosition, int childPosition, long id) { 

      TextView exptv = (TextView)v.findViewById(R.id.yourtextview); // Get the textview holding the text 
      String yourText = exptv.getText().toString(); // Get the text from the view and put it in a string 
      // use string as you need to 
} 

如果佈局是只有一個TextView,你可以直接去String yourText = v.getText().toString();因爲傳遞的將是你所需要的TextView的視圖V。

編輯

正如賈森·羅賓遜在他的評論中指出的那樣,你正在使用android.R.layout.simple_list_item_1爲您的孩子的佈局,因爲這是唯一一個TextView,它可以簡化您需要將代碼:

public boolean onChildClick(android.widget.ExpandableListView parent, 
     View v, int groupPosition, int childPosition, long id) { 

      String yourText = v.getText().toString(); // Get the text from the view and put it in a string 
      // use string as you need to 
} 
+0

他爲他的視圖使用'android.R.layout.simple_list_item_1',所以他需要用來獲取'TextView'的ID是'android.R.id.text1' –