我的asynctask中有一个Listview,我正在设置适配器。我应该实现自动搜索完成功能。我的代码工作正常,但是当我在可编辑的textview中输入文本时,列表不会被过滤。
@Override
protected void onPostExecute(ArrayList<RouteTrackerUser> result)
{
dialog.dismiss();
if(result!=null)
{
//System.out.println("User with result===>"+result);
/*for(RouteTrackerUser r :result ){
System.out.println("Contents===>"+r.getStoreid()+" "+r.getStorename()+" "+r.getStorecode()+" "+r.getStoreno()+" "+r.getVisitcount());}
*/ Intent intent=getIntent();
TextView username=(TextView)findViewById(R.id.usrname);
final String sname=intent.getExtras().getString("user_name");
username.setText("Welcome, "+sname);
userList = (ListView) findViewById(R.id.historylist);
userAdapter = new HistoryAdapter(History.this, R.layout.history_visits,R.id.vcount,result);
userList.setAdapter(userAdapter);
EditText inputSearch = (EditText) findViewById(R.id.autocomplete);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
System.out.println("afafasdf"+cs);
History.this.userAdapter.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
}
});
}
我的customAdapter如下:
private static class ViewHolder {
public final TextView storename;
public final TextView storevisitcount;
public ViewHolder(TextView name, TextView count) {
this.storename = name;
this.storevisitcount = count;
}
}
public class HistoryAdapter extends ArrayAdapter<RouteTrackerUser>
{
Context context;
//EditText autocomplete;
public HistoryAdapter(Context context,int resource,int textViewResourceId,ArrayList<RouteTrackerUser> result) {
super(context,resource,textViewResourceId, result);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView name;
TextView vcount;
final RouteTrackerUser User = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.history_visits, parent, false);
name = (TextView) convertView.findViewById(R.id.sname);
vcount = (TextView) convertView.findViewById(R.id.vcount);
convertView.setTag(new ViewHolder(name, vcount));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewdata(User.getUsername(),User.getUid(),User.getStoreid(),User.getStorename(),User.getStorecode(),User.getStoreno(),User.getVisitcount());
}});
} else {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
name = viewHolder.storename;
vcount = viewHolder.storevisitcount;
}
name.setText(User.getStorename());
vcount.setText("Visit Count: "+User.getVisitcount());
return convertView;
}
}
注意:我的主类名是History.java,Asynctask名是HistorySoapTask。
答案 0 :(得分:0)
搜索了几篇博客和其他帖子后,我找到了问题的答案。我犯了一个错误,我没有在我的程序中实现Filter概念,现在它工作正常。因此,为了过滤列表,我们应该如下定义过滤器:
private class OrderAdapterFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterText = (String) constraint;
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<RouteTrackerUser> filt = new ArrayList<RouteTrackerUser>();
ArrayList<RouteTrackerUser> lItems = new ArrayList<RouteTrackerUser>(fullitems);
for(int i = 0, l = lItems.size(); i < l; i++)
{
RouteTrackerUser m = lItems.get(i);
if(m.getStorename().toLowerCase().contains(constraint))
filt.add(m);
}
result.count = filt.size();
result.values = filt;
}
else
{
ArrayList<RouteTrackerUser> list = new ArrayList<RouteTrackerUser>(fullitems);
result.values = list;
result.count = list.size();
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// NOTE: this function is *always* called from the UI thread.
filtered = (ArrayList<RouteTrackerUser>)results.values;
originalitems.clear();
for(int i = 0, l = filtered.size(); i < l; i++)
originalitems.add(filtered.get(i));
notifyDataSetChanged();
}
}