我有一个包含 40 多个项目的 ListView
,我想将它们的详细信息保存在 MySQL 上。前 4-5 个项目一切正常,然后循环停止,没有任何错误。
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");
}
}
当我尝试将它们放入数组中然后将它们作为 JSON 一次性发送时,使用了注释行,但它不起作用。
如何在不停止循环的情况下保存 ListView
中的所有项目?
最佳答案
网络 I/O 速度慢。您需要将缓慢的 I/O 操作移出主线程(UI 线程)。你可以尝试一下AsyncTask。如果您可以显示 logcat 输出,这会有所帮助。
关于php - Android在for循环中调用php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35080049/