android 基础知识 九
2013-07-24 17:40
204 查看
Android Cursor查询更新数据库
写一些cursor查询、更新本地数据库的操作吧。先举个例子:
Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);
复制代码
这条语句相信大家一定经常看到用到,查看sdk帮助文档也很容易找到其中五个参数的意思
第一个参数:是一个URI,指向需要查询的表;
第二个参数:需要查询的列名,是一个数组,可以返回多个列;
第三个参数:需要查询的行,where表示需要满足的查询条件,where语句里面可以有?号;
第四个参数:是一个数组,用来替代上面where语句里面的问号;
第五个参数:表示排序方式;
下面还是用一段代码来加强下印象:
Cursor c = getContentResolver.query(Message.Content_URI ,
new String[]{SyncColumns.Server_Id} , SyncColumns.Id+"=?" , new String[]{Long.toString(MessageId)} , null);
try {
if(c.moveToFirst()) {
return c.getString(0);//0表示返回的行数
}
else {
return null;
}
}
finally {
c.close();
}
复制代码
下面再来看一段更新数据库的操作:
ContentValues cv = new ContentValues();
cv.put(Body.HTML_Content, newHtmlBody);//第一个参数是列名,第二个参数是要放入的值
String where = Body.Message_Key + "=" + mMessageId;
getContentResolver().update(uri , cv , where , null);
//这里的四个参数应该很清楚了,uri是表,cv上面要更新的值,where是搜索行的语句,null是历史记录可以为空
复制代码
---------------------------------------------------------------------------------------------
Android中include的使用
如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下:
main.xml
<font size="3"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell1"
layout="@layout/item"
android:layout_marginTop="10dp"
android:layout_marginLeft="45dp" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell2" layout="@layout/item"
android:layout_toRightOf="@+id/cell1"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell3" layout="@layout/item"
android:layout_toRightOf="@+id/cell2"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp" />
</RelativeLayout>
</font>
复制代码
item.xml
<font size="3"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
<ImageView
android:background="#000000"
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#a17006"
android:textStyle="bold" android:textSize="22dp"
android:layout_alignLeft="@+id/iv_img"
android:layout_below="@+id/iv_img" />
</RelativeLayout>
</font>
复制代码
使用Android include时需要注意的是要指定宽高属性,要不可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item。
---------------------------------------------------------------------------------
Android得到当前电量信息
通过广播的方式监听[Android当前电量信息。
registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
Log.v(TAG,
"Battery level: " + String.valueOf(level * 100 / scale)
+ "%");
}
}
};
写一些cursor查询、更新本地数据库的操作吧。先举个例子:
Cursor c = getContentResolver.query(uri , String[ ] , where , String[ ] , sort);
复制代码
这条语句相信大家一定经常看到用到,查看sdk帮助文档也很容易找到其中五个参数的意思
第一个参数:是一个URI,指向需要查询的表;
第二个参数:需要查询的列名,是一个数组,可以返回多个列;
第三个参数:需要查询的行,where表示需要满足的查询条件,where语句里面可以有?号;
第四个参数:是一个数组,用来替代上面where语句里面的问号;
第五个参数:表示排序方式;
下面还是用一段代码来加强下印象:
Cursor c = getContentResolver.query(Message.Content_URI ,
new String[]{SyncColumns.Server_Id} , SyncColumns.Id+"=?" , new String[]{Long.toString(MessageId)} , null);
try {
if(c.moveToFirst()) {
return c.getString(0);//0表示返回的行数
}
else {
return null;
}
}
finally {
c.close();
}
复制代码
下面再来看一段更新数据库的操作:
ContentValues cv = new ContentValues();
cv.put(Body.HTML_Content, newHtmlBody);//第一个参数是列名,第二个参数是要放入的值
String where = Body.Message_Key + "=" + mMessageId;
getContentResolver().update(uri , cv , where , null);
//这里的四个参数应该很清楚了,uri是表,cv上面要更新的值,where是搜索行的语句,null是历史记录可以为空
复制代码
---------------------------------------------------------------------------------------------
Android中include的使用
如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下:
main.xml
<font size="3"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell1"
layout="@layout/item"
android:layout_marginTop="10dp"
android:layout_marginLeft="45dp" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell2" layout="@layout/item"
android:layout_toRightOf="@+id/cell1"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp" />
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cell3" layout="@layout/item"
android:layout_toRightOf="@+id/cell2"
android:layout_alignTop="@+id/cell1"
android:layout_marginLeft="20dp" />
</RelativeLayout>
</font>
复制代码
item.xml
<font size="3"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible">
<ImageView
android:background="#000000"
android:id="@+id/iv_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#a17006"
android:textStyle="bold" android:textSize="22dp"
android:layout_alignLeft="@+id/iv_img"
android:layout_below="@+id/iv_img" />
</RelativeLayout>
</font>
复制代码
使用Android include时需要注意的是要指定宽高属性,要不可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item。
---------------------------------------------------------------------------------
Android得到当前电量信息
通过广播的方式监听[Android当前电量信息。
registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
private BroadcastReceiver mBatteryInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
int level = intent.getIntExtra("level", 0);
int scale = intent.getIntExtra("scale", 100);
Log.v(TAG,
"Battery level: " + String.valueOf(level * 100 / scale)
+ "%");
}
}
};
相关文章推荐
- 3.基础知识Android_ArrayMap
- Android 基础知识点整理
- android开发基础知识了解
- Android数据库基础知识——SQLite数据库的使用
- Android基础知识
- Android基础知识
- 【Android基础知识】TextView跑马灯效果
- Android基础知识_Application的用途
- 【Android基础知识】WebView基本用法
- 安卓(android)开发应该怎么学?需要哪些基础知识?
- Android手机分辨率基础知识
- Android基础知识整合篇——Service知识点
- Android基础知识之四大组件Activity(二)一个Activity的生命周期?
- Android常用基础知识个人总结(一)
- android基础知识:加载较大分辨率图片到手机内存
- [android基础知识] 之十九: JNI究竟处于什么位置?
- 【Android基础知识】Activity的四种启动模式
- 【转】Android手机分辨率基础知识(DPI,DIP计算)
- Android中apk动态加载技术研究(1)基础知识研修
- Android音频开发(1):基础知识