【问题标题】:Parsing boolean values from a JSONObject with CheckBoxes in a ListView Adapter使用 ListView 适配器中的复选框从 JSONObject 解析布尔值
【发布时间】:2015-12-05 16:15:00
【问题描述】:

我有一个带有布尔值和不同字符串的 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 项的值至少等于 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 中的字符串之一,请检查它是真还是假布尔值,并将复选框选择为真值。

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!我会看一下文档,看看我能做些什么来同步复选框的状态
    猜你喜欢
    • 2020-05-13
    • 2012-11-28
    • 2019-07-23
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多