Android AutoCompleteTextView shows object information instead of text in landscape mode

时光怂恿深爱的人放手 提交于 2019-12-04 01:20:30

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

As said in the Android documentation

public CharSequence convertResultToString (Object resultValue)

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

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

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

public class MyObject {
    ...

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

Create custom class which 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 must use your custom one. 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 it helps. Well at least it did for me.

I've found the answer last time. I had the same issue and it seems that autocomplete uses convertToString() method in landscape mode instead of newView() and bindView() method. You have to return all your cursor data into one single String without "\n" like the items will be displayed horizontaly over the softkeyboard.

Example in your AutoCompleteTextViewAdapter, add the method :

        @Override
    public CharSequence convertToString(Cursor cursor) {


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


        return name+" : "+number;
    }

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

this is your answer :

adapter.setCursorToStringConverter(new CursorToStringConverter() {
    @Override
    public CharSequence convertToString(Cursor c) {
        return c.getString(c.getColumnIndexOrThrow("Column"));
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!