android 基础知识 九

来源:互联网 发布:ov7670 51单片机 编辑:程序博客网 时间:2024/06/01 10:45
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. };
原创粉丝点击