編輯:關於Android編程
public class MainActivity extends Activity implements OnScrollListener{ private ProgressBar loadInfo; private ListView listView; private LinearLayout loadLayout; private ArrayList2,主界面布局文件activity_main.xmlitems; private DatabaseService service; private int currentPage = 1; //默認在第一頁 private static final int lineSize =10; //每次顯示數 private int allRecorders = 0; //全部記錄數 private int pageSize = 1; //默認共一頁 private Aleph0 baseAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview); service = new DatabaseService(this); //創建一個角標線性布局用來顯示正在加載 loadLayout = new LinearLayout(this); loadLayout.setGravity(Gravity.CENTER); //定義一個ProgressBar表示“正在加載” loadInfo = new ProgressBar(this,null,android.R.attr.progressBarStyleSmall); //增加組件 loadLayout.addView(loadInfo, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); //增加到listView頭部 listView.addHeaderView(loadLayout); listView.setOnScrollListener(this); showAllData(); } /** * 讀取全部數據 */ public void showAllData(){ allRecorders = service.getCount(); //計算總頁數 pageSize = (allRecorders + lineSize -1) / lineSize; items = service.getAllItems(currentPage, lineSize); Collections.reverse(items); //倒序 baseAdapter = new Aleph0(); listView.setAdapter(baseAdapter); listView.setSelection(items.size());//直接定位到最底部 } int firstItem=-1; @Override public void onScroll(AbsListView absView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { firstItem=firstVisibleItem; } @Override public void onScrollStateChanged(AbsListView view, int scorllState) { if (firstItem==0&& currentPage < pageSize&& scorllState == OnScrollListener.SCROLL_STATE_IDLE) {// 不再滾動 Log.e(log, 滑到第一位); currentPage++; // 增加數據 appendDate(); } } /** * 增加數據 */ private void appendDate(){ final ArrayList additems = service.getAllItems(currentPage, lineSize); Collections.reverse(additems); baseAdapter.setCount(baseAdapter.getCount() + additems.size()); //判斷,如果到了最末尾則去掉“正在加載” if(allRecorders == baseAdapter.getCount()){ listView.removeHeaderView(loadLayout); } items.addAll(0, additems); baseAdapter.notifyDataSetChanged(); listView.setSelection(additems.size()); } class Aleph0 extends BaseAdapter { int count = lineSize; public int getCount() { return count; } public void setCount(int count){ this.count = count; } public Object getItem(int pos) { return pos; } public long getItemId(int pos) { return pos; } public View getView(int pos, View v, ViewGroup p) { TextView view = new TextView(MainActivity.this); view.setTextSize(60); if(items != null){ view.setText(items.get(pos)); }else{ view.setText(pos); } return view; } } }
3,數據庫操作類
public class DatabaseService { private Context mContext; private MyDBOpenHelper dbHelper; public DatabaseService(Context context) { mContext = context; dbHelper = new MyDBOpenHelper(mContext); } // 添加 public void insert(String title) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = insert into database(title) values(?); db.execSQL(sql, new String[] { title }); } // 刪除 public void delete(String title) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = delete from database where title = ?; db.execSQL(sql, new String[] { title }); } // 查找 public ArrayList4,效果圖find(int id) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = select * from database where _id = ? ; Cursor c = db.rawQuery(sql, new String[] { String.valueOf(id) }); ArrayList titles = new ArrayList (); if (c.moveToNext()) { String title = c.getString(c .getColumnIndexOrThrow(MyDBOpenHelper.FIELD_TITLE)); titles.add(title); return titles; } // 不用忘記關閉Cursor。 c.close(); return null; } // 更新 public void upDate(int id, String title) { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = update database set title =? where _id = ?; db.execSQL(sql, new String[] { String.valueOf(title), String.valueOf(id) }); } // 查詢記錄的總數 public int getCount() { SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = select count(*) from database; Cursor c = db.rawQuery(sql, null); c.moveToFirst(); int length = c.getInt(0); c.close(); return length; } /** * 分頁查詢 * * @param currentPage 當前頁 * @param pageSize 每頁顯示的記錄 * @return 當前頁的記錄 */ public ArrayList getAllItems(int currentPage, int pageSize) { int firstResult = (currentPage - 1) * pageSize; int maxResult = currentPage * pageSize; SQLiteDatabase db = dbHelper.getWritableDatabase(); String sql = select * from database order by _id desc limit ?,? ; Cursor mCursor = db.rawQuery( sql, new String[] { String.valueOf(firstResult), String.valueOf(pageSize) }); ArrayList items = new ArrayList (); int columnCount = mCursor.getColumnCount(); while (mCursor.moveToNext()) { String item = mCursor.getString(mCursor .getColumnIndexOrThrow(MyDBOpenHelper.FIELD_TITLE)); items.add(item); } //不要關閉數據庫 return items; } }
接著上篇文章Android JNI/NDK開發之基本姿勢<一>,今天我們講講怎麼調用native方法,以及native調用java層方法,上篇文章我們已經完成
微信刪除好友對方知道嗎?微信刪除好友後對方還能看到我的朋友圈嗎?小伙伴們在玩微信的過程中,想必也經常會收到一些無聊的廣告消息,雖然自己刪除了對方,但由於微信
Line靜音自拍APP“B612”是一款自拍專用APP,專門用於自拍,且不會發出快門聲。該應用使用簡單,拍照之前無需像傳統拍照軟件那
手機qq安裝失敗怎麼辦?手機qq安裝後打不開是什麼原因?下面我們一起看看解決方法吧!手機安裝失敗原因及解決方法1、證書過期(或者未生效): 下載安裝前將手