Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 未正确分析Arraylist_Android_Mysql_Arraylist - Fatal编程技术网

Android 未正确分析Arraylist

Android 未正确分析Arraylist,android,mysql,arraylist,Android,Mysql,Arraylist,你好,我面临一个奇怪的问题。我正在尝试用mysql数据库中的问题和答案制作一个测验应用程序。我解析看起来像的值 如何创建列表: @Override protected List<QuestionsList> doInBackground(String... params) { nameValuePairs = new ArrayList<>(); try { url = new URL(params[0]);

你好,我面临一个奇怪的问题。我正在尝试用mysql数据库中的问题和答案制作一个测验应用程序。我解析看起来像的值

如何创建
列表

@Override
    protected List<QuestionsList> doInBackground(String... params) {
        nameValuePairs = new ArrayList<>();
        try {
            url = new URL(params[0]);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.setConnectTimeout(15000);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            setupDataToDB();
            outputStream = httpURLConnection.getOutputStream();
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            bufferedWriter.write(StringGenerator.queryResults(nameValuePairs));
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            httpURLConnection.connect();
            inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
            jsonResult = StringGenerator.inputStreamToString(inputStream, QuestionsActivity.this);
            jsonResponse = new JSONObject(jsonResult.toString());
            Log.e("Response: ", jsonResult.toString());
            checkDisplayLanguage(langText);
            questionsLists = new ArrayList<>();
            for (int i = 0; i < jsonMainNode.length(); i++) {
                jsonChildNode = jsonMainNode.getJSONObject(i);
                questionName = jsonChildNode.optString(Constants.QUESTION_NAME_JSON_NAME);
                Log.e("Question Name: ", questionName);
                jsonArray = new JSONArray(jsonChildNode.optString(Constants.QUESTIONS_ANSWERS_ARRAY));
                for (int j = 0; j < jsonArray.length(); j++) {
                    jsonSecondChildNode = jsonArray.getJSONObject(j);
                    answer1 = jsonSecondChildNode.optString("answer1");
                    answer2 = jsonSecondChildNode.optString("answer2");
                    answer3 = jsonSecondChildNode.optString("answer3");
                    iscorrect1 = jsonSecondChildNode.optString("iscorrect1");
                    iscorrect2 = jsonSecondChildNode.optString("iscorrect2");
                    iscorrect3 = jsonSecondChildNode.optString("iscorrect3");
                    question_answers = new ArrayList<>();
                    question_answers.add(answer1);
                    question_answers.add(answer2);
                    question_answers.add(answer3);
                    question_iscorrect = new ArrayList<>();
                    question_iscorrect.add(iscorrect1);
                    question_iscorrect.add(iscorrect2);
                    question_iscorrect.add(iscorrect3);
                    Log.e("Answers in for loop", question_answers.toString());
                    questionsLists.add(new QuestionsList(questionName, answersArray, question_iscorrect));
                }

            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return questionsLists;
    }

我在这里做错了什么,而arraylist没有正确解析?

您的代码有一个问题。 你得走了

questionsLists.add(new QuestionsList(questionName, question_answers));
在内部
for循环中
。 因为现在你要做的是只在你的
问题列表中添加最后一个
问题的答案
。应该是这样的-

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

            questionsLists.add(new QuestionsList(questionName, question_answers));
                }


            } 
for(int i=0;i对于(int j=0;j,您的代码有一个问题。
你得走了

questionsLists.add(new QuestionsList(questionName, question_answers));
在内部
for循环中
。 因为现在您正在做的是只将最后一个
问题的答案添加到
问题列表中-

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

            questionsLists.add(new QuestionsList(questionName, question_answers));
                }


            } 
for(int i=0;i对于(int j=0;j这是某种类型的打字错误或其他错误,需要调试并缓慢地进行,而不是堆栈溢出

我个人认为,由于初始化活动时出现错误,您的观点(保留在answer1、answer2和answer3中的观点)都是一个相同的观点。但坦率地说,这个问题应该结束,因为它没有显示任何具体问题

此外,加上:

question_answers = new HashMap<String,String>();

这是某种类型的打字错误或其他错误,需要调试并慢慢进行,而不是堆栈溢出

我个人认为,由于初始化活动时出现错误,您的观点(保留在answer1、answer2和answer3中的观点)都是一个相同的观点。但坦率地说,这个问题应该结束,因为它没有显示任何具体问题

此外,加上:

question_answers = new HashMap<String,String>();


为什么不使用gson?这与我如何解析无关,因为数据是正确获取的。问题是为什么它会获取hashmap的最后一项。是的,我这样做了,而且似乎都是正确的。这些常量真的不同吗?我无法理解为什么需要第二个for循环,因为您没有遍历ans,它看起来是多余的是的,但是仅仅通过你在别处定义的一个常量来提取它们,我遗漏了什么吗?为什么不使用gson?这与我如何解析无关,因为数据是正确获取的。问题是为什么它会得到hashmap的最后一项。是的,我做了,而且似乎都是正确的。这些常量真的不同吗?我无法理解d为什么你需要第二个for循环,它看起来是多余的,因为你不是在遍历答案,而是通过你在别处定义的常量将答案提取出来,我是否遗漏了什么?你的视图在哪里定义和实例化?对于answer1、answer2和..?你的视图在哪里定义和实例化?对于answer1、answer2和..?你在做什么它没有显示具体问题吗?你看到屏幕截图了吗?我应该如何解决它?没错。它显示了一个屏幕截图和大约10%的代码。这不是一个具体问题,而是一项调试任务。例如,我给了你一个非常好的答案-我不知道我是否指出了具体问题,但这是你的许多可能原因之一r代码不工作。什么是完美的答案?因为一个启动活动的bug?这意味着什么?这是哪个bug?这就是我要问的for@helldawg13如果查看初始化视图的代码(我仍然认为问题在于复制粘贴不正确,并且将answer1、answer2和answer3初始化为相同的值)很好,接下来的提示是在ontaskcompletedataset中打印ans1、ans2和ans3(顺便说一句:并开始使用变量而不是属性).但是,正如您所看到的,我不能给您任何答案,只能提供调试提示。因为您的问题中没有具体问题。@helldawg13:我已经写了两次。我将再试一次。代码。它初始化属性answer1、answer2和answer3。请看。它可能包含类似findViewById的内容。检查ID。检查XML(或者不管您如何构建视图,我都不知道)(我也不知道,因为您没有特定的问题,您只需要调试方面的帮助)你说它没有显示具体问题是什么意思?你看到屏幕截图了吗?我该如何解决它?没错。它显示了一个屏幕截图和大约10%的代码。这不是一个具体问题,这是一项调试任务。例如,我给了你一个非常好的答案-我不知道我是否指出了具体问题,但这是许多可能的问题之一你的代码有哪些地方不起作用。什么是一个完美的答案?因为一个启动活动的bug?这意味着什么?这是哪个bug?这就是我要问的for@helldawg13如果查看初始化视图的代码(我仍然认为问题在于复制粘贴不正确,并且将answer1、answer2和answer3初始化为相同的值)很好,接下来的提示是在ontaskcompletedataset中打印ans1、ans2和ans3(顺便说一句:并开始使用变量而不是属性).但是,正如您所看到的,我不能给您任何答案,只能提供调试提示。因为您的问题中没有具体问题。@helldawg13:我已经写了两次。我将再试一次。代码。它初始化属性answer1、answer2和answer3。请看。它可能包含类似findViewById的内容。检查ID。检查XML(或者不管您如何构建视图,我都不知道)(我也不知道,因为您没有特定的问题,您只需要调试方面的帮助)