适配器列表视图过滤Android

时间:2016-05-31 02:44:26

标签: android listview search filter

嗨,伙计们,任何人都可以帮助我。我不知道为什么,我无法在我的listview适配器中实现过滤器,错误是"不兼容的类型" on method getfilter()" String dataNames = mStringFilterList.get(i);" 这是我的适配器代码:

package com.ekoacak.belajartabhost;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Acer on 14/05/2016.
 */
public class ActorAdapter extends BaseAdapter implements Filterable {
    Context mContext;
    LayoutInflater layoutInflater;
    public ArrayList<Actors> arrayList;
    public List<Actors> actorsList = null;
    List<Actors> mStringFilterList;


    public ActorAdapter(Context context, List<Actors> actorsList) {
        mContext = context;
        this.actorsList = actorsList;
        mStringFilterList = actorsList;
        layoutInflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Actors>();
        this.arrayList.addAll(actorsList);
    }

    public class ViewHolder{
        TextView tvId,tvJudul,tvJenis,tvProvinsi,tvKota,tvdeskrip;
    }

    @Override
    public int getCount() {
        return actorsList.size();
    }

    @Override
    public Object getItem(int position) {
        return actorsList.get(position);
    }

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

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        final  ViewHolder holder;
        if (v == null){
            holder = new ViewHolder();
            v = layoutInflater.inflate(R.layout.data_item, null);
            holder.tvId = (TextView) v.findViewById(R.id.txtIDwisata);
            holder.tvJudul = (TextView) v.findViewById(R.id.txtjudulwisata);
            holder.tvJenis = (TextView) v.findViewById(R.id.txtjeniswisata);
            holder.tvProvinsi = (TextView) v.findViewById(R.id.txtprovwisata);
            holder.tvKota = (TextView) v.findViewById(R.id.txtkotawisata);
            holder.tvdeskrip = (TextView) v.findViewById(R.id.txtdeskripwisata);
            v.setTag(holder);
        }else{
            holder = (ViewHolder) v.getTag();
        }

        holder.tvId.setText(actorsList.get(position).getId());
        holder.tvJudul.setText(actorsList.get(position).getJudul());
        holder.tvJenis.setText(actorsList.get(position).getJenis());
        holder.tvProvinsi.setText(actorsList.get(position).getProvinsi());
        holder.tvKota.setText(actorsList.get(position).getKota());
        holder.tvdeskrip.setText(actorsList.get(position).getDeskrip());


        return v;
    }

    //Filter
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                List<String> arrayListNames = (List<String>) results.values;
                notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();
                ArrayList<String> FilteredArrayNames = new ArrayList<String>();

                // perform your search here using the searchConstraint String.

                constraint = constraint.toString().toLowerCase();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    String dataNames = mStringFilterList.get(i);
                    if (dataNames.toLowerCase().startsWith(constraint.toString()))  {
                        FilteredArrayNames.add(dataNames);
                    }
                }
                results.count = FilteredArrayNames.size();
                results.values = FilteredArrayNames;
                Log.e("VALUES", results.values.toString());

                return results;
            }
        };
        return filter;
    }

}

1 个答案:

答案 0 :(得分:0)

更改此行

String dataNames = mStringFilterList.get(i);

Actors dataNames = mStringFilterList.get(i);