如何在单个微调器中动态绑定多个json值?

时间:2018-02-24 10:50:01

标签: android json android-spinner

我有一个json数组,其中bullet1和bullet 2值要绑定在一个微调器中。

[{“id”:7,“description”:“Height dusting”,“is_active”:true,“createdBy”:1,“CreatedOn”:“2018-02-20T19:48:16”,“ModifiedBy” “:null,”ModifiedOn“:null,”Bullet1“:”完成“,”Bullet2“:”未完成“},

{“id”:8,“description”:“Cobwebs removed”,“is_active”:true,“createdBy”:1,“CreatedOn”:“2018-02-20T19:48:17”,“ModifiedBy” :NULL, “ModifiedOn”:NULL, “Bullet1”: “是”, “文档bullet2”: “否”},

{“id”:9,“description”:“侧墙清理”,“is_active”:true,“createdBy”:1,“CreatedOn”:“2018-02-20T19:48:17”,“ModifiedBy” “:NULL,” ModifiedOn “:NULL,” Bullet1 “:” 是”, “文档bullet2”: “否”}], “令牌”:NULL, “ActionName”: “GetQuestionnairesByLocation”}]

所以现在动态地有3个微调器,并且据说在相应的微调器中只能看到2个值。 第一个微调器 - 完成,未完成。 第二个微调 - 是的,没有。 第三个旋转器 - 是的,没有。 但我遇到的问题是每个微调器都有6个值。(完成,未完成,是,否,是,否)

代码:

ArrayList<UserFormModel> arrayList = new ArrayList<>();
List<String> list = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray("ResponseArray");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = 
                        jsonArray.getJSONObject(i);
                        UserFormModel userFormModel = new 
                        UserFormModel();
                        userFormModel.setId(jsonObject1.getInt("id"));
                        userFormModel.setDescription
                        (jsonObject1.getString("description"));
                        list.add(jsonObject1.getString("Bullet1"));
                        list.add(jsonObject1.getString("Bullet2"));
                        userFormModel.setBulletsList(list);
                        arrayList.add(userFormModel);

                    }
                    UserFormsAdapter locationMasterAdapter = new 
                    UserFormsAdapter(GetQuestions.this, arrayList);
                    listView.setAdapter(locationMasterAdapter);
                    locationMasterAdapter.notifyDataSetChanged();

适配器类:

private ArrayList<UserFormModel> userFormModelArrayList;
Activity home;

public UserFormsAdapter(Activity home, ArrayList<UserFormModel> 
userFormModelArrayList) {
    this.home = home;
    this.userFormModelArrayList = userFormModelArrayList;
}
 @Override
public View getView(final int position, View convertView, ViewGroup 
parent) {
    View grid;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) 
        home.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.get_questions_row, null);
    } else {
        grid = (View) convertView;
    }
    final UserFormModel userFormModel = 
    userFormModelArrayList.get(position);
    TextView textDesc = (TextView) grid.findViewById(R.id.textDesc);
    Spinner spinner = (Spinner) grid.findViewById(R.id.chooseBullets);
    String desc = userFormModel.getDescription();
    textDesc.setText(desc);

    CustomDropdownAdapter customDropdownAdapter = new 
    CustomDropdownAdapter(home, userFormModel.getBulletsList());
    spinner.setAdapter(customDropdownAdapter);
    return grid;
}

我需要为上面的解决方案做什么才能运行?

1 个答案:

答案 0 :(得分:1)

这是beacuase你在for循环之外初始化你的List。尝试在for循环中初始化列表。

ArrayList<UserFormModel> arrayList = new ArrayList<>();
List<String> list;\\don't initialize the list here just declare the list.
JSONArray jsonArray = jsonObject.getJSONArray("ResponseArray");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = 
                        jsonArray.getJSONObject(i);
                        list = new ArrayList<>();\\try to initialize your list here.
                        UserFormModel userFormModel = new 
                        UserFormModel();
                        userFormModel.setId(jsonObject1.getInt("id"));
                        userFormModel.setDescription
                        (jsonObject1.getString("description"));
                        list.add(jsonObject1.getString("Bullet1"));
                        list.add(jsonObject1.getString("Bullet2"));
                        userFormModel.setBulletsList(list);
                        arrayList.add(userFormModel);

                    }
                    UserFormsAdapter locationMasterAdapter = new 
                    UserFormsAdapter(GetQuestions.this, arrayList);
                    listView.setAdapter(locationMasterAdapter);
                    locationMasterAdapter.notifyDataSetChanged();