使用第三方API将适配器设置为微调器

时间:2017-11-08 20:12:54

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

我正在使用this第三方库来搜索可调整的微调器。每件事都很好但问题是当我将适配器设置为微调器时所以在下拉列表中我得到的是对象引用而不是字符串。由使用改造库的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天但我没有得到任何答案。请帮忙。

enter image description here

1 个答案:

答案 0 :(得分:0)

简单的答案是您无法使用此库设置自己的自定义适配器。如果你查看他的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;
}