android 基礎知識 九

Android Cursor查詢更新數據庫
寫一些cursor查詢、更新本地數據庫的操作吧。先舉個例子:

  1. Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);
複製代碼
這條語句相信大家一定經常看到用到,查看sdk幫助文檔也很容易找到其中五個參數的意思
第一個參數:是一個URI,指向需要查詢的表;
第二個參數:需要查詢的列名,是一個數組,可以返回多個列;
第三個參數:需要查詢的行,where表示需要滿足的查詢條件,where語句裏面可以有?號;
第四個參數:是一個數組,用來替代上面where語句裏面的問號;
第五個參數:表示排序方式;
下面還是用一段代碼來加強下印象:

  1. Cursor c = getContentResolver.query(Message.Content_URI ,  
  2. new String[]{SyncColumns.Server_Id} , SyncColumns.Id+"=?" ,  new String[]{Long.toString(MessageId)} , null);
  3. try {
  4.     if(c.moveToFirst()) {
  5.         return c.getString(0);//0表示返回的行數
  6.     }
  7.     else {
  8.         return null;
  9.     }
  10. }
  11. finally {
  12.     c.close();
  13. }
複製代碼
下面再來看一段更新數據庫的操作:

  1. ContentValues cv = new ContentValues();
  2. cv.put(Body.HTML_Content, newHtmlBody);//第一個參數是列名,第二個參數是要放入的值
  3. String where = Body.Message_Key + "=" + mMessageId;
  4. getContentResolver().update(uri , cv , where , null);
  5. //這裏的四個參數應該很清楚了,uri是表,cv上面要更新的值,where是搜索行的語句,null是歷史記錄可以爲空
複製代碼
---------------------------------------------------------------------------------------------
Android中include的使用
如果在程序中多次用到一部分相同的佈局,可以先將這部分佈局定義爲一個單獨的XML,然後在需要的地方通過<include>引入,如下:
main.xml

  1. <font size="3"><?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content">
  5.     <include
  6.         android:layout_width="wrap_content"
  7.         android:layout_height="wrap_content"
  8.         android:id="@+id/cell1"
  9.         layout="@layout/item"
  10.         android:layout_marginTop="10dp"
  11.         android:layout_marginLeft="45dp" />
  12.     <include
  13.         android:layout_width="wrap_content"
  14.         android:layout_height="wrap_content"
  15.         android:id="@+id/cell2" layout="@layout/item"
  16.         android:layout_toRightOf="@+id/cell1"
  17.         android:layout_alignTop="@+id/cell1"
  18.         android:layout_marginLeft="20dp" />
  19.     <include
  20.         android:layout_width="wrap_content"
  21.         android:layout_height="wrap_content"
  22.         android:id="@+id/cell3" layout="@layout/item"
  23.         android:layout_toRightOf="@+id/cell2"
  24.         android:layout_alignTop="@+id/cell1"
  25.         android:layout_marginLeft="20dp" />
  26. </RelativeLayout>
  27. </font>
複製代碼
item.xml

  1. <font size="3"><?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:visibility="invisible">
  6.     <ImageView
  7.         android:background="#000000"
  8.         android:id="@+id/iv_img"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:clickable="true"
  12.         android:focusable="false" />
  13.     <TextView
  14.         android:id="@+id/tv_name"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:textColor="#a17006"
  18.         android:textStyle="bold" android:textSize="22dp"
  19.         android:layout_alignLeft="@+id/iv_img"
  20.         android:layout_below="@+id/iv_img" />
  21. </RelativeLayout>
  22. </font>
複製代碼
使用Android include時需要注意的是要指定寬高屬性,要不可能會出現一些意想不到的效果,比如引用了三次,而界面上只顯示了一個item。
---------------------------------------------------------------------------------
Android得到當前電量信息
通過廣播的方式監聽[Android當前電量信息。

  1. registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  2. private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
  3.     @Override
  4.     public void onReceive(Context context, Intent intent) {
  5.         String action = intent.getAction();
  6.         if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
  7.   
  8.             int level = intent.getIntExtra("level", 0);
  9.             int scale = intent.getIntExtra("scale", 100);
  10.   
  11.             Log.v(TAG,
  12.                     "Battery level: " + String.valueOf(level * 100 / scale)
  13.                             + "%");
  14.         }
  15.     }
  16. };
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章