2012-08-17 102 views
0

我在我的Activity中有方法,如果用戶向下滾動TableLayout,它應該從webservice添加項目。它看起來像:TableLayout中的ArrayList IndexOutOfBoundsException

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

} 

這是我在表中追加行的功能:

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

最後,我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; 
    } 
    } 

它的工作原理很好,除了局面時,我滾動到數據的末尾......它像這樣拋出IndexOutOfBoundsException:

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) 

我該如何實施?

回答

0

我通過簡單驗證解決了這個問題。我的方法appendRows()看起來像這樣:

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()); 
     } 
    } 
} 
相關問題