android 搜索框 添加事件
2018-08-30 本文已影响0人
打死也不加班
image.png
为listView 添加点击事件:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//xxxxxxxxxxxxx
}
});
这边会有问题 通过position获取数据时,由于 搜索框的过滤条件 整个position的范围实际是缩小过了 很难通过position获取选中的值
这边通过适配器获取选中的值
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String)arrayAdapter.getItem(position);
//将值填到对应的控件去
//TextView.setText(item);
dialog.dismiss();
}
});
数据只是简单的String,如果要实现ListView更复杂的功能。
这边需要自定义适配器:
适配器需要重写 过滤方法 新建MyFilter 重写 过滤方法
public class SelectMultiChoicelAdapter extends ArrayAdapter<AffirmInfoBean.DataBean.UserListBean> {
private Context mContext;
private int mResource;
private List<AffirmInfoBean.DataBean.UserListBean> mData;
private MyFilter mFilter;
ArrayList<AffirmInfoBean.DataBean.UserListBean> mOriginalValues;
public SelectMultiChoicelAdapter(Context context, int resource, List<AffirmInfoBean.DataBean.UserListBean> objects) {
//这里会将object赋值给父类的mObjects成员变量,问题的所在
super(context, resource, objects);
this.mContext = context;
this.mResource = resource;
this.mData = objects;
}
@Override
public int getCount() {
return mData.size();
}
@Nullable
@Override
public AffirmInfoBean.DataBean.UserListBean getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(mContext).inflate(mResource, parent, false);
} else {
view = convertView;
}
CheckBox checkBox =(CheckBox) view.findViewById(R.id.cb_item);
TextView text = (TextView) view.findViewById(R.id.tv_item);
checkBox.setChecked(mData.get(position).getIsChecked());
text.setText(mData.get(position).getText());
return view;
}
@NonNull
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new MyFilter();
}
return mFilter;
}
private class MyFilter extends Filter {
@Override
//过滤数据
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
//为null,表示没有赋值过,这里的逻辑是mOriginalValues保存原始数据,而mData保存过滤后的数据
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<>(mData);
}
if (TextUtils.isEmpty(filterString)) {
results.values = mOriginalValues;
results.count = mOriginalValues.size();
} else {
List<AffirmInfoBean.DataBean.UserListBean> values = new ArrayList<>(mOriginalValues);
List<AffirmInfoBean.DataBean.UserListBean> newValues = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
AffirmInfoBean.DataBean.UserListBean value = values.get(i);
if (value.getText().contains(filterString)) {
newValues.add(value);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
//mData.clear();
//mData.addAll((List<String>)results.values);
//noinspection unchecked
mData = (List<AffirmInfoBean.DataBean.UserListBean>) results.values;
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}