我正在使用带有AutoCompleteTextView的自定义适配器。在模拟器和我的平板电脑上都可以正常工作。但是,我的手机在横向模式下存在问题。在此模式下显示的自动完成提示是对象信息而不是文本。但是,当我选择任何项目时,相应字段中的文本将正确填充字段。

基于Android Stock Array Adapter的其他字段的“自动完成”效果很好。

我需要在“自定义适配器”中为此做些什么吗?我只看到了一个类似的问题。这个问题的一个答案是谈论重写toString方法,但是我对它的理解还不够,无法在我的代码中实现。

任何指导将不胜感激?如果您需要更多信息,请告诉我。

编辑:添加了我的自定义适配器源代码。

  public class Part_Mstr_Info
  {
    private long part_id;
    private String name, desg, org, dept;

    public Part_Mstr_Info(long part_id, String name, String desg, String org, String dept)
    {
        this.part_id = part_id;
        this.name = name;
        this.desg = desg;
        this.org = org;
        this.dept = dept;
    }
    public long get_part_id() { return part_id; }
    public String get_name() { return name; }
    public String get_desg() { return desg; }
    public String get_org() { return org; }
    public String get_dept() { return dept; }
}

public class CustomAdapter extends ArrayAdapter<Part_Mstr_Info> implements Filterable{
   private List<Part_Mstr_Info> entries;
   private ArrayList<Part_Mstr_Info> orig;
   private Activity activity;
   private ArrayFilter myFilter;

   public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Part_Mstr_Info> entries) {
    super(a, textViewResourceId, entries);
    this.entries = entries;
    this.activity = a;
}

public static class ViewHolder{
    public TextView tv_ac_name;
    public TextView tv_ac_desg;
    public TextView tv_ac_org;
    public TextView tv_ac_dept;
}

@Override
public int getCount(){
      return entries!=null ? entries.size() : 0;
}

@Override
public Part_Mstr_Info getItem(int index) {
    return entries.get(index);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder;
    if (v == null) {
        LayoutInflater vi =
            (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.ac_name_list, null);
        holder = new ViewHolder();
        holder.tv_ac_name = (TextView) v.findViewById(R.id.ac_name);
        holder.tv_ac_desg = (TextView) v.findViewById(R.id.ac_desg);
        holder.tv_ac_org = (TextView) v.findViewById(R.id.ac_org);
        holder.tv_ac_dept = (TextView) v.findViewById(R.id.ac_dept);
        v.setTag(holder);
    }
    else
        holder=(ViewHolder)v.getTag();

    final Part_Mstr_Info custom = entries.get(position);
    if (custom != null) {
        holder.tv_ac_name.setText(custom.get_name());
        holder.tv_ac_desg.setText(custom.get_desg());
        holder.tv_ac_org.setText(custom.get_org());
        holder.tv_ac_dept.setText(custom.get_dept());
    }
    return v;
}

@Override
public Filter getFilter() {
    if (myFilter == null){
        myFilter = new ArrayFilter();
    }
    return myFilter;
}

@Override
public String toString() {
    String temp = getClass().getName();
    return temp;
}

private class ArrayFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (orig == null)
            orig = new ArrayList<Part_Mstr_Info>(entries);
        if (constraint != null && constraint.length() != 0) {
            ArrayList<Part_Mstr_Info> resultsSuggestions = new ArrayList<Part_Mstr_Info>();
            for (int i = 0; i < orig.size(); i++) {
                if(orig.get(i).get_name().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                    resultsSuggestions.add(orig.get(i));
                }
            }

            results.values = resultsSuggestions;
            results.count = resultsSuggestions.size();

        }
        else {
            ArrayList <Part_Mstr_Info> list = new ArrayList <Part_Mstr_Info>(orig);
            results.values = list;
            results.count = list.size();
        }
        return results;
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void publishResults(CharSequence constraint, FilterResults results) {
        clear();
        ArrayList<Part_Mstr_Info> newValues = (ArrayList<Part_Mstr_Info>) results.values;
        if(newValues !=null) {
            for (int i = 0; i < newValues.size(); i++) {
                add(newValues.get(i));
            }
            if(results.count>0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

}

最佳答案

正确的方法是重写ArrayFilter私有(private)类中的Filter#convertResultToString(Object)方法。

如Android文档中所述



在您的情况下,应该是这样的:

private class ArrayFilter extends Filter {
    @Override
    public CharSequence convertResultToString(Object resultValue) {
        return ((Part_Mstr_Info) resultValue).get_name();
    }

添加此方法将使AutoCompleteTextView(在横向 View 中)提供更正提示,而不是对象引用或默认为toString

关于Android AutoCompleteTextView在横向模式下显示对象信息而不是文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12682933/

10-10 20:08