SQlite資料庫 | 編寫類繼承SQLiteOpenHelper publicMySqlOpenHelper(Contextcontext){ //上下文資料庫名結果集(遊标)資料庫版本 super(context,"sqlite.db",null,1); } MySqlOpenHelper helper=new MySqlOpenHelper(getApplicationContext()); //打開或者建立一個資料庫。第一次就是建立 SQLiteDatabasedatabase1=helper.getWritableDatabase(); //如果磁盤滿了,傳回一個隻讀的 SQLiteDatabasedatabase2=helper.getReadableDatabase(); @Override public void onCreate(SQLiteDatabase db) { //資料庫底層全是以string存,來提升性能; db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20))"); } 版本跟換之後會執行适合做更新表的結構 增 SQLiteDatabase db=helper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name", "dsy"); values.put("phone", "256521"); long l=db.insert("info", null, values); db.close(); | 删 SQLiteDatabase db=helper.getReadableDatabase(); //傳回值代表影響的行數 int delete=db.delete("info", "name=?", new String[]{"dsy"}); db.close(); | 查 SQLiteDatabase db=helper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("phone", "123"); int update=db.update("info", values, "name=?", new String[]{"dsy"}); db.close(); | 改 SQLiteDatabase db=helper.getWritableDatabase(); //第二個參數null表示查詢所有 Cursor cursor=db.query("info", new String[]{"phone"}, "name=?", new String[]{"dsy"}, null, null, null); if(cursor!=null&&cursor.getCount()>0){ while(cursor.moveToNext()){ System.out.println(cursor.getString(0)); } } db.close(); | SQLiteDatabase db=helper.getWritableDatabase(); //第二個參數null表示查詢所有 Cursor cursor=db.query("info", new String[]{"phone"}, "name=?", new String[]{"dsy"}, null, null, null); if(cursor!=null&&cursor.getCount()>0){ while(cursor.moveToNext()){ System.out.println(cursor.getString(0)); } } db.close(); | public void zz(View v){ db=helper.getWritableDatabase(); db.beginTransaction(); try { db.execSQL("update count set money=money+100 where name=?",new Object[]{"dsy"}); db.execSQL("update count set money=money-100 where name=?",new Object[]{"scy"}); db.setTransactionSuccessful(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "伺服器忙請稍後!", 1).show(); }finally{ db.endTransaction(); } } | - 資料庫事物操作
- 建立的資料庫位于項目包名下的databases/
- 在指令行進入目錄,使用sqlite3 資料庫名字來查詢
- 增删查改操作可以使用傳統的語句操作,也可以使用一下API操作
- 建立資料庫onUpgrade
- 建立資料庫onCreate方法合适做表結構的初始化
- 建立需要寫帶Context的參數的構造方法
- 使用Sqlite
|
LitePal2.0資料庫 | Android studio 需要在build.grade中修改一下配置,使用Eclipse需要導入一下jar包 <https://github.com/LitePalFramework/LitePal/blob/master/downloads/litepal-2.0.0.jar> dependencies { implementation 'org.litepal.android:core:2.0.0' } | - 在app/src/main 下建立assets檔案夾,在檔案夾中建立litepal.xml
<litepal> <dbname value=""></dbname> <version value="1"></version> <list> <mapping class="org.litepal.litepalsample.model.Book" /> </list> </litepal> | - 編寫實體類Book繼承LitePalSupport作為映射
- 在maniFest的application中加入
android:name="org.litepal.LitePalApplication" - 在Activity中直接調用Connector.getDatabase();
- 增删查改實體類需要繼承LitePalSupport
增 對象.setXx() 對象.save() | 删 LitePalSupport.deleteAll(指定表名和限制); | 查 LitePalSupport.findAll(指定表名和限制); findFirst findLast LitePalSupport.select("name","price").find(指定表名); //查詢指定列 LitePalSupport.where("page>?","200").find(指定表名); LitePalSupport.order("price desc").find(指定表名); LitePalSupport.limit(3).find(指定表名); LitePalSupport.limit(3).offset(1).find(指定表名); LitePalSupport.findBySQL("sql語句"); | 改 正常對象.set - 對象.save()
- 對象.updateAll("name=? And author=?","a","b")
- 對象.setToDefault(“列名”) 對象.updateAll() 用來把字段值更新成預設值
| |
ListView的基本用法 | 優化1:應為ListView 快速拖動會導緻記憶體溢出 原因是應為消耗的記憶體大于釋放的記憶體,是以需要使用緩存機制 @Override public View getView(finalint position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = mInflater.inflate(R.layout.item,null); holder = new ViewHolder(); holder.title = (TextView) convertView.findViewById(R.id.ItemTitle); holder.text = (TextView) convertView.findViewById(R.id.ItemText); holder.bt = (Button) convertView.findViewById(R.id.ItemButton); convertView.setTag(holder);//綁定ViewHolder對象 }else{ holder = (ViewHolder)convertView.getTag();//取出ViewHolder對象 } holder.title.setText(getDate().get(position).get("ItemTitle").toString()); holder.text.setText(getDate().get(position).get("ItemText").toString()); holder.bt.setOnClickListener(new OnClickListener() { @Override publicvoid onClick(View v) { }); return convertView; } | 優化2:ListView 的Layout_height需要使用 match-parent 使用wrap-content需要校驗多次,運作更多次的getView()方法,效率就差了 - 設定監聽 setOnItemClickListener
- 建立類繼承BaseAdapter,實作構造函數 getCount getItem getItemId getView
-
- 如何使用
- 在布局加入ListView控件
- 在Activity中初始化
- 設定資料
- lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strs));
- SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,new String[] {"ItemImage","ItemTitle", "ItemText"},
|
inflater對象的擷取三種方式 | View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item,null); View view=View.inflate(getApplicationContext(), R.layout.item, null); View View=getSystemService(LAYOUT_INFLATER_SERVICE).inflate(R.layout.item,null); |