OnItemSelected 不适用于自定义微调器适配器 class

OnItemSelected not working on custom spinner adapter class

我可以用 API 中的数组列表填充微调器。但无法 select 或从微调器填充 selected 项目并显示给用户。

onItemSelected 方法无法获取 selected 项目在微调器中的位置。

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) {

        }
    });

要从微调器中获取选定的值,您应该

String selectedValue = spTypeModel.getSelectedItem().toString();

String selectedValue = String.valueOf(parent.getAdapter().getItem(position));

而不是

String selectedValue = String.valueOf(position);

更新: 将自定义适配器中的 getItem 方法更改为

@Override
public Object getItem(int i) {
    return userNames.get(i);
}

更新2:我看到你的问题,请修改你的代码。

private SpinnerAdapter customAdapter;

private CustomSpinnerAdapter customAdapter;

然后在向适配器添加新数据后添加此行。

eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
customAdapter.notifyDataSetChanged(); // Add this line to notify your adapter about new data

更新 3: 来自您的 xml 文件,因为您的微调器高度是 18dp。

  • 在微调器中,您将填充设置为 4dp(顶部和底部为 8dp)
  • 在自定义文本视图中,您还设置了 4dp 的内边距(顶部和底部为 8dp)
  • 所以你只有等于或小于2dp来显示文字内容,太小了所以你看不到内容。

您可以将微调器高度设置为 wrap_content 或保持当前高度但从微调器或自定义文本视图中删除填充。由你决定。

尝试一下是否有效(它会显示值,而不是位置)

  @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(mContext, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
    }