Android TableLayout IndexOutOfBoundsException中的ArrayList
我的活动中有一个方法,如果用户向下滚动TableLayout,它应该从webservice添加项。它看起来像:Android TableLayout IndexOutOfBoundsException中的ArrayList,android,scrollview,tablelayout,gesture,Android,Scrollview,Tablelayout,Gesture,我的活动中有一个方法,如果用户向下滚动TableLayout,它应该从webservice添加项。它看起来像: private void getOrderList(String page, String perpage) throws JSONException { DatabaseHandler db = new DatabaseHandler(getApplicationContext()); try { DataParsing u = new Data
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();
}
}
我应该如何实现这一点 我通过简单的验证解决了这个问题。我的方法appendRows()如下所示:
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)
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());
}
}
}
private void appendRows(int start,int end){
最终TableLayout table=(TableLayout)findviewbyd(R.id.table);
if(forPrint.size()>=end)//解决我的问题的实现
{
for(int i=开始;i
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)
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());
}
}
}