我创建了一个"搜索" Android中的框。当我尝试搜索任何内容时,它没有在列表中显示更新的值。该值将进入列表但搜索相关值不会更改。这是我的快照:
如果我进入" ab"它运行正常,但是当我正在使用#34;时,它会显示第一个listView项目:
这是我的Fragment类,我使用的是addTextChangeListener
:
etSearchUser.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (i2<i1){
adapter.resetData();
}
adapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
这是我的适配器类:
public class RemoveUserAdapter extends BaseAdapter implements Filterable {
private ArrayList<UserModel> listcontent;
private ArrayList<UserModel> searchList;
Context context;
private Filter planetFilter;
UserModel content, content1;
LayoutInflater inflater;
public RemoveUserAdapter(Context context, ArrayList listContent) {
this.context = context;
this.listcontent = listContent;
this.searchList = listContent;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listcontent.size();
}
@Override
public Object getItem(int i) {
return listcontent.get(i);
}
@Override
public long getItemId(int i) {
return listcontent.get(i).hashCode();
}
class ViewHolder {
private TextView tvEmail, tvUserId;
private CheckBox checkBox;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder viewHolder, viewHolder1;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.remove_user_adapter, null);
viewHolder = new ViewHolder();
viewHolder.tvEmail = (TextView) view.findViewById(R.id.tvEmailUser);
viewHolder.tvUserId = (TextView) view.findViewById(R.id.tvUserGroup);
viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
viewHolder.checkBox.setVisibility(View.INVISIBLE);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
content = (UserModel) listcontent.get(i);
content = (UserModel) searchList.get(i);
//Log.i("@@@",content.getEmail()+" email");
viewHolder.tvEmail.setText(content.getEmail());
viewHolder.tvUserId.setText(content.getUsergroup());
return view;
}
public void resetData() {
listcontent = searchList;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected void publishResults(CharSequence charSequence, FilterResults result) {
listcontent = (ArrayList<UserModel>) result.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<UserModel> FilteredArrList = new ArrayList<UserModel>();
if (searchList == null) {
searchList = new ArrayList<UserModel>(listcontent);
}
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = searchList.size();
results.values = searchList;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < searchList.size(); i++) {
content = (UserModel) searchList.get(i);
String data = content.getEmail();
String user = content.getUserId();
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(new UserModel(data, user));
}
}
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
}
对于搜索,我使用的是简单的EditText。
还有一件事就是从Gmail的第一封信中搜索。 我想让它随机搜索。
例如:abc@gmail.com
在那里,如果我进入搜索框&#34; gmail&#34;它应该显示一个结果。
这是我的UserModel类:
public class UserModel {
private String usergroup;
private String name;
private String email;
private String phone;
private String password;
private String userId;
public UserModel(String email, String userGroup) {
super();
this.email = email;
this.usergroup = userGroup;
}
public UserModel(){}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsergroup() {
return usergroup;
}
public void setUsergroup(String usergroup) {
this.usergroup = usergroup;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
在Try
中,您必须返回已过滤的列表计数而非原始列表计数。
有关更多信息,请参阅此要点:
{{3}}
我更新了你的代码:
getCount