Android - ExpandableListActivity过滤

时间:2015-06-15 12:38:48

标签: java android filtering expandablelistview textwatcher

我想通过edittext过滤我的可扩展列表,但我不知道该怎么做。我在ExpandableListView中检查了一些关于TextWatcher的教程,但我仍然不知道如何在ExpandableListActivity中做到这一点(是的,我是初学者)。这是我的代码:

MainActivity:

public class MainActivity extends ExpandableListActivity {

private ArrayList<Parent> parents;
private EditText editSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    editSearch = (EditText)findViewById(R.id.editSearch);


    getExpandableListView().setGroupIndicator(null);
    getExpandableListView().setDividerHeight(1);
    registerForContextMenu(getExpandableListView());

    //Creating static data in arraylist
    final ArrayList<Parent> dataList = putDataIntoArrays();

    // Adding ArrayList data to ExpandableListView values
    loadHosts(dataList);

    editSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    setContentView(R.layout.activity_main);

}
MainActivity中的

loadHosts方法:

private void loadHosts(final ArrayList<Parent> newParents) {
    if (newParents == null)
        return;

    parents = newParents;

    // Check for ExpandableListAdapter object
    if (this.getExpandableListAdapter() == null) {
        //Create ExpandableListAdapter Object
        final ExpandableListAdapter mAdapter = new ExpandableListAdapter();

        // Set Adapter to ExpandableList Adapter
        this.setListAdapter(mAdapter);
    } else {
        // Refresh ExpandableListView data
        ((ExpandableListAdapter) getExpandableListAdapter()).notifyDataSetChanged();
    }
}

我还有类ExpandableListAdapter,它扩展了BaseExpandableListAdapter和将数据放入可扩展列表的方法。我知道我应该使用onTextChanged方法,但是如何?我将不胜感激。

修改

我已经对我的ExpandableListAdapter类实现了Filterable,并添加了两个方法:

    private class ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable { 

        public void notifyDataSetInvalidated() {
        super.notifyDataSetInvalidated();
        }

        @Override
        public Filter getFilter() {
            Filter filter = new MyFilter();
            return filter;
        }
    }

然后我创建了扩展Filter的类MyFilter:

private class MyFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        constraint = editSearch.getText().toString().toLowerCase();
        FilterResults result = new FilterResults();
        ArrayList<Parent> oList = new ArrayList<>();
        ArrayList<Parent> newList = new ArrayList<>();

        if (constraint != null && constraint.toString().length() > 0) {


            synchronized (this) {
                oList.addAll(filteredData);
            }
            for (Parent parent : oList) {
                if (parent.getName().toLowerCase().contains(constraint))
                    newList.add(parent);
            }
            result.count = newList.size();
            result.values = newList;
        } else {
            oList.addAll(filteredData);
            synchronized (this) {
                result.count = oList.size();
                result.values = oList;
            }

        }
        return result;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {

        if (results.count > 0) {
            Log.d("TEST", results.count + " FOUND");
        } else Log.d("TEST", "NO RESULTS");

        ArrayList<Parent> filtered = (ArrayList<Parent>) results.values;

    }
}

其中filteredData是包含所有Parent对象的原始数组的副本。我还在onTextChanged方法中添加了这一行:

public void onTextChanged(CharSequence s, int start, int before, int count) {
            ((Filterable) getExpandableListAdapter()).getFilter().filter(s);

        }

现在我必须通过publishResults()方法发布resulst,但是我该怎么办呢?日志显示,一切正常,但我不知道如何显示此过滤数据。

编辑2

问题解决了 - 只需将这两行添加到publishResults方法:

 protected void publishResults(CharSequence constraint, FilterResults results) {

        parents = (ArrayList<Parent>) results.values;
        ((ExpandableListAdapter) getExpandableListAdapter()).notifyDataSetChanged();

    }

0 个答案:

没有答案