gpt4 book ai didi

android - 如何使全局对象的数据适应微调器

转载 作者:行者123 更新时间:2023-11-30 00:42:31 24 4
gpt4 key购买 nike

我有我的 GlobalClass extends 应用程序类。我想将全局对象数据绑定(bind)到微调器。这是我的 GlobalClass

public class GlobalClass extends Application {

private List<ProjectType> projectTypes;

public List<ProjectType> getProjectTypes() {
return projectTypes;
}

public void setProjectTypes(List<ProjectType> projectTypes) {
this.projectTypes = projectTypes;
}
}

Pojo class

public class ProjectType implements Serializable {
private Integer projectTypeId;
private String typeName;
private Integer peckOrder;

//getter and setter here

然后我使用 volley 从服务器获取响应并使用 GSON 进行解析并将响应设置为 GlobalClass这里是代码

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, getString(R.string.TEST_projectTypeURL), null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
final GlobalClass globalVariable = (GlobalClass) getApplicationContext();
Gson gson = new Gson();
List<ProjectType> projectTypes = gson.fromJson(String.valueOf(response),List.class);
globalVariable.setProjectTypes(projectTypes);
}
}

最后在另一个 Activity 类中,我使用微调器绑定(bind)来自 GlobalClass 对象的数据

globalVariable = (GlobalClass) getApplicationContext();
List<String> projectTypeList = new ArrayList<>();

ArrayList<ProjectType> projectTypesCollection = new ArrayList<ProjectType>(globalVariable.getProjectTypes());

for (ProjectType projectType: projectTypesCollection) {

projectTypeList.add(projectType.getTypeName());
}

prjtTypeSpinner = (Spinner)findViewById(R.id.spn_prjt_type);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,projectTypeList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
prjtTypeSpinner.setAdapter(dataAdapter);

尝试上述代码时出现错误“com.google.gson.internal.LinkedTreeMap cannot be cast to the pojo.ProjectType class”

这里是对象的返回值,

[{projectTypeId=3.0, typeName=ALS, peckOrder=220.0}, {projectTypeId=2.0, typeName=ALB, peckOrder=210.0}, {projectTypeId=1.0, typeName=CL, peckOrder=200.0}, {projectTypeId=7.0, typeName=ACG, peckOrder=40.0}, {projectTypeId=6.0, typeName=ACS, peckOrder=30.0}, {projectTypeId=5.0, typeName=ACB, peckOrder=20.0}, {projectTypeId=4.0, typeName=CC, peckOrder=10.0}]

我想要微调器中的 typeName。提前致谢。

最佳答案

我终于解决了这个问题,

Arrays.asList(gson.fromJson(response, ProjectType[].class))

当我尝试代码时,我的错误得到解决,这段代码运行良好

关于android - 如何使全局对象的数据适应微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42340930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com