代码之家  ›  专栏  ›  技术社区  ›  Rakshit Sorathiya

OnItemSelected无法处理自定义微调器适配器类

  •  0
  • Rakshit Sorathiya  · 技术社区  · 6 年前

    MSelected方法无法获取选定项在微调器中的位置。

    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) {
    
            }
        });
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Son Truong    6 年前

    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);
    }
    

    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: 因为微调器的高度是18dp。

    • 在自定义文本视图中,您还可以设置padding 4dp(对于top和bot将是8dp)

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

        2
  •  0
  •   Joaquín    6 年前

    如果这样做有效的话就试试看(它会显示你的价值,而不是位置)

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