Android AutoCompleteTextView 在横向模式下显示对象信息而不是文本 [英] Android AutoCompleteTextView shows object information instead of text in landscape mode

查看:90
本文介绍了Android AutoCompleteTextView 在横向模式下显示对象信息而不是文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a Custom Adapter with AutoCompleteTextView. It works fine on the emulator and my tablet. However, there is an issue on my phone in landscape mode. The auto complete hints being shown in this mode are object info rather than text. However, when I select any item the fields gets populated correctly with text in the respective fields.

基于 Android Stock Array Adapter 的其他字段的自动完成工作正常.

Auto Complete for other fields that are based on Android Stock Array Adapter works fine.

我必须在我的自定义适配器中为此做些什么吗?我在 SO 上只看到一个类似的问题.该问题的一个回答是谈论覆盖 toString 方法,但我无法理解它以在我的代码中实现.

Do I have to do something for this in my Custom Adapter? I saw only one similar question on SO. One response for that question was talking about overriding the toString method but I couldn't understand it enough to implement in my code.

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

Any guidance will be greatly appreciated? Please let me know if you need more information.

添加了我的自定义适配器源代码....

Added my custom adapter source code....

  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();
            }   
        }    

    }

}

推荐答案

正确的做法是覆盖方法 Filter#convertResultToString(Object) 在您的 ArrayFilter 私有类中.

The right way to do it is overriding the method Filter#convertResultToString(Object) in your ArrayFilter private class.

如 Android 文档中所述

As said in the Android documentation

public CharSequence convertResultToString (Object resultValue)

public CharSequence convertResultToString (Object resultValue)

将过滤后的集合中的值转换为 CharSequence.子类应该覆盖这个方法来转换他们的结果.默认的实现为空值或默认值返回一个空字符串值的字符串表示.

Converts a value from the filtered set into a CharSequence. Subclasses should override this method to convert their results. The default implementation returns an empty String for null values or the default String representation of the value.

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

In your case it should be something like that:

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

添加此方法将允许 AutoCompleteTextView 提供更正提示(在横向视图中)而不是对象引用或默认为字符串

Adding this method will allow the AutoCompleteTextView to provide corrects hints (in landscape view) instead of object references or default toString

这篇关于Android AutoCompleteTextView 在横向模式下显示对象信息而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆