2017-06-05 21 views
0

我在擴張列表視圖搜索後有我的onClick問題。當我搜索我得到正確的結果,但是當我單獨點擊他們時敬酒的價值,我得到不同的結果?好心幫搜索列表視圖擴展與onGroupClick&onChildClick顯示錯誤的結果

我的方法來篩選我的數據搜索

public void filterData(String query){ 
    query=query.toLowerCase(); 
    parentRowArrayList.clear(); 

    if(query.isEmpty()){ 
     parentRowArrayList.addAll(originalList); 
    }else{ 
     for(ParentRow parentRow:originalList){ 
      ArrayList<ChildRow> childList = parentRow.getChildList(); 
      ArrayList<ChildRow> newList = new ArrayList<>(); 

      for(ChildRow childRow:childList){ 
       if(childRow.getText().toLowerCase().contains(query)){ 
        Toast.makeText(context, childRow.getText().toString(), Toast.LENGTH_SHORT).show(); 
        newList.add(childRow); 
       } 
      }//end for ChildRow childRow:childList 

      if(newList.size()>0){ 
       ParentRow newParentRow = new ParentRow(parentRow.getName(),newList); 

       parentRowArrayList.add(newParentRow); 
      } 
     } // end for ParentRow parentRow:originalList 
    } //end else 


    notifyDataSetChanged(); 
} 

我OnChildClickListener

 myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(), 
            parentRowArrayList.get(groupPosition).getChildList() 
              .get(childPosition).getText() +"", 
         Toast.LENGTH_SHORT) 
         .show(); 
       return false; 
      } 
     }); 

我OnGroupClickListener

myList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { 

      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, 
             int groupPosition, long id) { 
       Toast.makeText(getApplicationContext(), 
       "Group Clicked " + parentRowArrayList.get(groupPosition).getName(), 
       Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     }); 

搜索查看實現的方法

@Override 
public boolean onClose() { 
    listAdapter.filterData(""); 
    expandAll(); 
    return false; 
} 

@Override 
public boolean onQueryTextChange(String query) { 
    listAdapter.filterData(query); 
    myList.setAdapter(listAdapter); 
    listAdapter.notifyDataSetChanged(); 
    expandAll(); 
    return false; 
} 

@Override 
public boolean onQueryTextSubmit(String query) { 
    listAdapter.filterData(query); 
    myList.setAdapter(listAdapter); 
    listAdapter.notifyDataSetChanged(); 
    expandAll(); 
    return false; 
} 
+0

是適配器的篩選數據方法的一部分? – Rohit

+0

filterData在適配器類 –

回答

1

因爲篩選數據的方法是在適配器類和它指的是同一個實例成員變量(即parentRowArrayList)和OnChildClickListener部分的活動的話,指向兩個不同的數據源的這兩種不同的方法。如果從OnChildClickListener方法中的適配器引用'arraylist',將獲得相同的結果。

+0

中,我該怎麼做? –

+0

在parentRowArrayList的適配器類中添加getter方法。現在在你的點擊監聽器方法中使用它,例如'Toast.makeText(getApplicationContext(), 「Group Clicked」+ listAdapter.getparentRowArrayList()。get(groupPosition).getName(), Toast.LENGTH_SHORT).show();'它的工作原理是 – Rohit

+0

。謝謝 –