how to adapt the data from global object to spinne

2019-07-27 03:02发布

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.

2条回答
成全新的幸福
2楼-- · 2019-07-27 03:31

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

查看更多
疯言疯语
3楼-- · 2019-07-27 03:39

I think you have problem with this codes

List<ProjectType> projectTypes = gson.fromJson(String.valueOf(response),List.class);

you need to change to:

List<ProjectType> projectTypes = gson.fromJson(new Gson().toJson(response),new TypeToken<List<ProjectType>>() {
            }.getType());
查看更多
登录 后发表回答