0

I have a ListView with more than 40 items and I want to save their details on MySQL. All works well for the first 4-5 items then the loop stops without any error.

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");
    }
}

The commented lines were used when I tried to put them in an array then send them all at once as JSON, but it didn't worked.

How can I save all the items from the ListView without the loop stopping?

vard
  • 4,057
  • 2
  • 26
  • 46
David NIWEWE
  • 117
  • 1
  • 4
  • 13
  • 1
    Try to send them all at once. As of now If you have 20 items, you will have 20 webservice calls now. Also use an `AsyncTask` for webservice calls. Don't run long running operations on the main thread. – K Neeraj Lal Jan 29 '16 at 08:46
  • Actually those codes are in a method being called in doInBackground, I tried to send them all at once, but transferring the JSON have been a problem. I was putting them in Hasmap and transform it into JSON but sending that as parameter gave me a headache for real. – David NIWEWE Jan 29 '16 at 08:51
  • put all in string array and then convert them in string by this ---> String user_id = Arrays.toString(item); – Chaudhary Amar Jan 29 '16 at 08:52
  • I haven't worked with android for so long, I'm just getting started. Could please provide a sample if possible. I appreciate the help ;) – David NIWEWE Jan 29 '16 at 08:56
  • This will help, http://stackoverflow.com/questions/13911993/sending-a-json-http-post-request-from-android – K Neeraj Lal Jan 29 '16 at 08:59
  • Thanks a lot! let me check it up! – David NIWEWE Jan 29 '16 at 09:11

2 Answers2

0

Network I/O is slow. You need to move slow I/O operation out of main thread (UI thread). You can try AsyncTask. It helps if you can show your logcat output.

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • Make sure you get data from ListView adapter. ListView's getChildCount() method only returns number of subviews (list item view). If you have 10 visible list item in ListView and 100 item of data then getChildCount() returns 10 instead of 100. – Zamrony P. Juhara Jan 29 '16 at 08:59
  • I'm using AsyncTask actually, and within the logcat I'm receiving the server response properly but it stops for the first 4-5 items only – David NIWEWE Jan 29 '16 at 09:02
  • hmmm! I think you you are right getChildCount must be getting subViews, How can get the whole list? I used getCount() and it was throwing errors – David NIWEWE Jan 29 '16 at 09:10
  • Try using ListView getAdapter(). It returns the adapter then you can Adapter.getCount() method. Anyway what error message said? – Zamrony P. Juhara Jan 29 '16 at 09:25
  • it throws java nullExceptionPointer after on this line studentIdTxt=(TextView) view.findViewById(R.id.student_id_ls); – David NIWEWE Jan 29 '16 at 09:37
  • If you use getCount() then it reports how many item in list not how many sub view in list view. Then ListView.getChildAt() may return invalid view (null). Try get value of studentIdTxt from item that Adapter instance return. – Zamrony P. Juhara Jan 29 '16 at 09:52
0

Try This:

Put your Listview data in string array

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

Convert your array into single string param by this:

String user_id = Arrays.toString(item);

Then put this as a param.

Chaudhary Amar
  • 836
  • 8
  • 20