首页 > 解决方案 > 设置 ArrayAdapter 的 AutoCompleteTextView onClick 的标签

问题描述

我有一个ArrayAdapter我要附加到AutoCompleteTextView的 ArrayAdapter 可以在我每次创建它时包含不同的类。我想将 AutoCompleteTextView 的标签设置为单击视图的 id。我试图创建一个客户 ArrayAdapter 并覆盖getView()但我没有太大的进展。我正在创建至少两个 ArrayAdapter,所以一个OnClickListener在这里对我没有多大好处。

设置 AutoCompleteTextView 标记背后的想法是,我可以将所选值的 id 发送到接收服务器,而不是匹配文本上的所选值。如果有更好的方法可以做到这一点,请告诉我。

提前致谢!

编辑:我修复了它,但我不喜欢我这样做的方式,可能有更好的方法(正如我评论的那样)所以我没有将它标记为已解决

public class FooArrayAdapter<T> extends ArrayAdapter<T> {
private int resourceLayout;
private Context mContext;
private AutoCompleteTextView autoCompleteTextView;

public FooArrayAdapter(Context context, int resource, List<T> items, AutoCompleteTextView autoCompleteTextView) {
    super(context, resource, items);
    this.resourceLayout = resource;
    this.mContext = context;
    this.autoCompleteTextView = autoCompleteTextView;
}

@Override
public View getView(int position, final View convertView, ViewGroup parent) {

    View textView = convertView;
    final ListView dropdownList = (ListView) parent;

    if (textView == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(mContext);
        textView = vi.inflate(resourceLayout, null);
    }

    T item = getItem(position);

    if (item != null) {
        //TODO There should be a better way to do this instead of checking wether this class is of the right instance, use that way
        final TextView adapterTextView = (TextView) textView;
        if(item instanceof Product){
            adapterTextView.setText(((Product) item).getName());
            adapterTextView.setTag(((Product) item).getId());
        }
        if(item instanceof Customer){
            adapterTextView.setText(((Customer) item).getName());
            adapterTextView.setTag(((Customer) item).getId());
        }

        dropdownList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ((View)dropdownList.getParent()).setVisibility(View.GONE); //Hide the container in which the list is displayed
                autoCompleteTextView.clearFocus(); //Remove the focus from this view so a click on the back button hides the keyboard
                String clickedText = adapterTextView.getText().toString();
                Long clickedId =  (Long) adapterTextView.getTag();
                autoCompleteTextView.setText(clickedText);
                autoCompleteTextView.setTag(clickedId);
            }
        });
    }
    return textView;
}

}

标签: android

解决方案


我遇到了同样的问题并找到了解决方法

这是我的模型类(DropDownItemModel.java)

public class DropDownItemModel {

    private int id = -1;
    private String name;

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

    public DropDownItemModel(String name) {
        this.name = name;
    }

    public DropDownItemModel(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}    

在我的活动中,我有一个名为 auto_textview 的 AppCompatAutoCompleteTextView 和我的价值观:

AppCompatAutoCompleteTextView auto_textview;
String row_name;
int    row_id;

您应该创建一个类来实现一些单击选定的侦听器,如下所示:

class ItemSelected implements AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener, View.OnFocusChangeListener{
    
    @Override
    public void onFocusChange(View view, boolean b) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long row) {
        DropDownItemModel itemModel = (DropDownItemModel) auto_textview.getAdapter().getItem(position);
        row_id = itemModel.getId();
        row_name = itemModel.getName();
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long row) {
        DropDownItemModel itemModel = (DropDownItemModel) aut_product_city.getAdapter().getItem(position);
        row_id= itemModel.getId();
        row_name = itemModel.getName();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

最后我们将这个类分配给我们的 AppCompatAutoCompleteTextView (auto_textview):

ItemSelected itemSelected = new ItemSelected();
auto_textview.setOnItemSelectedListener(itemSelected);
auto_textview.setOnFocusChangeListener(itemSelected);
auto_textview.setOnItemClickListener(itemSelected);

现在您可以使用 row_id 作为项目 id 而不是行字符串名称。


推荐阅读