Android OnItemSelected不在自定义微调器适配器类上工作
我可以用API中的数组列表填充微调器。但无法从微调器中选择或填充所选项目并显示给用户 onItemSelected方法无法获取选定项在微调器中的位置 CustomSpinnerAdapter.javaAndroid OnItemSelected不在自定义微调器适配器类上工作,android,android-layout,android-spinner,android-xml,Android,Android Layout,Android Spinner,Android Xml,我可以用API中的数组列表填充微调器。但无法从微调器中选择或填充所选项目并显示给用户 onItemSelected方法无法获取选定项在微调器中的位置 CustomSpinnerAdapter.java public class CustomSpinnerAdapter extends BaseAdapter { Context context; List<String> userNames; LayoutInflater inflter; public CustomSpinnerA
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;
}
}
公共类CustomSpinnerAdapter扩展了BaseAdapter{
语境;
列出用户名;
更平坦的过滤器;
公共CustomSpinerAdapter(上下文应用程序上下文,列出用户名){
this.context=applicationContext;
this.userNames=用户名;
inflter=(LayoutInflater.from(applicationContext));
}
@凌驾
public int getCount(){
返回userNames.size();
}
@凌驾
公共对象getItem(int i){
返回用户名。get(i);
}
@凌驾
公共长getItemId(int i){
返回0;
}
@非空
@SuppressLint({“视图持有者”、“充气参数”})
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
视图=过滤器充气(R.layout.custom\u spinner\u项目,空);
CustomTextView名称=(CustomTextView)view.findViewById(R.id.tv\u微调器\u项);
names.setText(userNames.get(i));
返回视图;
}
}
我的逻辑支离破碎
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) {
}
});
专用SpinnerAdapter自定义适配器;
私有列表eqIds=新的ArrayList;
apiInterface=ApiRequest.createService(apiInterface.class);
Call Call=apinterface.getEquipmentType(“application/json”,token,id);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
addAll(response.body().getData().getEquipmentList().getEquipmentId());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
customAdapter=新的CustomSpinerAdapter(mContext,eqIds);
spTypeModel.setAdapter(customAdapter);
spTypeModel.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Toast.makeText(mContext,String.valueOf(parent.getAdapter().getItem(position)),Toast.LENGTH_SHORT.show();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
要从微调器获取所选值,您应该
String selectedValue = spTypeModel.getSelectedItem().toString();
或
而不是
String selectedValue = String.valueOf(position);
更新:将自定义适配器中的getItem
方法更改为
@Override
public Object getItem(int i) {
return userNames.get(i);
}
更新2:我看到了您的问题,请更改代码
private SpinnerAdapter customAdapter;
到
然后在向适配器添加新数据后添加此行
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
customAdapter.notifyDataSetChanged(); // Add this line to notify your adapter about new data
从xml文件更新3:,因为微调器高度为18dp
- 在微调器中,将填充设置为4dp(顶部和底部为8dp)
- 在自定义文本视图中,您还可以设置填充4dp(顶部和底部为8dp)
- 所以你只有等于或小于2dp的文本内容来显示,这就是为什么你看不到内容的原因
wrap\u content
或保持当前高度,但从微调器或自定义文本视图中删除填充。这取决于您。如果这有效,请尝试(它将向您显示值,而不是位置)
@覆盖
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Toast.makeText(mContext,parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}
谢谢@Son Truong的帮助,但它还不起作用。您能否将此代码添加到OnItem Selected
方法中,并在单击项目时告知结果<代码>Toast.makeText(mContext、eqIds.get(position)、Toast.LENGTH\u SHORT.show()代码>当前listview未显示任何数据,是吗?否,API中的数据已成功添加到微调器项中。单击微调器时,我还可以查看其内部的列表。这里的问题是onItemSelected微调器不工作。让我们来看看。onItemSelected不工作,因此没有toast显示。
private CustomSpinnerAdapter customAdapter;
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
customAdapter.notifyDataSetChanged(); // Add this line to notify your adapter about new data
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}