Q:

ArrayList in TableLayout IndexOutOfBoundsException

I have method in my Activity, which should add items from the webservice if user scrolls down the TableLayout.  It looks like:
  private void getOrderList(String page, String perpage) throws JSONException
{

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    try {
        DataParsing u = new DataParsing();
        UserFunctions n = new UserFunctions();
        JSONObject k= n.getOrders(page,perpage);

        orders = u.wrapOrders(k);
        Dao<ProcessStatus,Integer> dao = db.getStatusDao();
        Log.i("status",dao.queryForAll().toString());
        QueryBuilder<ProcessStatus,Integer> query = dao.queryBuilder();
        Where where = query.where();
        //query.selectColumns(ProcessStatus. STATUS_TITLE).where().eq(ProcessStatus. STATUS_ID,).toString());
        //PreparedQuery<ProcessStatus, Integer> preparedQuery = ((Object) query).prepareQuery();
        String result = "";
        List<String[]> results = null;
        String res[][] = null;

        try{
            for(Order r : orders)
            {
                 GenericRawResults<String[]> rawResults = dao.queryRaw("select "+ ProcessStatus. STATUS_TITLE +" from process_status where "+ ProcessStatus. STATUS_ID + " = " + r.getProcess_status().getProccessStatusId());
                 results = rawResults.getResults();
                 String[] resultArray = results.get(0); //This will select the first result (the first and maybe only row returned)
                 result = resultArray[0]; //This will select the first field in the result (That should be the ID you need)
                 r.getProcess_status().setProccessStatusTitle(result);
                 Log.i("res",r.toString());
                 forPrint.add(r);
                 Log.i("asd",forPrint.toString());
                 //res = Integer.toString(r.getOrderid());             
             }

        }

        catch (Exception e) {
            e.printStackTrace();
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

This is my function of appending rows in the table:
 private void appendRows(int start,int end) {
    final TableLayout table = (TableLayout)findViewById(R.id.table);
    for (int i=start; i<end; i++)
    { 

        TableRow row = new TableRow(this); 

        TextView idText = new TextView(this);

        idText.setText(Integer.toString(forPrint.get(i).getOrderid()));

        idText.setPadding(10, 0, 0, 20);
        idText.setTextColor(Color. BLACK);
        row.addView(idText);

        TextView textOne = new TextView(this);
        textOne.setText(forPrint.get(i).getTitle());
        textOne.setSingleLine(false);
        textOne.setPadding(15, 0, 0, 0);
        textOne.setTextColor(Color. BLACK);
        row.addView(textOne);

        TextView textTwo = new TextView(this);
        textTwo.setText(Float.toString(forPrint.get(i).getPrice()));
        textTwo.setPadding(15, 0, 0, 0);
        textTwo.setTextColor(Color. BLACK);
        row.addView(textTwo);

         TextView textThree = new TextView(this);
         textThree.setText(forPrint.get(i).getProcess_status().getProccessStatusTitle());
         textThree.setPadding(15, 0, 0, 0);
         textThree.setTextColor(Color. BLACK);
        row.addView(textThree);

       table.addView(row, new TableLayout. LayoutParams()); 
    }
 }

And, finally, my GestureListener:
  class MyGestureListener extends SimpleOnGestureListener implements OnTouchListener
{

public boolean onDown(MotionEvent arg0) {
    Log.i("asd","sdas");
    return false;
}

public void onLongPress(MotionEvent arg0) {
    // TODO Auto-generated method stub

}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float arg2,
        float arg3) {
    mIsScrolling = true;   

    float pos = e2.getY() - e1.getY();
    if (pos< -120)
    {
        Log.i("sd","I'm here");
        try {

            page+=1;
            start +=10;    // start position of the new items
            end+=10;       // end position of the new items
            Log.i("position down",Integer.toString(page));
            getOrderList(Integer.toString(page),"10");
            appendRows(start,end);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    return true;
   }
   }

It works good except situation when I scroll to the end of the data...It throws IndexOutOfBoundsException like this:
    08-17 10:46:34.749: E/AndroidRuntime(6321): FATAL EXCEPTION: main
08-17 10:46:34.749: E/AndroidRuntime(6321): java.lang. IndexOutOfBoundsException: Invalid index 58, size is 58
08-17 10:46:34.749: E/AndroidRuntime(6321):     at java.util. ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at java.util. ArrayList.get(ArrayList.java:311)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.assignmentexpert. DashboardActivity.appendRows(DashboardActivity.java:192)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.assignmentexpert. DashboardActivity.access$2(DashboardActivity.java:183)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.assignmentexpert. DashboardActivity$MyGestureListener.onScroll(DashboardActivity.java:264)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.view. GestureDetector.onTouchEvent(GestureDetector.java:541)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.assignmentexpert. DashboardActivity.dispatchTouchEvent(DashboardActivity.java:334)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.android.internal.policy.impl. PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1734)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.view. ViewRoot.deliverPointerEvent(ViewRoot.java:2216)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.view. ViewRoot.handleMessage(ViewRoot.java:1887)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.os. Handler.dispatchMessage(Handler.java:99)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.os. Looper.loop(Looper.java:123)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at android.app. ActivityThread.main(ActivityThread.java:3687)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at java.lang.reflect. Method.invokeNative(Native Method)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at java.lang.reflect. Method.invoke(Method.java:507)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.android.internal.os. ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at com.android.internal.os. ZygoteInit.main(ZygoteInit.java:625)
08-17 10:46:34.749: E/AndroidRuntime(6321):     at dalvik.system. NativeStart.main(Native Method)

How should I implement this?  

A:

I solved this by simple verification.  My method appendRows() looks like this:
private void appendRows(int start,int end) {
    final TableLayout table = (TableLayout)findViewById(R.id.table);
    if(forPrint.size() >= end)  // implementation which solved my problem
    {
        for (int i=start; i<end-1; i++)
        { 

            TableRow row = new TableRow(this); 

            TextView idText = new TextView(this);

            idText.setText(Integer.toString(forPrint.get(i).getOrderid()));

            idText.setPadding(10, 0, 0, 20);
            idText.setTextColor(Color. BLACK);
            row.addView(idText);

            TextView textOne = new TextView(this);
            textOne.setText(forPrint.get(i).getTitle());
            textOne.setSingleLine(false);
            textOne.setPadding(15, 0, 0, 0);
            textOne.setTextColor(Color. BLACK);
            row.addView(textOne);

            TextView textTwo = new TextView(this);
            textTwo.setText(Float.toString(forPrint.get(i).getPrice()));
            textTwo.setPadding(15, 0, 0, 0);
            textTwo.setTextColor(Color. BLACK);
            row.addView(textTwo);

             TextView textThree = new TextView(this);
             textThree.setText(forPrint.get(i).getProcess_status().getProccessStatusTitle());
             textThree.setPadding(15, 0, 0, 0);
             textThree.setTextColor(Color. BLACK);
            row.addView(textThree);

           table.addView(row, new TableLayout. LayoutParams()); 
         }
   }
}

