2012-07-18 76 views
8

我onChildClick方法對我的應用程序,這是我的代碼如何從ExpandableListView中的子項中獲取項目?

public class listview extends ExpandableListActivity { 

private static final String NAME = "NAME"; 
private static final String IS_EVEN = "IS_EVEN"; 
private ExpandableListAdapter mAdapter; 
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>(); 
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>(); 
InputStream is; 
boolean addState = true; 
boolean addCountry; 
HashMap<String, String> countryMap = new HashMap<String, String>(); 
HashMap<String, String> stateMap = new HashMap<String, String>(); 
int count = 0; 
int count1 = -1; 
int count2 = 0; 
List<String> stateCount = new ArrayList<String>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // Reading data from text file to inout stream 
    is = this.getResources().openRawResource(R.raw.countrystate); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    try { 
    String line = ""; 
    while ((line = reader.readLine()) != null) { 
    String[] strings = line.split("-"); 
    String ctry = strings[0].trim(); 
    String st = strings[1].trim(); 
    /* 
    * loading country and state to two separate hash tables 
    */ 
    if (countryMap.containsValue(ctry)) { 
    addCountry = false; 
    if (!addState) { 
     stateMap.put("state" + count1 + count2, st); 
     count2++; 
    } 
    } else { 
    addCountry = !addCountry; 
    count1++; 
    //adding no. of states for a country to a list 
    stateCount.add("" + count2); 
    if (addCountry) { 
     count2 = 0; 
     countryMap.put("country" + count, ctry); 
     stateMap.put("state" + count1 + count2, st); 
     addState = false; 
    } 
    count2++; 
    count++; 
    } 
    } 
    } catch (Exception e) { 
    } 
    //re-arranging the state count list 
    stateCount.add("" + count2); 
    stateCount.remove(0); 
    for (int i = 0; i < countryMap.size(); i++) { 
    Map<String, String> curGroupMap = new HashMap<String, String>(); 
    groupData.add(curGroupMap); 
    String ctry = countryMap.get("country" + i); 
    curGroupMap.put(NAME, ctry); 
    curGroupMap.put(IS_EVEN, "Country " + i); 
    List<Map<String, String>> children = new ArrayList<Map<String, String>>(); 
    int k = Integer.parseInt(stateCount.get(i)); 
    for (int j = 0; j < k; j++) { 
    Map<String, String> curChildMap = new HashMap<String, String>(); 
    children.add(curChildMap); 
    curChildMap.put(NAME, stateMap.get("state" + i + j)); 
    // curChildMap.put(IS_EVEN, "State " + j); 
    } 
    childData.add(children); 
    } 



    // Set up our adapter 
    mAdapter = new SimpleExpandableListAdapter(this, groupData, 
    android.R.layout.simple_expandable_list_item_1, new String[] { 
     NAME, IS_EVEN }, new int[] { android.R.id.text1, 
     android.R.id.text2 }, childData, 
    android.R.layout.simple_expandable_list_item_2, new String[] { 
     NAME, IS_EVEN }, new int[] { android.R.id.text1, 
     android.R.id.text2 }); 
    setListAdapter(mAdapter); 


    } 
    public boolean onChildClick (ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
     System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition+v.toString()); 

     return true; 

} 

    public List<List<Map<String, String>>> getChildData() { 
    return childData; 
} 

} 

我的onClick方法

public boolean onChildClick (ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
     System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition+v.toString()); 

     return true; 

} 

我想要得到的字符串,如果我點擊列表中的孩子

這是我的logcat:

07-18 11:40:54.334: INFO/System.out(1014): Inside onChildClick at groupPosition = 0 Child clicked at position [email protected] 
07-18 11:40:56.714: DEBUG/dalvikvm(916): GC_EXPLICIT freed 14K, 53% free 2557K/5379K, external 884K/1038K, paused 111ms 
07-18 11:40:58.744: INFO/System.out(1014): Inside onChildClick at groupPosition = 0 Child clicked at position [email protected] 

我想獲取字符串數據n ot「TwoLineListItem @ 405337c0」,任何人都可以幫助我? 感謝

+0

顯示短的和錯誤的部分代碼的。 – hasanghaforian 2012-07-18 04:53:50

回答

17

用途:

parent.getExpandableListAdapter().getChild(groupPosition, childPosition); 

(如果你想有一個孩子的數據,或者.getGroup(groupPosition)如果你想從該組數據)視圖,而不是:

v.toString() 
+0

感謝@Luksprog,但結果是{NAME = Colombo},我想要得到String「Colombo」,你能幫我嗎? – idham 2012-07-18 05:06:25

+0

你節省了我的一天。我將它投影到一個'Cursor'上,現在我很開心(我用'CursorAdapter'支持'ExpandableListView') – 2016-01-22 02:14:05

4

試試這個

public boolean onChildClick (ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { 
     System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childData.get(groupposition).get(childPosition).get("NAME")); 

     return true; 

} 
+1

非常感謝@Sanket,很努力:) – idham 2012-07-18 05:38:17

2

由於某種原因上述答案沒有爲我工作,但我找到了解決方案h ERE下面

@Override 
    public boolean onChildClick(ExpandableListView parent, View v, 
      int groupPosition, int childPosition, long id) { 
     // TODO Auto-generated method stub 

     String message; 
     TextView tx = (TextView) v; 
     message=tx.getText().toString(); 
     return false; 
    } 

通過這個代碼,你會得到ExpandableListView孩子的文字點擊

+0

工程很好!謝謝! – Sharath 2016-01-10 11:23:16

相關問題