使用onGroupClick&搜索可扩展Listview onChildClick显示错误的结果

时间:2017-06-05 11:07:53

标签: java android listview expandablelistview

在我的可扩展列表视图中搜索后,我的onClick出现问题。当我搜索到我得到正确的结果,但当我单独点击它时敬酒价值,我得到不同的结果?请帮助

我的搜索数据的方法

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

1 个答案:

答案 0 :(得分:1)

因为filterData方法在适配器类中,并且它引用了同一个实例成员变量(即parentRowArrayList)和OnChildClickListener部分活动所以,这两个不同的方法指向两个不同的数据源。如果你引用了'arraylist&#39;从OnChildClickListener方法中的适配器将获得相同的结果。