1
我在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相同。
我已經嘗試了一個簡單的自動完成與字符串數組,它的工作,但我無法讓我的頭圍繞自定義對象的自定義自動完成,任何幫助,歡迎!
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;
}
}