有 Java 编程相关的问题?

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

java如何使用searchManager搜索和筛选listview

我正在使用字符串[]{“item 1”、“item 2”、“item 3”}作为arrayadapter,它将显示在我的ListView中,但现在的问题是我无法为搜索目的进行筛选。大多数在线教程都是从数据库中筛选出来的,但就我而言,我根本不接触数据库,所有这些都只是硬编码的。对不起,我的英语不好,请帮忙:)

代码如下: 主要活动。爪哇:

 String[] values = new String[] { "item 1", "item 2", "item 3" } ;
 ItemAdapter adapter = new ItemAdapter(this, values);
 setListAdapter(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) 个答案

  1. # 1 楼答案

    在活动中创建两个字段

    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;
      }
    
  2. # 3 楼答案

    用于ListView的阵列适配器

    // 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