2012-10-02 30 views
8

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

自动完成的基于Android股票数组适配器的其他领域的工作正常。

我必须在自定义适配器中为此做些什么吗?我在SO上只看到一个类似的问题。对这个问题的一个回应是谈论重写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(); 
      } 
     }  

    } 

} 

回答

6

是的,只是重写你的POJO的toString()方法。

public class MyObject { 
    ... 

    @Override 
    public String toString() { 
     return showThisStringOnAdapter; 
    } 
} 
+0

我试过这个,但它不工作..可能是因为我重写过滤器和getview方法也...当我试图调试,执行路径并没有进入toString方法..我会添加更多的细节并编码到我原来的问题澄清... – Sriman

+0

基于我在ArrayAdapter源代码中看到的东西,我觉得我必须在getView方法中做些事情,但不知道究竟需要做什么......正如我所说的,它可以在平板电脑和模拟器上完美工作。只有在手机处于横向模式时,如果没有足够的空间显示自定义视图,我才有... – Sriman

+0

在看到您的CustomAdapter不应该有任何Probs后,看起来像原因是'ConfigChanges',您是不是将手机从纵向旋转到横向?如果是的话,看看[这里](http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android),可以帮助你 – Aprian

3

创建扩展<AutoCompleteTextView />的自定义类。例如:

package lt.irisas.akcija.utils; 

import lt.irisas.akcija.data.ItemsModel; 
import android.content.Context; 
import android.database.Cursor; 
import android.util.AttributeSet; 
import android.widget.AutoCompleteTextView; 

/** Customizing AutoCompleteTextView to return Cursor column info as String 
*/ 
public class CustomAutoCompleteTextView extends AutoCompleteTextView { 

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    /* Overriding this method and returning String type solves the problem */ 
    @Override 
    protected CharSequence convertSelectionToString(Object selectedItem) { 
     Cursor c = (Cursor) selectedItem; 

     return c.getString(c.getColumnIndex("COLUMN_NAME")); 
    } 
} 

但是,而不是<AutoCompleteTextView/>您必须使用您的自定义之一。例如:

<LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <lt.irisas.akcija.utils.CustomAutoCompleteTextView 
      android:id="@+id/search_field" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:singleLine="true" 
      android:imeOptions="actionSearch" > 
     </lt.irisas.akcija.utils.CustomAutoCompleteTextView> 
</LinearLayout> 

希望它有帮助。至少它为我做了。

+0

对我不起作用...原因是我需要返回类Part_Mstr_Info的对象以便我可以从中得到名称,设计,组织和部门。我的汽车完整的建议是两行,每行的名字在第一个和另外三个字段在第二个...所以我不能只是返回一个字符串..我需要返回完整的对象... – Sriman

0

我上次找到答案了。我有同样的问题,似乎自动完成使用横向模式convertToString()方法,而不是newView()和bindView()方法。您必须将所有光标数据返回到一个没有“\ n”的单个字符串中,例如项目将在软键盘上横向显示。

例在AutoCompleteTextViewAdapter,添加方法:

 @Override 
    public CharSequence convertToString(Cursor cursor) { 


     String name = cursor.getString(0); 
     String number = cursor.getString(1); 


     return name+" : "+number; 
    } 

我用我的手机的Xperia U和另一Galaxy Note的测试,它的工作原理。这很简单。 当然,您将根据您希望显示的内容填充此方法。

+0

谢谢...我会在几天内尝试这一点,并报告发生了什么... – Sriman

0

这就是你的答案:

adapter.setCursorToStringConverter(new CursorToStringConverter() { 
    @Override 
    public CharSequence convertToString(Cursor c) { 
     return c.getString(c.getColumnIndexOrThrow("Column")); 
    } 
}); 
10

做的覆盖方法Filter#convertResultToString(Object)在ArrayFilter私有类的正确方式。

由于Android文档中所述

公共的CharSequence convertResultToString(对象resultValue)

从过滤的设置成一个的CharSequence值转换。子类 应该重写此方法来转换它们的结果。默认的 实现返回空值的空字符串或该值的字符串表示形式的默认值 。

在你的情况应该是这样的:

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

添加此方法将允许AutoCompleteTextView提供校正提示(横向视图),而不是对象引用或默认的toString

相关问题