如何使用searchManager搜索和过滤listview

时间:2015-10-09 03:59:59

标签: java android listview android-listview

我正在使用String [] {“item 1”,“item 2”,“item 3”}作为将在我的ListView中显示的arrayadapter,但现在的问题是我无法过滤搜索目的。大多数在线教程都经过过滤并从数据库中获取,但对于我的情况,我根本不触及数据库,所有这些都只是硬编码。对不起我的英文,请帮助:)

代码如下: MainActivity.java:

 String[] values = new String[] { "item 1", "item 2", "item 3" } ;
 ItemAdapter adapter = new ItemAdapter(this, values);
 setListAdapter(adapter);

Adapter.java代码如下

public class BuildingAdapter extends ArrayAdapter<String> {
 private final Context context;
 private final String[] buildingname;

  public BuildingAdapter(Context context, String[] buildingname) {
    super(context, R.layout.row_buidling_item, buildingname);
    this.context = context;
    this.buildingname = buildingname;
}
 @Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_buidling_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.buildingname);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.buildingicon);
    TextView hints = (TextView) rowView.findViewById(R.id.buildinghints);
    textView.setText(buildingname[position]);
    // Change the icon for Windows and iPhone
    String s = buildingname[position];
  return rowView;
}




//that's all I did, hopefully someone can answer please...

3 个答案:

答案 0 :(得分:0)

在您的活动中创建两个字段

String[] values;
ArrayList<String> filteredValues;

现在将值分配给适配器,如:

    values = new String[]{"item 1", "item 2", "item 3"};
    filteredValues = new ArrayList<>(values.length);
    for (int i = 0; i < values.length; i++) {
        filteredValues.add(values[i]);
    }
    ItemAdapter adapter = new ItemAdapter(this, values);
    setListAdapter(adapter);

现在定义您调用过滤器的过滤器方法:

  private void filter(String s) {
    filteredValues.clear();
    for (int i = 0; i < values.length; i++) {
        if (TextUtils.isEmpty(s) || values[i].contains(s)) {
            filteredValues.add(values[i]);
        }
    }
    //notifyDatasetChange of adapter here;
  }

答案 1 :(得分:0)

ListView的Arrary适配器

// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Listview Data
    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
                            "iPhone 4S", "Samsung Galaxy Note 800",
                            "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
    lv.setAdapter(adapter);       

}

启用搜索功能

inputSearch.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
        // When user changed the Text
        MainActivity.this.adapter.getFilter().filter(cs);   
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub                          
    }
});

最后在AndroidManifest.xml文件中添加以下属性,以便在加载活动时隐藏键盘。

 android:windowSoftInputMode="stateHidden"

refference

答案 2 :(得分:0)

对于那些与我有同样问题的人,我在这里找到了最好的例子:) 看一看... listview getfilter for multiple string array (customized String[] or drawable images)

希望它有所帮助:)