有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

ExpandableListView适配器过滤函数的java实现

我想在ExpandableListView适配器中过滤数据,但基本上我不知道如何做到这一点。我看了很多教程,但是,也许我一开始就有很多复杂的适配器

这是我的适配器:

package com.example.senso.tutuapp;

import 安卓.content.Context;
import 安卓.graphics.Typeface;
import 安卓.util.Log;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseExpandableListAdapter;
import 安卓.widget.Filter;
import 安卓.widget.Filterable;
import 安卓.widget.TextView;

import java.util.HashMap;
import java.util.List;


public class ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable {
    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lgroupItem);

        txtListChild.setText(childText);
        return convertView;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lgroupHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

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

    @Override
    public Filter getFilter() {
        return null;
    }

    public void filterData(String query){
        //TODO::No idea how to filter this

        query = query.toLowerCase();

        notifyDataSetChanged();

    }

}

下面是一个示例,我基本上是如何使用expandablelistview的

public class WorkActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;
    SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.work_layout);

        Intent newActivity1 = new Intent();
        setResult(RESULT_OK,newActivity1);


        expListView = (ExpandableListView)findViewById(R.id.expandable_list_view);

        // preparing list data
        prepareListData();


        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        searchView = (SearchView) findViewById(R.id.searcher);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

            }
        });


    }
private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Станция отправления");
    listDataHeader.add("Станция прибытия");
    listDataHeader.add("Дата отправления");


    // Adding child data
    List<String> departStation = new ArrayList<String>();
    departStation.add("Test1");
    departStation.add("fffff");

    List<String> arriveStation  = new ArrayList<String>();
    arriveStation.add("Test2");

    List<String> data = new ArrayList<String>();
    data.add("Test3");



    listDataChild.put(listDataHeader.get(0), departStation); // Header, Child data
    listDataChild.put(listDataHeader.get(1), arriveStation);
    listDataChild.put(listDataHeader.get(2), data);}

提前感谢您,并为我的错误代码感到抱歉,我正处于安卓之旅的开始阶段


共 (0) 个答案