我可以使用API中的数组列表填充微调器。但是无法从微调器中选择或填充所选项目并向用户显示。
onItemSelected方法无法获得所选项目在微调器中的位置。
CustomSpinnerAdapter.java
public class CustomSpinnerAdapter extends BaseAdapter {
Context context;
List<String> userNames;
LayoutInflater inflter;
public CustomSpinnerAdapter(Context applicationContext, List<String> userNames) {
this.context = applicationContext;
this.userNames = userNames;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return userNames.size();
}
@Override
public Object getItem(int i) {
return userNames.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@NonNull
@SuppressLint({"ViewHolder", "InflateParams"})
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_item, null);
CustomTextView names = (CustomTextView) view.findViewById(R.id.tv_spinner_item);
names.setText(userNames.get(i));
return view;
}
}
我的逻辑在片段中。
private SpinnerAdapter customAdapter;
private List<String> eqIds = new ArrayList<>;
apiInterface = ApiRequest.createService(ApiInterface.class);
Call<EquipmentTypeModel> call = apiInterface.getEquipmentType("application/json", token, id);
call.enqueue(new Callback<EquipmentTypeModel>() {
@Override
public void onResponse(Call<EquipmentTypeModel> call, Response<EquipmentTypeModel> response) {
if (response.isSuccessful()) {
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
}
}
@Override
public void onFailure(Call<EquipmentTypeModel> call, Throwable t) {
}
});
customAdapter = new CustomSpinnerAdapter(mContext, eqIds);
spTypeModel.setAdapter(customAdapter);
spTypeModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, String.valueOf(parent.getAdapter().getItem(position)), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 0 :(得分:0)
尝试一下是否可行(它将向您显示价值,而不是排名)
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}