在android listview中搜索

时间:2014-09-08 12:48:08

标签: android listview android-listview textwatcher

我已在每个列表视图项目中输入了商家名称,城市和区域。我在列表视图的顶部放置了一个编辑文本,用于搜索过程。 我想通过商业名称,城市和地区搜索列表视图项目,我在编辑文本上使用了文本观察器并使用了过滤器;但这不起作用。我该怎么办???

这是我的代码:

CustomerShowPointActivity.java: -

                    ListView lv = (ListView) findViewById(R.id.list);
                    final ShowPointAdapter adapter = new ShowPointAdapter(
                            CustomerShowPointActivity.this, item);
                    lv.setAdapter(adapter);
                    lv.setTextFilterEnabled(true);
                    lv.bringToFront();

                    // Locate the EditText in listview_main.xml
                    editsearch = (EditText) findViewById(R.id.et1);
                    // Capture Text in EditText
                    editsearch.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void onTextChanged(CharSequence s, int start,
                                int before, int count) {

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start,
                                int count, int after) {
                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            String text = editsearch.getText().toString()
                                    .toLowerCase(Locale.getDefault());
                            adapter.filter(text);
                        }
                    });

这是我的Adapter类: -

public class ShowPointAdapter extends BaseAdapter {

// Declare Variables
Context mContext;
LayoutInflater mInflater;
private List<PointItem> item = null;
private ArrayList<PointItem> arraylist;
String search;

public ShowPointAdapter(Context context, List<PointItem> item) {
    mContext = context;
    this.item = item;
    mInflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<PointItem>();
    this.arraylist.addAll(item);
}

public class ViewHolder {
    TextView Bus_name;
    TextView bus_area;
    TextView bus_point;
}

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

@Override
public PointItem getItem(int position) {
    return item.get(position);
}

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

public boolean isEnabled(int position) {
    return false;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = mInflater.inflate(R.layout.customer_show_point_item, null);
        holder.Bus_name = (TextView) view
                .findViewById(R.id.item_bname);
        holder.bus_area = (TextView) view
                .findViewById(R.id.item_barea);
        holder.bus_point = (TextView) view
                .findViewById(R.id.bus_point);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // set the result into textviews
    holder.Bus_name.setText(item.get(position).getbus_name());
    holder.bus_area.setText(item.get(position).getbus_area() + ", "
            + item.get(position).getbus_city());
    holder.bus_point.setText(item.get(position).getcus_bus_point());
    return view;
}

public void filter(String charText) {
    // TODO Auto-generated method stub
    charText = charText.toLowerCase(Locale.getDefault());
    item.clear();
    if (charText.length() == 0) {
        item.addAll(arraylist);
    } else {
        for (PointItem it : arraylist) {
            if (it.getbus_name().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                item.add(it);
            }
        }
    }
    notifyDataSetChanged();
}
}

2 个答案:

答案 0 :(得分:3)

我的代码中没有使用Holder ...只需更改showPointAdapter类代码...使用

final ViewHolder holder;

只需更改适配器类代码: -

public class ShowPointAdapter extends BaseAdapter {


Context mContext;
LayoutInflater mInflater;
private List<PointItem> item = null;
private ArrayList<PointItem> arraylist;
String search;

public ShowPointAdapter(Context context, List<PointItem> item) {
    mContext = context;
    this.item = item;
    mInflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<PointItem>();
    this.arraylist.addAll(item);
}

public class ViewHolder {
    TextView Bus_name;
    TextView bus_area;
    TextView bus_point;
}

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

@Override
public PointItem getItem(int position) {
    return item.get(position);
}

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

public boolean isEnabled(int position) {
    return false;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = mInflater.inflate(R.layout.customer_show_point_item, null);
        holder.Bus_name = (TextView) view
                .findViewById(R.id.item_bname);
        holder.bus_area = (TextView) view
                .findViewById(R.id.item_barea);
        holder.bus_point = (TextView) view
                .findViewById(R.id.bus_point);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // set the result into textviews
    holder.Bus_name.setText(item.get(position).getbus_name());
    holder.bus_area.setText(item.get(position).getbus_area() + ", "
            + item.get(position).getbus_city());
    holder.bus_point.setText(item.get(position).getcus_bus_point());
    return view;
}

public void filter(String charText) {
    // TODO Auto-generated method stub
    charText = charText.toLowerCase(Locale.getDefault());
    item.clear();
    if (charText.length() == 0) {
        item.addAll(arraylist);
    } else {
        for (PointItem it : arraylist) {
            if (it.getbus_name().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                item.add(it);
            }
        }
    }
    notifyDataSetChanged();
}
} 

答案 1 :(得分:0)

您需要执行过滤并在适配器中实现Filterable。 接下来,覆盖

   @Override
public Filter getFilter() {
      if (mFilter == null)
      {
          mFilter = new ValueFilter();
      }
    return mFilter;
}

你需要做下面的事情:

私有类ValueFilter扩展了Filter {

    //Invoked in a worker thread to filter the data according to the constraint.
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results=new FilterResults();
        if(constraint!=null && constraint.length()>0){
            ArrayList<Vertex> filterList = new ArrayList<Vertex>();
            for(int i=0;i<mStringFilterList.size();i++)
            {
                Vertex v = mStringFilterList.get(i);
                if (v != null)
                {

                    if((v.getTitle().toUpperCase())
                            .contains(constraint.toString().toUpperCase()))
                    {
                        Vertex contacts = null;
                        try {
                            contacts = new Vertex(v.getJSObject());
                            contacts.setTitle(v.getTitle());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        filterList.add(contacts);
                    }
                }

            }
            results.count=filterList.size();
            results.values=filterList;
        }else{
            results.count=mStringFilterList.size();
            results.values=mStringFilterList;
        }
        return results;
    }


    //Invoked in the UI thread to publish the filtering results in the user interface.
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
                                  FilterResults results) {
        mData=(ArrayList<Vertex>) results.values;
        notifyDataSetChanged();
    }
}

现在可能没问题。