【问题标题】:Set the adapter to spinner using 3rd party API使用 3rd 方 API 将适配器设置为微调器
【发布时间】:2018-04-21 16:15:34
【问题描述】:

我正在使用this 3rd 方库来搜索微调器。每件事都很好,但问题是当我将适配器设置为微调器时,所以在下拉列表中我得到的是对象引用而不是字符串。 Web 服务使用改造库填充的数组列表。

以下是我的活动:

countriesCustomAdapterInr = new CountriesCustomAdapterInr(getActivity(), R.layout.custom_spinner_items, arrayList,res);
spinner.setAdapter(countriesCustomAdapterInr); 

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                Toast.makeText(getActivity(), ""+arrayList.get(i).getFull_name()+i, Toast.LENGTH_LONG).show();
                            }

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

                            }
                        });  

这是我的模型类:

public class CurrencyConverter {

    @SerializedName("short_name")
    private String short_name;

    @SerializedName("full_name")
    private String full_name;

    @SerializedName("flag")
    private String flag;

    public CurrencyConverter(String short_name, String full_name, String flag) {
        this.short_name = short_name;
        this.full_name = full_name;
        this.flag = flag;
    }

    public CurrencyConverter() {}

    public String getShort_name() {
        return short_name;
    }

    public void setShort_name(String short_name) {
        this.short_name = short_name;
    }

    public String getFull_name() {
        return full_name;
    }

    public void setFull_name(String full_name) {
        this.full_name = full_name;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

}

我的适配器:

public class CountriesCustomAdapterInr extends ArrayAdapter<String> {

    private ArrayList data;
    public Resources res;
    private LayoutInflater inflater;

    public CountriesCustomAdapterInr(
            FragmentActivity activitySpinner,
            int textViewResourceId,
            ArrayList objects,
            Resources resLocal)
    {
        super(activitySpinner, textViewResourceId, objects);

        data     = objects;
        res      = resLocal;
        inflater = (LayoutInflater) activitySpinner.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    private View getCustomView(int position, View convertView, ViewGroup parent) {

        View row = inflater.inflate(R.layout.custom_spinner_items, parent, false);

        CurrencyConverter tempValues = null;
        tempValues = (CurrencyConverter) data.get(position);

        TextView label        = (TextView)row.findViewById(R.id.textView_short_name);
        TextView sub          = (TextView)row.findViewById(R.id.textView_full_name);
        ImageView flag = (ImageView)row.findViewById(R.id.imageView_flag);
        TextView line = (TextView)row.findViewById(R.id.row);
        if(position==0){
            label.setText("INR");
            sub.setText("India");
            flag.setImageResource(R.drawable.ind);
            line.setVisibility(View.GONE);
        }
        else
        {
            // Set values for spinner each row
            label.setText(tempValues.getShort_name());
            sub.setText(tempValues.getFull_name());
            Picasso.with(getContext()).load("http://currencyconvertor.ccube9projects.com/uploads/country_flag/"+ tempValues.getFlag()).into(flag);
        }


        return row;
    }

我还附上了图片以进行更多说明。请问有人可以帮忙吗?我浪费了我的 2 天,但我没有得到任何答案。请帮忙。

【问题讨论】:

    标签: android arraylist android-arrayadapter android-spinner onitemselectedlistener


    【解决方案1】:

    简单的答案是您无法使用此库设置自己的自定义适配器。如果您查看他的 SearchableListDialog (https://github.com/miteshpithadiya/SearchableSpinner/blob/master/searchablespinnerlibrary/src/main/java/com/toptoche/searchablespinnerlibrary/SearchableListDialog.java#L160) 的第 160 行,它总是将适配器设置为仅用于字符串的简单数组适配器。

    如果您想要一种方法来实际实现您想要的,您可以将他的库复制到您的代码中并修改您需要的内容。例如,将 SearchableListDialog 中的第 160 行更改为:

    listAdapter = new CountriesCustomAdapterInr(getContext(), R.layout.custom_spinner_items, items, getResources());
    

    您还需要将适配器的构造函数更改为:

    public CountriesCustomAdapterInr(
        Context context,
        int textViewResourceId,
        List<CurrencyConverter> objects,
        Resources resLocal) {
        super(context, textViewResourceId, objects);
    
        data = objects;
        res = resLocal;
        inflater = (LayoutInflater) context.getSystemService(Context
            .LAYOUT_INFLATER_SERVICE);
    }
    

    最后,您需要在 CurrencyConverter 中覆盖 toString 才能使搜索正常工作:

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

    【讨论】:

    • 实际上我在自定义项目列表中有 2 个文本和 1 个图像,所以我不能使用 toString()。如果我只有一个文本,那么我可以使用 toString() 但自定义设计我不能使用。还有其他解决方案吗?
    • toString() 只是为了让搜索过滤器工作。更改库的 SearchableListDialog 将为您提供所需的自定义适配器。
    • 我不知道你在问什么。上面的代码工作并生成你想要的。
    • 你好@tim.paetz 我明白你的意思。我得到了我想要的结果,但仍然有一个问题,例如在搜索视图小部件中我搜索伦敦,我没有得到过滤为伦敦的结果,但是当单击列表视图时,结果显示项目为 London.It就像项目的位置发生了变化,但列表没有显示可搜索的结果。你能建议我有什么问题吗?请帮帮我。
    • 您应该用您的代码发布另一个问题以及问题所在。
    猜你喜欢
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多