开发者

Convert a selection in Android Autocomplete to a String

开发者 https://www.devze.com 2023-04-04 19:10 出处:网络
I have just implemented an autocompletion for a textfield using an online webservice, based on this answer on Stackoverflow:

I have just implemented an autocompletion for a textfield using an online webservice, based on this answer on Stackoverflow: ArrayAdapter is updated late from Webservice in AutoCompleteTextAdapter

Using an ArrayAdapter<User> implements Filterable, I have managed that the autocompletion suggests me entries as intended. User is a Java Bean that contains information which is presented in the suggestion (age, name, ...).

When I select a suggestion, the Autocomplete field is filled with the 'wrong' data - using the toString()method, instead of the 'name' property of the bean.

My Question is: Can I override (in the Adapter) a method which will allow me to specify how to convert 开发者_运维问答the bean so that the correct property is returned for the AutoCompleteTextView? (Ideally, User.toString() should not be changed)

Thx!


There is no need subclassing AutoCompleteTextViewand override the convertSelectionToStringmethod. The same thing is achievable by overriding the convertResultToStringmethod in your custom Filter in (your already subclassed) ArrayAdapter.

I had the same problem: custom objects in my ArrayAdapter whose toString() implementation wasn't something I could control. I implemented the method like this:

// In custom Filter implementation

@Override
public CharSequence convertResultToString(Object result) {
   if(result instanceof MyCustomClass) {
      return ((MyCustomClass) result).getAttribute("name");
   }

   return super.convertResultToString(result);
}


The search results depend on what the data's toString() returns. In your case you need to return name field in the toString() implementation.


I have found another way:

The method convertSelectionToString(Object selectedItem) in AutoCompleteTextView can be overridden by subclasses to allow for custom conversions. This way, no adjustment to the toString() method is required.

This - it seems to me - has the advantage that the Filter can return not just a list of Strings but a list of custom objects, which can be used by getView(int position, View convertView, ViewGroup parent) of the adapter to construct "richer" suggestions.

The obvious disadvantage is that it requires subclassing AutoCompleteTextView for every Filterresult type whose toString() method shall not be modified.

@Override
protected CharSequence convertSelectionToString(Object selectedItem) {
    if(selectedItem instanceof User){
        User u = (User) selectedItem;
        return u.getUsername();
    } else {
        return super.convertSelectionToString(selectedItem);
    }
}

Any comments on this?


If you subclass your own adapter from SimpleCursorAdapter, you can set a CursorToStringConverter on the Adapter in the constructor.

private class AutoCompleteAdapter extends SimpleCursorAdapter {

    public AutoCompleteAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        /* Other setup code here  */

        setCursorToStringConverter(new CursorToStringConverter() {
            @Override
            public CharSequence convertToString(Cursor item) {
                return item.getString(item.getColumnIndex(DESIRED_COLUMN_NAME));
            }
        });
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消