2016-01-29 45 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。