我在AutoCompleteTextView中显示自动填充建议时遇到问题。
这是TextView:
<AutoCompleteTextView
android:id="@+id/atvAddContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white" />
这就是我设置适配器的方式:
ArrayList<Contacto> list = cc.getContactosList("");
ContactosAdapter2 adapter = new ContactosAdapter2(this, R.layout.contact_list_item, list);
adapter.notifyDataSetChanged();
现在对于棘手的部分,这是我的适配器,它是ListView的完全工作适配器的副本(具有相同的列表项),但它实现了Filterable,现在它不会过滤,应该显示所有结果,但它没有显示任何结果。设置为适配器的ArrayList包含多个项目,因此数据就在那里。
我的getFilter()方法永远不会被调用,我不知道为什么,与getView相同。
我已经尝试了一个带有String数组的简单自动完成功能,但它无法正常使用自定义对象自定义自动完成,欢迎任何帮助!
import java.util.ArrayList;
import com.android.proj.R;
import com.android.proj.datas.Contacto;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;
public class ContactosAdapter2 extends ArrayAdapter<Contacto> implements Filterable{
Context context;
int layoutResourceId;
ArrayList<Contacto> data = new ArrayList<Contacto>();
public ContactosAdapter2(Context context, int layoutResourceId, ArrayList<Contacto> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public Filter getFilter()
{
Filter myFilter = new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults filterResults = new FilterResults();
if(constraint != null)
{
filterResults.values = data;
filterResults.count = data.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results)
{
if(results != null && results.count > 0)
{
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ContactoHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ContactoHolder();
holder.contactoNombre = (TextView)row.findViewById(R.id.tvContactoNombre);
holder.contactoInfo = (TextView)row.findViewById(R.id.tvContactoInfo);
holder.contactoImg = (ImageView)row.findViewById(R.id.ivContactoImg);
row.setTag(holder);
}
else
{
holder = (ContactoHolder)row.getTag();
}
Contacto ap = data.get(position);
holder.contactoNombre.setText(ap.getNombre());
byte[] baImage = ap.getBaImage();
if (baImage == null)
holder.contactoImg.setImageURI(ap.getPhoto());
else
holder.contactoImg.setImageBitmap(BitmapFactory.decodeByteArray(baImage, 0, baImage.length));
if (ap.getNumber().length() > 0 && ap.getNumber().charAt(0) == '#')
holder.contactoInfo.setText(ap.getNumber().substring(1, ap.getNumber().length()));
else
holder.contactoInfo.setText(ap.getNumber());
return row;
}
static class ContactoHolder
{
TextView contactoNombre;
TextView contactoInfo;
ImageView contactoImg;
}
}
答案 0 :(得分:0)
(在问题编辑中由OP回答。转换为社区维基答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat))
OP写道:这确实非常愚蠢,但我没有说明要用于AutoCompleteTextView的适配器,缺少的代码是:
atvAddContact.setAdapter(adapter);