如何使全球对象的数据适应微调 [英] how to adapt the data from global object to spinner
问题描述
我有我的GlobalClass extends
应用程序类。我想将全局对象数据绑定到微调器。这是我的 GlobalClass
public class GlobalClass extends Application {
私人列表< ProjectType> projectTypes;
公共列表< ProjectType> getProjectTypes(){
return projectTypes;
}
public void setProjectTypes(List< ProjectType> projectTypes){
this.projectTypes = projectTypes;
$ / code $ / pre
$ b
Pojo class p>
public class ProjectType实现Serializable {
private Integer projectTypeId;
private String typeName;
private Integer peckOrder;
// getter and setter here
我得到服务器使用volley和parse使用 GSON
并将响应设置为 GlobalClass
这里的代码
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,getString(R.string.TEST_projectTypeURL),null,new Response.Listener< JSONArray> (){
@Override $ b $ public void onResponse(JSONArray response){
GlobalClass globalVariable =(GlobalClass)getApplicationContext();
Gson gson = new Gson();
List< ProjectType> projectTypes = gson.fromJson(String.valueOf(response),List.class);
globalVariable.setProjectTypes(projectTypes);
}
}
最后在另一个活动类iam中使用微调来绑定来自GlobalClass对象的数据
globalVari able =(GlobalClass)getApplicationContext();
列表< String> projectTypeList = new ArrayList<>();
ArrayList< ProjectType> projectTypesCollection = new ArrayList< ProjectType>(globalVariable.getProjectTypes()); (ProjectType projectType:projectTypesCollection){
projectTypeList.add(projectType.getTypeName());
}
prjtTypeSpinner =(微调)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不能转换为pojo.ProjectType类。
这里的对象返回值,
[{projectTypeId = 3.0,typeName = ALS,peckOrder = 220.0},{projectTypeId = 2.0,typeName = ALB,peckOrder = 210.0},{projectTypeId = 1.0 projectNameId = 5.0,typeName = ACB,peckOrder = 200.0} 20.0},{projectTypeId = 4.0,typeName = CC,peckOrder = 10.0}]
I需要微调器中的 typeName
。
解决方案我终于解决了这个问题,
Arrays.asList(gson.fromJson(response,ProjectType []。class))
当我尝试代码时,我的错误得到解决,此代码很好地工作
I have my GlobalClass extends
Application class. I want to bind the global object data to spinner. Here is my 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
And I get the response from the server using volley and parse using GSON
and set the response to GlobalClass
here the code
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);
}
}
And finally in another activity class iam using spinner to bind the data from the GlobalClass object
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);
While trying the above code I am getting an error "com.google.gson.internal.LinkedTreeMap cannot be cast to the pojo.ProjectType class"
here the object return value,
[{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}]
I want the typeName
in spinner. Thanks in advance.
解决方案 I finally solve this problem,
Arrays.asList(gson.fromJson(response, ProjectType[].class))
when I try the code my error get resolved, this code work well
这篇关于如何使全球对象的数据适应微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!