public void updateList(int page_num) { curpage_num = page_num; connectDB(); count = helper.getCount(TABLE); total.setText(String.valueOf(count)); total_page = getTotalPage(); page.setText(String.valueOf(total_page)); page1.setText(String.valueOf(total_page)); curpage.setText(String.valueOf(curpage_num)); if (page_num == 1) { page_num = 0; } int offset = 0; if (page_num > 0) { offset = (page_num - 1) * pagesize; } else { offset = page_num * pagesize; } connectDB(); Cursor cur = helper.fetchAll(TABLE, offset, pagesize); if (cur != null) { SimpleCursorAdapter adapter = new SimpleCursorAdapter( activity, R.layout.dept_item, cur, new String[] {"_id", "dept_name", "code_num"}, new int[] {R.id.dept_id, R.id.dept_name, R.id.dept_code}); // 实现列表的显示 listView.setAdapter(adapter); } releaseDB(); }
@Override public void onClick(View v) { connectDB(); count = helper.getCount(TABLE); releaseDB(); total_page = getTotalPage(); switch (v.getId()) { case R.id.dept_first_page: updateList(1); break; case R.id.dept_last_page: if (curpage_num != total_page && total_page > 1) { updateList(total_page); } break; case R.id.dept_pre_page: if (curpage_num > 1) { updateList(curpage_num - 1); } break; case R.id.dept_next_page: if (curpage_num != total_page) { updateList(curpage_num + 1); } break; case R.id.dept_add: LayoutInflater inflater = getActivity().getLayoutInflater(); View detailView = inflater.inflate(R.layout.dept_detail, null); detailView.findViewById(R.id.dept_id_block).setVisibility(View.INVISIBLE); showDetailDialog(detailView, ACTION_ADD); break; } }
private int getTotalPage() { connectDB(); count = helper.getCount(TABLE); if (count > 0 && count < pagesize) { total_page = 1; } else { double d = (Double.parseDouble(String.valueOf(count)) / Double.parseDouble(String.valueOf(pagesize))); total_page = (int) Math.ceil(d); } releaseDB(); return total_page; }
private void connectDB() { if (helper == null) { helper = DBHelper.getInstance(getActivity()); } }