本文介绍了在AutoCompleteTextView自定义筛选不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我延长了数组适配器,因为它遵循,但我得到的还是旧的结果你能告诉我是什么问题?
公共类适配器扩展ArrayAdapter<字符串>实现过滤的{
私人的ArrayList<字符串>项目;
公共适配器(上下文的背景下,INT textViewResourceId,字符串[]对象){
超(背景下,textViewResourceId,对象);
项目=新的ArrayList<字符串>();
的for(int i = 0; I< objects.length;我++)
items.add(对象[I]);
}
@覆盖
公众诠释getCount将(){
返回items.size();
}
@覆盖
公共字符串的getItem(INT位置){
返回items.get(位置);
}
@覆盖
公共过滤用getFilter(){
过滤myFilter =新的过滤器(){
@覆盖
保护FilterResults performFiltering(CharSequence的arg0中){
FilterResults rezultate =新FilterResults();
ArrayList的<字符串> chestii =新的ArrayList<字符串>();
的for(int i = 0; I< items.size();我++)
{
串TMP = items.get(ⅰ).toUpperCase();
如果(tmp.startsWith(arg0.toString()。与toUpperCase()))
chestii.add(items.get(ⅰ));
}
rezultate.count = chestii.size();
rezultate.values = chestii;
返回rezultate;
}
@覆盖
保护无效publishResults(CharSequence的约束,
FilterResults结果){
如果(结果= NULL和放大器;!&安培; results.count大于0)
{
notifyDataSetChanged();
}
别的notifyDataSetInvalidated();
}
};
返回myFilter;
}
}
解决方案
您的列表中包含的项目,从项目和,必须从项目中删除的位置,然后调用notifyDataSetChange,你不要修改的项目在其最近恢复所有你必须拯救previous项目项
这是正常工作:
/ **
*适配器包装重新对话的present名单
* @author利亚赞策夫德米特里
* @Email [email protected] 2012
* /
公共类FriendsAdapter扩展ArrayAdapter<使用者>
{
私人最终LayoutInflater充气;
私人最终ImageLoader的IL;
私人语境parentContext;
私人列表<使用者> MDATA;
私人列表<使用者> mOriginalData;
公共SimpleImageLoader SIL;
@覆盖
公共无效添加(用户对象)
{
如果(mOriginalData!= NULL)
mOriginalData.add(对象);
其他
mData.add(对象);
}
@覆盖
公共无效删除(用户对象)
{
如果(mOriginalData!= NULL)
mOriginalData.remove(对象);
其他
mData.remove(对象);
}
@覆盖
公众诠释getCount将()
{
返回mData.size();
}
@覆盖
公众用户的getItem(INT位置)
{
返回mData.get(位置);
}
@覆盖
公众诠释为getPosition(用户项)
{
返回mData.indexOf(项目);
}
公共FriendsAdapter(上下文的背景下,INT textViewResourceId,列表<使用者>对象)
{
超(背景下,textViewResourceId,对象);
Log.v(刷新,背景++ textViewResourceId ++对象);
parentContext =背景;
充气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
IL =新ImageLoader的(上下文);
MDATA =物体;
SIL =新SimpleImageLoader(背景下,真实,64);
sil.setOnUiThread(假);
}
/ **
*我们有一个自定义视图,需要组织它
* /
@覆盖
公共查看getView(最终诠释的立场,观点convertView,最终的ViewGroup家长)
{
ViewHolder持有人;
如果(convertView == NULL)
{
convertView = inflater.inflate(R.layout.friends_list_row,NULL);
持有人=新ViewHolder();
holder.name =(TextView中)convertView.findViewById(R.id.friendName);
holder.root = convertView.findViewById(R.id.root);
holder.photo =(ImageView的)convertView.findViewById(R.id.friendPhoto);
holder.online =(ImageView的)convertView.findViewById(R.id.online);
convertView.setTag(保持器);
}
其他
支架=(ViewHolder)convertView.getTag();
用户USER =的getItem(位置);
holder.online.setVisibility(user.isOnline()大于0 View.VISIBLE:View.GONE?);
如果(用户!= NULL)
{
holder.name.setText(user.getFIO());
holder.photo.setTag(user.getPhotoUrl());
如果(user.getPhotoBitmap()!= NULL)
holder.photo.setImageBitmap(user.getPhotoBitmap());
其他
{
holder.photo.setImageResource(R.drawable.contact_nophoto);
sil.displayImageUserAttach(holder.photo,user.getPhotoUrl(),用户,空,假,空);
}
}
// parent.setVisibility(位置%2 == 0 View.GONE:View.VISIBLE?);
返回convertView;
}
私有静态类ViewHolder
{
公共TextView的名称;
公共ImageView的照片;
公众查看根;
公众在线查阅;
}
@覆盖
公共过滤用getFilter()
{
返回新的过滤器()
{
@燮pressWarnings(未登记)
@覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果)
{
Log.v(过滤器,过滤器之后);
MDATA =(名单<使用者>)results.values;
如果(results.count大于0)
{
notifyDataSetChanged();
}
其他
{
notifyDataSetInvalidated();
}
}
@覆盖
保护FilterResults performFiltering(CharSequence的约束)
{
Log.v(过滤器,过滤器执行);
如果(mOriginalData == NULL)
mOriginalData =新的ArrayList<使用者>(MDATA);
清单<使用者>结果;
FilterResults R =新FilterResults();
如果(约束== NULL || constraint.length()< = 0)
结果=新的ArrayList<使用者>(mOriginalData);
其他
{
结果=新的ArrayList<使用者>();
的for(int i = 0; I< mOriginalData.size();我++)
如果(constraint.length()大于0
&功放;&安培; mOriginalData.get(ⅰ).getFIO()。与toLowerCase()
。载(constraint.toString()。与toLowerCase()))
result.add(mOriginalData.get(ⅰ));
}
r.values =结果;
r.count = result.size();
返回ř;
}
};
}
}
I am extending the Array Adapter as it follows but I get still the old results can you please tell me what is the problem ?
public class Adaptor extends ArrayAdapter<String> implements Filterable{
private ArrayList<String> items;
public Adaptor(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
items = new ArrayList<String>();
for (int i = 0; i < objects.length ; i++)
items.add(objects[i]);
}
@Override
public int getCount() {
return items.size();
}
@Override
public String getItem(int position) {
return items.get(position);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter(){
@Override
protected FilterResults performFiltering(CharSequence arg0) {
FilterResults rezultate = new FilterResults();
ArrayList<String> chestii = new ArrayList<String>();
for (int i = 0; i < items.size() ; i++)
{
String tmp = items.get(i).toUpperCase();
if (tmp.startsWith(arg0.toString().toUpperCase()))
chestii.add(items.get(i));
}
rezultate.count = chestii.size();
rezultate.values = chestii;
return rezultate;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
if (results != null && results.count > 0)
{
notifyDataSetChanged();
}
else notifyDataSetInvalidated();
}
};
return myFilter;
}
}
解决方案
Your list contains items from "items" and you don't modify items in it, you have to remove positions from "items" and then call notifyDataSetChange, to restore lately all items you have to save previous items
this is works fine:
/**
* Adapter wrapper to represent list of dialogs
* @author Ryazantsev Dmitry
* @email [email protected] 2012
*/
public class FriendsAdapter extends ArrayAdapter<User>
{
private final LayoutInflater inflater;
private final ImageLoader il;
private Context parentContext;
private List<User> mData;
private List<User> mOriginalData;
public SimpleImageLoader sil;
@Override
public void add(User object)
{
if (mOriginalData != null)
mOriginalData.add(object);
else
mData.add(object);
}
@Override
public void remove(User object)
{
if (mOriginalData != null)
mOriginalData.remove(object);
else
mData.remove(object);
}
@Override
public int getCount()
{
return mData.size();
}
@Override
public User getItem(int position)
{
return mData.get(position);
}
@Override
public int getPosition(User item)
{
return mData.indexOf(item);
}
public FriendsAdapter(Context context, int textViewResourceId, List<User> objects)
{
super(context, textViewResourceId, objects);
Log.v("refresh", context + " " + textViewResourceId + " " + objects);
parentContext = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
il = new ImageLoader(context);
mData = objects;
sil = new SimpleImageLoader(context, true, 64);
sil.setOnUiThread(false);
}
/**
* We have a custom view and need to organize it
*/
@Override
public View getView(final int position, View convertView, final ViewGroup parent)
{
ViewHolder holder;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.friends_list_row, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.friendName);
holder.root = convertView.findViewById(R.id.root);
holder.photo = (ImageView) convertView.findViewById(R.id.friendPhoto);
holder.online = (ImageView) convertView.findViewById(R.id.online);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
User user = getItem(position);
holder.online.setVisibility(user.isOnline() > 0 ? View.VISIBLE : View.GONE);
if (user != null)
{
holder.name.setText(user.getFIO());
holder.photo.setTag(user.getPhotoUrl());
if (user.getPhotoBitmap() != null)
holder.photo.setImageBitmap(user.getPhotoBitmap());
else
{
holder.photo.setImageResource(R.drawable.contact_nophoto);
sil.displayImageUserAttach(holder.photo, user.getPhotoUrl(), user, null, false, null);
}
}
// parent.setVisibility(position % 2 == 0?View.GONE:View.VISIBLE);
return convertView;
}
private static class ViewHolder
{
public TextView name;
public ImageView photo;
public View root;
public View online;
}
@Override
public Filter getFilter()
{
return new Filter()
{
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
Log.v("filter", "filter finished");
mData = (List<User>) results.values;
if (results.count > 0)
{
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
Log.v("filter", "filter perform");
if (mOriginalData == null)
mOriginalData = new ArrayList<User>(mData);
List<User> result;
FilterResults r = new FilterResults();
if (constraint == null || constraint.length() <= 0)
result = new ArrayList<User>(mOriginalData);
else
{
result = new ArrayList<User>();
for (int i = 0; i < mOriginalData.size(); i++)
if (constraint.length() > 0
&& mOriginalData.get(i).getFIO().toLowerCase()
.contains(constraint.toString().toLowerCase()))
result.add(mOriginalData.get(i));
}
r.values = result;
r.count = result.size();
return r;
}
};
}
}
这篇关于在AutoCompleteTextView自定义筛选不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!