Filetrable自定义适配器android

时间:2014-11-05 11:37:36

标签: android baseadapter searchview android-appcompat

我正在使用SearchView和appcompat为我的列表做一个可过滤的自定义适配器。也许函数public Filter getFilter()很糟糕,有人可以帮帮我吗?我不能,搜索不起作用。我的适配器代码在这里:

public class LazyAdapter extends BaseAdapter implements Filterable {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private ArrayList<HashMap<String, String>> orig;
    private static LayoutInflater inflater=null;

    Typeface tf; 

    Context context;


    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d, String font) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        tf = Typeface.createFromAsset(activity.getAssets(), font);
    }


    public Filter getFilter() {
        return new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<HashMap<String, String>> results = new ArrayList<HashMap<String, String>>();
                if (orig == null)
                    orig = data;
                if (constraint != null) {
                    if (orig != null && orig.size() > 0) {
                        for (final HashMap<String, String> g: orig) {
                            if (g.toString()
                                    .contains(constraint.toString()))
                                results.add(g);
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                data = (ArrayList<HashMap<String, String>>) results.values;
                notifyDataSetChanged();
            }
        };
    }

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


    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

            TextView id = (TextView)vi.findViewById(R.id.txt_id); // poi_id
            TextView title = (TextView)vi.findViewById(R.id.txt_title); // poi_title
            TextView geocoordinates = (TextView)vi.findViewById(R.id.txt_geocoordinates); //poi_geocoordinate

            HashMap<String, String> poi = new HashMap<String, String>();
            poi = data.get(position);

            // Setting all values in listview
            id.setText(poi.get(POIListActivity.TAG_ID));
            id.setTypeface(tf);

            title.setText(poi.get(POIListActivity.TAG_TITLE));
            title.setTypeface(tf);

            geocoordinates.setText(poi.get(POIListActivity.TAG_GEOCOORDINATES));
            geocoordinates.setTypeface(tf);

        return vi;
    }
}

在我的活动中:

@覆盖     public boolean onCreateOptionsMenu(Menu menu){         //给菜单充气;这会将项目添加到操作栏(如果存在)。         getMenuInflater()。inflate(R.menu.main,menu);

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    MenuItem searchItem = menu.findItem(R.id.action_search);

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    // searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    // searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    return super.onCreateOptionsMenu(menu);
}


public boolean onQueryTextChange(String newText)
{

    if (TextUtils.isEmpty(newText)) {
        list_poi.clearTextFilter();
    } else {
        list_poi.setFilterText(newText);
    }
    return true;
}

public boolean onQueryTextSubmit(String query)
{
    return false;
}

我的searchable.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_name"
            >
</searchable>

在我的清单中:

 <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>



            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

        </activity>

0 个答案:

没有答案