我的 Activity 中有方法,如果用户向下滚动 TableLayout,它应该从 web 服务中添加项目。看起来像:
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)
我应该如何实现这个?