Android AutoCompleteTextView displays object information instead of text in landscape mode

I am using Custom Adapter with AutoCompleteTextView. It works great on emulator and tablet. However, a problem occurs on my phone in landscape mode. The automatic complete prompts displayed in this mode are information about the object, not text. However, when I select an element, the fields are correctly filled with the text in the corresponding fields.

Autocomplete for other fields based on the Android Stock Array Adapter works great.

Do I have to do something for this in my user adapter? I saw only one similar question about SO. One answer to this question was to override the toString method, but I could not figure it out enough to implement in my code.

Any guidance welcome? Please let me know if you need more information.

EDIT: adapter source code added ...

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(); } } } } 
+8
android custom-adapter autocompletetextview
source share
5 answers

The proper way to do this is to override the Filter method # convertResultToString (Object) in your private ArrayFilter class.

As said in the documentation for Android

public CharSequence convertResultToString (Object resultValue)

Converts a value from a filtered set to CharSequence. Subclasses must override this method to convert their results. By default, the implementation returns an empty string for null values ​​or a default value. String representation of the value.

In your case, it should be something like this:

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

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

+10
source share

Yes, just override the toString () method on your POJO.

 public class MyObject { ... @Override public String toString() { return showThisStringOnAdapter; } } 
+6
source share

Create your own class that extends <AutoCompleteTextView /> . For example:

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

But instead of <AutoCompleteTextView/> you should use your own. For example:

  <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> 

Hope this helps. Well, at least that was for me.

+3
source share

I found the answer last time. I had the same problem and it seems that autocomplete uses the convertToString () method in landscape mode instead of the newView () and bindView () method. You must return all the cursor data in one line without "\ n", since the elements will be displayed horizontally above the soft keyboard.

Example in your AutoCompleteTextViewAdapter application, add a method:

  @Override public CharSequence convertToString(Cursor cursor) { String name = cursor.getString(0); String number = cursor.getString(1); return name+" : "+number; } 

I tested it with an Xperia U phone and another Galaxy note, and it works. It is so simple. Of course, you will populate this method depending on what you want to display.

0
source share

this is your answer:

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

All Articles