使用过滤器Android的自定义Listview适配器请尝试在我的列表视图上实现一个过滤器。但是只要文字变了,列表就会消失。请帮我把我的密码。适配器类。package com.talagbe.schymn;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.TextView;public class HymnsAdapter extends ArrayAdapter<Hymns> {ArrayList<Hymns> hymnarray;Context context;LayoutInflater inflater;int Resource;public HymnsAdapter(Context context, int resource, ArrayList<Hymns> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
hymnarray=objects;
Resource= resource;
this.context=context;
inflater= (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
convertView= inflater.inflate(Resource,null);
holder= new ViewHolder();
holder.hymntitle= (TextView) convertView.findViewById(R.id.Hymn_title);
// holder.hymntext= (TextView) convertView.findViewById(R.id.Channel_name);
convertView.setTag(holder);
}else{
holder=(ViewHolder)convertView.getTag();
}
holder.hymntitle.setText(hymnarray.get(position).getTitle());
//holder.hymntext.setText(hymnarray.get(position).getText());
return convertView;}
static class ViewHolder{
public TextView hymntitle;
public TextView hymntext;}
}下面是我试图实现过滤器的另一个类。我有一个编辑文本,在textChangeListener上实现
3 回答
红颜莎娜
TA贡献1842条经验 获得超12个赞
// put below code (method) in Adapter classpublic void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); myList.clear(); if (charText.length() == 0) { myList.addAll(arraylist); } else { for (MyBean wp : arraylist) { if (wp.getName().toLowerCase(Locale.getDefault()).contains(charText)) { myList.add(wp); } } } notifyDataSetChanged();}
在适配器类中声明下面的代码
private ArrayList<MyBean> myList; // for loading main listprivate ArrayList<MyBean> arraylist=null; // for loading filter data
下面是适配器构造函数中的代码
this.arraylist = new ArrayList<MyBean>(); this.arraylist.addAll(myList);
和下面的代码在您的活动类中。
final EditText searchET = (EditText)findViewById(R.id.search_et); // Capture Text in EditText searchET.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub String text = searchET.getText().toString().toLowerCase(Locale.getDefault()); adapter.filter(text); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } });
呼如林
TA贡献1798条经验 获得超3个赞
DrawerActivity.userListview .setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int pos = position; Intent intent = new Intent(getContext(), UserDetail.class); intent.putExtra("model", list.get(position)); context.startActivity(intent); } }); return convertView;}@Overridepublic android.widget.Filter getFilter() { return new android.widget.Filter() { @Override protected void publishResults(CharSequence constraint, FilterResults results) { ArrayList<UserListModel> updatelist = (ArrayList<UserListModel>) results.values; UserListCustomAdaptor newadaptor = new UserListCustomAdaptor( getContext(), getCount(), updatelist); if (results.equals(constraint)) { updatelist.add(modelobj); } if (results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); list = new ArrayList<UserListModel>(); if (constraint != null && DrawerActivity.userlist != null) { constraint = constraint.toString().toLowerCase(); int length = DrawerActivity.userlist.size(); int i = 0; while (i < length) { UserListModel modelobj = DrawerActivity.userlist.get(i); String data = modelobj.getFirstName() + " " + modelobj.getLastName(); if (data.toLowerCase().contains(constraint.toString())) { list.add(modelobj); } i++; } filterResults.values = list; filterResults.count = list.size(); } return filterResults; } };}@Overridepublic int getCount() { return list.size();}@Overridepublic UserListModel getItem(int position) { return list.get(position);}
- 3 回答
- 0 关注
- 638 浏览
添加回答
举报
0/150
提交
取消