2016-01-29 47 views
0

我有一个ListView超过40项,我想保存他们的详细信息在MySQL。对于前面的4-5项,所有效果都很好,然后循环停止而没有任何错误。当我试图把它们放在一个数组,然后立刻把他们所有的JSONAndroid调用php for for循环

String student,obtained_value,max_value; 

for (int i=0;i<listStudent.getChildCount();i++) { 
    View view=listStudent.getChildAt(i); 
    studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls); 
    obtainedTxt=(EditText) view.findViewById(R.id.obtained); 
    maxTxt=(TextView) view.findViewById(R.id.max); 
    student=studentIdTxt.getText().toString(); 
    obtained_value=obtainedTxt.getText().toString(); 
    max_value=maxTxt.getText().toString(); 
    //updating the new mark list array 
    /*HashMap<String,String>studentMark=new HashMap<String,String>(); 
    studentMark.put(TAG_STUDENT_ID,student); 
    studentMark.put(TAG_MARKS_OBTAINED,obtained_value); 
    studentMark.put(TAG_MARKS_MAX, max_value);*/ 
    //studentMarksList.add(studentMark); 
    //transform studentMarksList to json 
    //String studentList=gson.toJson(studentMarksList); 

    String URL_CHECK=domain+"/app/caller.php?action=save_marks&work_id="+workId+"&student="+student+"&obtained="+obtained_value+"&max="+max_value+"&course="+course+"&staff="+staff; 


    String response=caller.makeServiceCall(URL_CHECK,serviceHandler.POST); 

    boolean result=false; 
    Log.d("Response: ", "> " + response); 

    if (response != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(response); 

      // Getting JSON Array node 
      ServerReply = jsonObj.getJSONArray(TAG_CHECK_RESULT); 
      JSONObject c=ServerReply.getJSONObject(0); 
      String error_txt= c.getString(TAG_ERROR_TXT); 
      String error_code=c.getString(TAG_ERROR_CODE); 
      String message=c.getString(TAG_MESSAGE); 
      if(error_txt.equals("success")){ 
       result=true; 
      }else{ 
       result=false; 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } else { 
     Log.e("ServiceHandler", "Couldn't get any data from the url"); 
    } 
} 

注释行被使用,但它并没有奏效。

如何在不停止循环的情况下保存ListView中的所有项目?

+1

尝试一次发送全部。截至目前如果您有20个项目,现在您将有20个webservice调用。还可以使用“AsyncTask”进行web服务调用。不要在主线程上运行长时间运行的操作。 –

+0

其实这些代码是在doInBackground中调用的方法,我试图一次发送它们,但传输JSON一直是个问题。我将它们放入Hasmap并将其转换为JSON,但将其作为参数发送给我,让我很头疼。 –

+0

把所有的字符串数组,然后将其转换为字符串由此--->字符串user_id = Arrays.toString(item); –

回答

0

网络I/O速度很慢。您需要将缓慢的I/O操作移出主线程(UI线程)。你可以尝试AsyncTask。它可以帮助你显示你的logcat输出。

+0

确保您从ListView适配器获取数据。 ListView的getChildCount()方法只返回子视图的数量(列表项视图)。如果在ListView和100项数据中有10个可见列表项,则getChildCount()将返回10而不是100. –

+0

实际上,我使用AsyncTask,并且在logcat中,我正确接收服务器响应,但是它停止了第一个4-5项只有 –

+0

嗯!我认为你是对的getChildCount必须得到subViews,怎样才能得到整个列表?我使用了getCount(),它引发错误 –

0

试试这个:

把你的列表视图中的数据字符串数组

String[] item = new String[user_list.size()]; 
int index = 0; 

      for (int i = 0; i < user_list.size(); i++) { 
       item[index] = user_list.get(i).getId(); 
        index++; 
       } 

通过这个转换您的数组一个字符串PARAM:

String user_id = Arrays.toString(item); 

然后把这个作为一个PARAM。