【问题标题】:Android call php in for loopAndroid在for循环中调用php
【发布时间】:2016-01-29 08:34:54
【问题描述】:

我有一个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 中的所有项目?

【问题讨论】:

  • 尝试一次全部发送。截至目前,如果您有 20 个项目,那么您现在将有 20 个 Web 服务调用。还可以使用AsyncTask 进行网络服务调用。不要在主线程上运行长时间运行的操作。
  • 实际上这些代码在doInBackground中被调用的方法中,我试图一次发送它们,但是传输JSON一直是个问题。我将它们放入 Hasmap 并将其转换为 JSON,但将其作为参数发送让我非常头疼。
  • 全部放入字符串数组,然后通过这个转换成字符串---> String user_id = Arrays.toString(item);
  • 我已经很久没有使用android了,我才刚刚开始。如果可能,请提供样品。感谢您的帮助;)

标签: php android mysql listview


【解决方案1】:

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

【讨论】:

  • 确保从 ListView 适配器获取数据。 ListView 的 getChildCount() 方法只返回子视图的数量(列表项视图)。如果 ListView 中有 10 个可见列表项和 100 个数据项,则 getChildCount() 返回 10 而不是 100。
  • 我实际上正在使用 AsyncTask,并且在 logcat 中我可以正确接收服务器响应,但它仅在前 4-5 个项目中停止
  • 嗯!我认为你是对的 getChildCount 必须获取子视图,如何获取整个列表?我使用 getCount() 并抛出错误
  • 尝试使用 ListView getAdapter()。它返回适配器然后您可以使用 Adapter.getCount() 方法。无论如何,错误信息是什么?
  • 它在这一行之后抛出 java nullExceptionPointer studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls);
【解决方案2】:

试试这个:

将您的 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);

然后把它作为参数。

【讨论】:

    猜你喜欢
    • 2020-01-11
    • 2019-03-12
    • 2015-01-28
    • 1970-01-01
    • 2013-06-12
    • 2013-11-14
    • 2022-10-06
    • 2014-07-25
    • 2012-10-18
    相关资源
    最近更新 更多