1

我已經構建了以下說明的可搜索ExpandableListView在Android ExpandableListView Search Filter Example但我掙扎,選擇正確的兒童在使用onChildClick()ExpandableListView搜索過濾器

當目前我使用下面的代碼來選擇和發送信息發展到新的意圖。但是,當應用搜索過濾器時,會發送該位置的原始子女的信息,而不是來自過濾列表中的子女的信息。

myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 

      Object a = continentList.get(groupPosition).getCountryList().get(childPosition).getCode(); 
      Object b = continentList.get(groupPosition).getCountryList().get(childPosition).getName(); 
      Object c = continentList.get(groupPosition).getCountryList().get(childPosition).getPopulation(); 

      // Start new intent 
      Intent intent = new Intent(SearchActivity.this, DisplayCountry.class); 
      intent.putExtra("code", ""+ a); 
      intent.putExtra("name", "" + c); 
      intent.putExtra("population", "" + b); 
      startActivity(intent); 

      return true; 
     } 
    }); 

回答

1

我假設單擊偵聽器代碼不在適配器類本身中,而是在活動中?在這種情況下,問題是您正在訪問錯誤的數據集。當想要從適配器中檢索數據時,請始終通過適配器方法。例如:

mAdapter.getChild(groupPosition, childPosition).getCode(); 
mAdapter.getChild(groupPosition, childPosition).getName(); 
mAdapter.getChild(groupPosition, childPosition).getPopulation(); 

這適用於任何適配器。切勿假設外部數據與內部適配器數據相同。

+0

你的假設是正確的!使用適配器方法代替解決了問題。 –