Android调用php for for循环

时间:2016-01-29 08:34:54

标签: php android mysql listview

我有一个ListView超过40项,我想在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中的所有项目?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

试试这个:

将Listview数据放入字符串数组

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++;
                 }

将数组转换为单字符串参数:

String user_id = Arrays.toString(item);

然后把它作为一个参数。