当我从列表数据加载数据到微调器时,它可以工作。但是当我为微调器创建一个适配器时,它不起作用。我的问题是什么?
的活动:
final FieldOfficerApiInterface apiService=OperatingApiClient.getClient().create(FieldOfficerApiInterface.class);
Call<FieldOfficerApiResponse> call = apiService.getAllEOByBranchID(branchid);
call.enqueue(new Callback<FieldOfficerApiResponse>() {
@Override
public void onResponse(Call<FieldOfficerApiResponse> call, Response<FieldOfficerApiResponse> response) {
List<FieldOfficer> fieldofficerlist=response.body().getFieldofficerlist();
Log.d(TAG,"Number of Fieldofficerlist received: "+fieldofficerlist.size());
//*********DROPDOWN******************************************************
final Spinner spnrFieldOfficer=(Spinner)findViewById(R.id.spinnerFieldOfficer);
CustomSpinnerAdapter adapter=new CustomSpinnerAdapter(getApplicationContext(),fieldofficerlist);
spnrFieldOfficer.setAdapter(adapter);
//*********END DROPDOWN******************************************************
}
@Override
public void onFailure(Call<FieldOfficerApiResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG,t.toString());
}
});
CustomSpinnerAdapter:
public class CustomSpinnerAdapter extends BaseAdapter {
private List<FieldOfficer> fieldOfficers;
private Context context;
LayoutInflater inflter;
public CustomSpinnerAdapter(Context context, List<FieldOfficer> fieldOfficers) {
this.context = context;
this.fieldOfficers = fieldOfficers;
this.inflter = (LayoutInflater.from(context));
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView=inflter.inflate(R.layout.custom_spinner_items,null);
TextView tv=(TextView)convertView.findViewById(R.id.textView2);
tv.setText(fieldOfficers.get(position).getFoname());
return convertView;
}
}
没有自定义适配器:
//*********DROPDOWN******************************************************
ArrayAdapter<FieldOfficer> adapter;
adapter = new ArrayAdapter<FieldOfficer>(getApplicationContext(), android.R.layout.simple_selectable_list_item,fieldofficerlist); //getApplication()
//setting adapter to spinner
spnrFieldOfficer.setAdapter(adapter);
//*********END DROPDOWN******************************************************
答案 0 :(得分:2)
更改您的getCount() 像这样
@Override
public int getCount() {
if (fieldOfficers!= null) {
return fieldOfficers.size();
}
return 0;
}
答案 1 :(得分:0)
微调器将调用你的getCount()来确定列表中有多少项,在你的实现中你告诉它列表总是空的。
将您的getCount()更改为以下
@Override
public int getCount() {
if (fieldOfficers!= null) {
return fieldOfficers.size();
}
return 0;
}