Java 如何循环数组并忽略选定值

Java 如何循环数组并忽略选定值,java,android,Java,Android,我有这组问题,我想通过它循环显示在对话框列表中。将有2个AlertDialog,如果用户从其中一个AlertDialog中选择值。这些问题不会出现在另一个AlerDialog中。对于循环,这是我的代码: String response = "[\n" + " {\n" + " \"id\": 1,\n" + " \"name\": \"What is your your father name\"\n" + " },

我有这组问题,我想通过它循环显示在对话框列表中。将有2个AlertDialog,如果用户从其中一个AlertDialog中选择值。这些问题不会出现在另一个AlerDialog中。对于循环,这是我的代码:

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" +
        "]";
public void onClick(视图v){
int id=v.getId();
开关(id){
案例R.id.q1:
试一试{
JSONArray JSONArray=新JSONArray(响应);
如果(已选择2>-1)
q1=新字符串[jsonArray.length()-1];
其他的
q1=新字符串[jsonArray.length()];
int指数=0;
对于(int x=0;x-1)
q2=新字符串[ja.length()-1];
其他的
q2=新字符串[ja.length()];
int指数=0;

对于(int x=0;x要忽略循环中的值,请使用
如果(id==id\u被忽略)继续;

这不是完整的解决方案,但我给出了一些如何实现它的想法

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

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;
        }
首先创建所有问题的列表
i、 e.
arraylistall

然后为
ArrayList filteredList

public Question
{
  int id;
  String name;
}
并将所选id存储在变量
selected_1,selected_2

选择项目时,将其分配给
selected\u 1或selected\u 2
和筛选项目并放入
filteredList

public Question
{
  int id;
  String name;
}
每次选择某个对象时都使用此选项


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

我不明白,请更简短地说明您想要什么,伙计。@arpittatel这里,用户需要从同一组问题中选择两个问题。因此,当用户单击按钮并选择问题时,我使用2个AlertDialog列表。当用户已经选择时一个问题,如果用户点击第二个按钮,同样的问题不应该出现。你让这里的事情变得非常复杂…@arpittatel这些问题实际上也带有id号,但我不知道如何将id输入AlertDialog.setItems。因此,如果有任何可能的方法,我可以将id和问题放入AlertDialog列表w没有显示id。@MichaelShrestha实际上是的,我意识到我把事情弄得很复杂。但我越是努力让逻辑正确,我就越是迷茫,实际上android的问题部分也在那里。我想把id作为值的一部分带到AlerDialog.list中,但不向用户显示,因为它是id。