微调器下拉视图显示参考

时间:2017-11-03 12:13:26

标签: android spinner android-arrayadapter android-spinner custom-adapter

我试过这个答案 Android spinner showing object reference instead of string 但对我没用。

我正在使用'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'

我正面临有关spinnerdropdown的问题。 我为微调器制作了CustomAdapter但在dropdownview中显示了参考 像

  

spinneritems @ f1312a01

这是我的适配器代码

public class CustomAdapter extends ArrayAdapter<SpinnerItems> {

    LayoutInflater inflater;
    Activity context;

    private List<SpinnerItems> spinnerItemsList;


    public CustomAdapter(@NonNull Activity context, @LayoutRes int resource,List<SpinnerItems> spinnerItemsList) {
        super(context, resource,spinnerItemsList);
        this.context=context;

        this.spinnerItemsList=spinnerItemsList;

        //inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

        View row = convertView;
        if(row == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.simple_item_spiner, parent, false);
        }
        SpinnerItems spinnerItems=spinnerItemsList.get(position);
        String name=spinnerItems.getName();
        TextView label=(TextView)row.findViewById(R.id.spinnertitle);
        label.setText(spinnerItemsList.get(position).getName());

        return row;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View row = convertView;

        Log.d("drop","drp");
            LayoutInflater inflater = context.getLayoutInflater();
            row = inflater.inflate(R.layout.simple_item_spiner, parent, false);

        SpinnerItems spinnerItems=spinnerItemsList.get(position);
        String name=spinnerItems.getName();
        TextView label=(TextView)row.findViewById(R.id.spinnertitle);
        label.setText(spinnerItemsList.get(position).getName());
        Toast.makeText(getContext(),String.valueOf(spinnerItems),Toast.LENGTH_LONG).show();
        return  row;
    }


}

我打印了Toast并登录dropdown但似乎没有进入getdropdownview方法

这是主要课程:

  for (int i = 0; i < response.length(); i++) {
                SpinnerItems spinnerItems=new SpinnerItems(response.getJSONObject(i).getString("id"),response.getJSONObject(i).getString("name"));
                cityList.add(spinnerItems);
            }
            cityAdapter = new CustomAdapter(getActivity(),R.layout.simple_item_spiner,cityList);
            city.setAdapter(cityAdapter);

2 个答案:

答案 0 :(得分:0)

而不是:

SpinnerItems spinnerItems=spinnerItemsList.get(position);

使用getItemAtPosition(position):

SpinnerItems spinnerItems=spinnerItemsList.getItemAtPosition(position);

答案 1 :(得分:0)

而不是:

&#13;
&#13;
Toast.makeText(getContext(),String.valueOf(spinnerItems),Toast.LENGTH_LONG).show();
&#13;
&#13;
&#13;

使用此..

&#13;
&#13;
Toast.makeText(getApplicationContext(),String.valueOf(name),Toast.LENGTH_LONG).show();
&#13;
&#13;
&#13;