使用 onGroupClick 和 onChildClick 搜索可扩展列表视图显示错误结果
Searching Expandable Listview with onGroupClick & onChildClick showing wrong results
在我的可扩展列表视图中搜索后,我的 onClick 出现问题。当我搜索时我得到了正确的结果,但是当我单独单击它们时 Toast 值时,我得到了不同的结果?请帮助
我过滤搜索数据的方法
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;
}
因为 filterData 方法在适配器 class 中,它引用相同的实例成员变量(即 parentRowArrayList)和 activity 的 OnChildClickListener 部分,所以,这两个不同的方法指向两个不同的数据源。如果您在 OnChildClickListener 方法中引用来自适配器的 'arraylist' 将获得相同的结果。
在我的可扩展列表视图中搜索后,我的 onClick 出现问题。当我搜索时我得到了正确的结果,但是当我单独单击它们时 Toast 值时,我得到了不同的结果?请帮助
我过滤搜索数据的方法
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;
}
因为 filterData 方法在适配器 class 中,它引用相同的实例成员变量(即 parentRowArrayList)和 activity 的 OnChildClickListener 部分,所以,这两个不同的方法指向两个不同的数据源。如果您在 OnChildClickListener 方法中引用来自适配器的 'arraylist' 将获得相同的结果。