我试过这个答案 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);
答案 0 :(得分:0)
而不是:
SpinnerItems spinnerItems=spinnerItemsList.get(position);
使用getItemAtPosition(position):
SpinnerItems spinnerItems=spinnerItemsList.getItemAtPosition(position);
答案 1 :(得分:0)
而不是:
Toast.makeText(getContext(),String.valueOf(spinnerItems),Toast.LENGTH_LONG).show();
&#13;
使用此..
Toast.makeText(getApplicationContext(),String.valueOf(name),Toast.LENGTH_LONG).show();
&#13;