繁体   English   中英

使用ListView适配器中的CheckBoxes解析JSONObject中的布尔值

[英]Parsing boolean values from a JSONObject with CheckBoxes in a ListView Adapter

我有一个带有布尔值和对象内部不同字符串的JSONObject,它看起来非常像这样

{"1c1":false,"1c2":false,"1c3":false,"1c4":false,"1ed1":false,"1ed2":false,"1ed3":false,"1ep1":true,"2c1":true,"2c2":true,"2c3":false,"2ed1":false,"2ed2":false,"2ed3":true}

每个ListView Item的值至少等于JSONObject中的字符串之一

ChecklistAdapter.java

public class ChecklistAdapter extends ArrayAdapter<Record> {



public ChecklistAdapter(Context context) {
    super(context, R.layout.checklist_item);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.checklist_item, parent, false);

    }

        TextView desc = (TextView) convertView.findViewById(R.id.textView9);
        CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);

        final JSONObject fetchedJSON = ParseUser.getCurrentUser().getJSONObject("checklistData");
        final Record dataRecord = getItem(position);

        desc.setText(dataRecord.getValue());

        final JSONObject myObject = new JSONObject();



    // if item id in list view is equal to one of the strings in the json object make sure the checkbox is selected as true


    checkBox.setOnClickListener(new View.OnClickListener() {

        String idSelected = dataRecord.getID();

        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {

                ParseUser.getCurrentUser().put("checklistData", myObject);
                ParseUser.getCurrentUser().saveInBackground();

                Toast.makeText(getContext(), idSelected,
                        Toast.LENGTH_SHORT).show();

            } else {

                Toast.makeText(getContext(), "CheckBox is unchecked",
                        Toast.LENGTH_SHORT).show();

            }
        }
    });

    return convertView;

        }

        public void swapImageRecords(List<Record> objects) {
            clear();

            for (Record object : objects) {
                add(object);
            }

            notifyDataSetChanged();

        }

    }

如在代码内部所看到的,当单击ListView项时,它将返回给定的选定ID,该ID等于JSONObject中的字符串之一。

String idSelected = dataRecord.getID();

我的计划是,如果“ idSelected”等于JSONObject中的字符串之一,则检查它是true还是false布尔值,并选中复选框作为true值。

我不太确定您是否需要有关布尔值或将状态同步到CheckBox

对于布尔值,可以在JSONObject上使用getBoolean()方法来接收Java布尔值。 以下是文档的链接。

http://developer.android.com/reference/org/json/JSONObject.html#getBoolean(java.lang.String)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM