如何遍历数组并忽略所选值

时间:2017-07-07 03:46:57

标签: java android

String response = "[\n" +
        "  {\n" +
        "    \"id\": 1,\n" +
        "    \"name\": \"What is your your father name\"\n" +
        "  },\n" +
        "  {\n" +
        "    \"id\": 2,\n" +
        "    \"name\": \"What is your mother's father name\"\n" +
        "  },\n" +
        "  {\n" +
        "    \"id\": 3,\n" +
        "    \"name\": \"What is your brother's father name\"\n" +
        "  },\n" +
        "  {\n" +
        "    \"id\": 4,\n" +
        "    \"name\": \"What is your father's father name\"\n" +
        "  },\n" +
        "  {\n" +
        "    \"id\": 5,\n" +
        "    \"name\": \"What is your sister's father name\"\n" +
        "  }\n" +
        "]";

我有这组问题,我想循环浏览它以显示在Dialog列表中。如果用户从其中一个AlertDialog中选择值,则会有2个AlertDialog。问题不会出现在其他AlerDialog中。对于循环,这是我的代码:

public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.q1:
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    if (selected2>-1)
                        q1 = new String[jsonArray.length()-1];
                    else
                        q1 = new String[jsonArray.length()];
                    int index = 0;

                    for (int x = 0; x<jsonArray.length(); x++){
                        idQuestion[index] = jsonArray.getJSONObject(x).getInt("id");
                        if (selected2==x)
                            continue;
                        q1[index]= jsonArray.getJSONObject(x).getString("name");
                        index++;
                    }

                    AlertDialog alertDialog =new AlertDialog.Builder(getActivity())
                            .setTitle(R.string.select_security_question)
                            .setItems(q1, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    question1.setText(q1[which]);
                                    if(selected == -1) {
                                        selected = which;
                                    } else {
                                        selected = which+1;
                                    }
                                }
                            }).create();
                    alertDialog.show();

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

            case R.id.q2:
                try {
                    JSONArray ja = new JSONArray(response);
                    if (selected>-1)
                        q2 = new String[ja.length()-1];
                    else
                        q2 = new String[ja.length()];
                    int index = 0;
                    for (int x = 0; x<ja.length(); x++){
                        if (selected==x)
                            continue;
                        q2[index] = ja.getJSONObject(x).getString("name");
                        index++;
                    }
                    AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
                            .setTitle(R.string.select_security_question)
                            .setItems(q2, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    question2.setText(q2[which]);
                                    selected2 = which;
                                    if(selected2 == -1) {
                                        selected2 = which;
                                    } else {
                                        selected2 = which+1;
                                    }
                                }
                            }).create();
                    alertDialog.show();

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

但是我发现这个逻辑是错误的。问题是表单服务器,这就是为什么它是JSON格式的原因。如果可能的话,我想使用问题的ID。因为我必须返回id的值,而不是问题。

2 个答案:

答案 0 :(得分:0)

要忽略循环内的值,请使用if(id==id_ignored) continue;

答案 1 :(得分:0)

这不是完整的解决方案,但我想知道如何实现它。

首先将您的所有问题转换为ArrayList,以便您可以手动执行,或者也可以使用gson

public Question
{
  int id;
  String name;
}

首先创建所有问题的列表
ArrayList<Question> all

然后为ArrayList<Question> filteredList

创建另一个列表

并将选定的ID存储在变量selected_1, selected_2;

当您选择要放置的项目时,将其分配给selected_1 or selected_2 并过滤项目并输入filteredList

for (Question q : all)
{
 if (id in selected_1 or selected_2) //based on your alertdialog
    ignore;
else
  filteredList.add(q);
}

每次选择时都使用

并在AlertDialog中显示列表,请使用此filteredList