我使用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...
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文件中添加以下属性以隐藏加载Activity时的键盘
android:windowSoftInputMode="stateHidden"
值
在activity中创建两个字段
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;
}
对于那些和我有同样问题的人,我在这里找到了最好的例子。来看看……listview getfilter用于多个字符串数组(自定义字符串[]或可绘制的图像)
希望有帮助