2017-06-05 3 views
0

私の展開可能なリストビューで検索した後にonClickに問題があります。私は正しい結果を得ることができますが、個別にクリックしたときに値をトーストすると、別の結果が得られますか?親切に検索onGroupClickとonChildを使用してExpandable Listviewを検索すると、間違った結果が表示されます

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; 
      } 
     }); 

SearchView実装されたメソッドを助ける

@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

filterDataメソッドはアダプタの一部ですか? – Rohit

+0

filterDataはアダプタクラス –

答えて

1

filterDataメソッドがアダプタクラスにあり、アクティビティの同じインスタンスメンバ変数(parentRowArrayList)とOnChildClickListener部分を参照するため、これら2つの異なるメソッドが2つの異なるデータソースを指しています。 OnChildClickListenerメソッドのアダプタから 'arraylist'を参照すると、同じ結果が得られます。

+0

にありますので、どうすればよいですか? –

+0

parentRowArrayListのアダプタクラスにgetterメソッドを追加します。これで、Toast.makeText(getApplicationContext()、 "Group Clicked"のようなクリックリスナーのメソッドで使用します)+ listAdapter .getparentRowArrayList()。get(groupPosition).getName()、 Toast.LENGTH_SHORT).show(); ' – Rohit

+0

それが動作します。ありがとう –

関連する問題