gpt4 book ai didi

android - 如何在单个微调器中动态绑定(bind)多个 json 值?

转载 作者:行者123 更新时间:2023-11-30 00:06:10 27 4
gpt4 key购买 nike

我有一个 json 数组,其中包含要在单个微调器中绑定(bind)的 bullet1 和 bullet 2 值。

[{"id":7,"description":"高度除尘","is_active":true,"createdBy":1,"CreatedOn":"2018-02-20T19:48:16","ModifiedBy ":null,"ModifiedOn":null,"Bullet1":"完成","Bullet2":"未完成"},

{"id":8,"description":"蜘蛛网已移除","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":"Yes","Bullet2":"No"}],"token":null,"ActionName":"GetQuestionnairesByLocation"}]

所以现在动态地有 3 个微调器,并且根据那个,在各自的微调器中应该只看到 2 个值。第一个微调器 - 完成,未完成。第二微调器 - 是,否。第三微调器 - 是,否。但我面临的问题是每个微调器都绑定(bind)了 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;
}

要运行上述解决方案,我需要做什么?

最佳答案

这是因为您在 for 循环之外初始化了您的列表。尝试在 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();

关于android - 如何在单个微调器中动态绑定(bind)多个 json 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48961980/

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