【问题标题】:how to add dynamic checkbox inside web api response?如何在 Web api 响应中添加动态复选框?
【发布时间】:2020-01-04 14:51:11
【问题描述】:

这是我的代码,我在多个时间槽中得到响应我想根据时间响应创建多个复选框这是我的代码:

  @Override
        public void onResponse(String response) {
            pd.dismiss();
            Log.e("@@TimeApi", response);
            try {

                JSONObject obj = new JSONObject(response);
                JSONObject objData = obj.getJSONObject("data");
                JSONArray jsonArray = objData.getJSONArray("time");

                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                if (!obj.getBoolean("error")) {

                    for (int i=0; i<jsonArray.length(); i++){

                        String time = jsonArray.getString(i);
                        Log.e("@@Time", time);
                        final List<String> timeList = Arrays.asList(time);


                    }
                } else {
                    Toast.makeText(getActivity(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

在 for 循环中,我现在有时间了,我需要用复选框将时间放在对话框中

在日志中我收到这样的回复,可能会有多个

【问题讨论】:

    标签: android json checkbox dialog


    【解决方案1】:

    在 AlertDialog 中你需要在 setMultipleChoiceItems 中设置列​​表,检查以下代码

    @Override
            public void onResponse(String response) {
                pd.dismiss();
                Log.e("@@TimeApi", response);
                try {
    
                    JSONObject obj = new JSONObject(response);
                    JSONObject objData = obj.getJSONObject("data");
                    JSONArray jsonArray = objData.getJSONArray("time");
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
                    if (!obj.getBoolean("error")) {
                        String[] timeList = new String[jsonArray.length()];
                        boolean[] checkedItems = new boolean[jsonArray.length()];
                        for (int i = 0; i < jsonArray.length(); i++) {
    
                            String time = jsonArray.getString(i);
                            Log.e("@@Time", time);
                            timeList[i] = time;
                            checkedItems[i] = false;
                        }
    
                        builder.setMultiChoiceItems(timeList, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                Log.i("@@Time", "position" + which + " state " + isChecked);
                            }
                        });
                    } else {
                        Toast.makeText(getActivity(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                    }
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
    

    【讨论】:

    • 这里如何根据时间设置复选框
    • 按时间表示?
    • 作为响应,我在 json 数组中获得时间
    • 您要按时间排序吗?
    • 我想用复选框添加动态时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2014-02-12
    相关资源
    最近更新 更多