Java 微调器值未显示正确的值(改装)
第一次尝试使用微调器。 微调器值并没有显示确切的值,而是显示名称空间 我正在通过改造获取数据。在那之后,我不知道该怎么做,但在互联网代码的帮助下尝试了Java 微调器值未显示正确的值(改装),java,android,retrofit,spinner,Java,Android,Retrofit,Spinner,第一次尝试使用微调器。 微调器值并没有显示确切的值,而是显示名称空间 我正在通过改造获取数据。在那之后,我不知道该怎么做,但在互联网代码的帮助下尝试了 final Call<List<ModelErrorType>> errorTypeList = CustomersAPI.getSpinnerErrrorService().errorTypeList(); errorTypeList.enqueue( new Callback<List<Mo
final Call<List<ModelErrorType>> errorTypeList = CustomersAPI.getSpinnerErrrorService().errorTypeList();
errorTypeList.enqueue( new Callback<List<ModelErrorType>>() {
@Override
public void onResponse(Call<List<ModelErrorType>> call, Response<List<ModelErrorType>> response) {
// Log.w("2.0 getFeed >> ",new GsonBuilder().setPrettyPrinting().create().toJson(response));
Log.d( "JSON LIST", new GsonBuilder().setPrettyPrinting().create().toJson( response ) );
Toast.makeText( getContext(), response.toString(), Toast.LENGTH_SHORT ).show();
List<ModelErrorType> list = new ArrayList<ModelErrorType>();
ArrayAdapter<ModelErrorType> dataAdapter = new ArrayAdapter<ModelErrorType>
(getContext(), android.R.layout.simple_spinner_item, list);
list.addAll(response.body());
dataAdapter.notifyDataSetChanged();
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
我的模型如下,我是在Json到Pojo的帮助下得到的
package com.example.iqhut;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import androidx.annotation.NonNull;
public class ModelErrorType {
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<ModelErrorTypeBack> data = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<ModelErrorTypeBack> getData() {
return data;
}
public void setData(List<ModelErrorTypeBack> data) {
this.data = data;
}
}
改装代码如下
public static SpinnerErrrorService spinnerErrrorService= null;
public static SpinnerErrrorService getSpinnerErrrorService() {
if (spinnerErrrorService == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl( url )
.addConverterFactory( GsonConverterFactory.create() )
.build();
spinnerErrrorService = retrofit.create( SpinnerErrrorService.class );
}
return spinnerErrrorService;
}
public interface SpinnerErrrorService {
@GET("/iqhut/api/errorcategory.php?")
Call<List<ModelErrorType>> errorTypeList();
}
公共静态SpinnerErrorService SpinnerErrorService=null;
公共静态SpinnerErrorService GetSpinnerErrorService(){
if(SpinnerErrorService==null){
改装改装=新改装.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
SpinnerErrorService=改装.create(SpinnerErrorService.class);
}
返回喷丝板服务;
}
公共接口SpinnerErrorService{
@获取(“/iqhut/api/errorcategory.php?”)
调用errorTypeList();
}
下拉列表显示模型名称空间com.example.iqhut。ModelErrorType@52453b这里的响应主体是一个ModelErrorType的列表,您试图将其添加到微调器数据列表中,但微调器只接受字符串列表 因此,您需要做的是循环响应主体中的所有ModelErrorType对象,循环ModelErrorTypeBack并逐个添加它们 所以试着改变这一行
list.addAll(response.body())代码>
到
add(errorTypeBack.getName());显示错误,表示需要类型。提供的模型错误类型StringChange将列表和dataAdapter类型从ModelErrorType更改为StringType您能再帮我一点忙吗?类似于如何选择和获取名称的ID值。要选择和获取名称的值,您需要为微调器创建自定义适配器,请选中以下链接:
package com.example.iqhut;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import androidx.annotation.NonNull;
public class ModelErrorTypeBack {
@SerializedName("id")
@Expose
private String id;
@SerializedName("Name")
@Expose
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NonNull
@Override
public String toString() {
return name;
}
}
public static SpinnerErrrorService spinnerErrrorService= null;
public static SpinnerErrrorService getSpinnerErrrorService() {
if (spinnerErrrorService == null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl( url )
.addConverterFactory( GsonConverterFactory.create() )
.build();
spinnerErrrorService = retrofit.create( SpinnerErrrorService.class );
}
return spinnerErrrorService;
}
public interface SpinnerErrrorService {
@GET("/iqhut/api/errorcategory.php?")
Call<List<ModelErrorType>> errorTypeList();
}
for(ModelErrorType errorType : response.body()){
for(ModelErrorTypeBack errorTypeBack : errorType.getData){
list.add(errorTypeBack.getName());
}
}