編輯:關於Android編程
RuiXin.java類
[html]
package com.gtl.data.two;
import android.net.Uri;
public class RuiXin {
public static final String DBNAME = "ruixinonlinedb";
public static final String TNAME = "ruixinonline";
public static final int VERSION = 3;
public static String TID = "tid";
public static final String USERNAME = "username";
public static final String DATE = "date";
public static final String SEX = "sex";
public static final String AUTOHORITY = "com.gtl.data.two";
public static final int ITEM = 1;
public static final int ITEM_ID = 2;
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.ruixin.login";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.ruixin.login";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/ruixinonline");
}
package com.gtl.data.two;
import android.net.Uri;
public class RuiXin {
public static final String DBNAME = "ruixinonlinedb";
public static final String TNAME = "ruixinonline";
public static final int VERSION = 3;
public static String TID = "tid";
public static final String USERNAME = "username";
public static final String DATE = "date";
public static final String SEX = "sex";
public static final String AUTOHORITY = "com.gtl.data.two";
public static final int ITEM = 1;
public static final int ITEM_ID = 2;
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.ruixin.login";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.ruixin.login";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTOHORITY + "/ruixinonline");
}
DBlite.java類
[html
package com.gtl.data.two;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBlite extends SQLiteOpenHelper {
public DBlite(Context context) {
super(context, RuiXin.DBNAME, null, RuiXin.VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+RuiXin.TNAME+"(" +
RuiXin.TID+" integer primary key autoincrement not null,"+
RuiXin.USERNAME+" text not null," +
RuiXin.DATE+" interger not null,"+
RuiXin.SEX+" text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+RuiXin.TNAME);
onCreate(db);
}
public void add(String username,String date,String sex){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(RuiXin.USERNAME, username);
values.put(RuiXin.DATE, date);
values.put(RuiXin.SEX, sex);
db.insert(RuiXin.TNAME,"",values);
}
public void del(){
SQLiteDatabase db = getWritableDatabase();
db.delete(RuiXin.TNAME,RuiXin.DATE+"=?" , new String[]{"time=1368497000287"});
}
public void update(){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(RuiXin.USERNAME, "louis koo");
values.put(RuiXin.DATE, "today");
values.put(RuiXin.SEX, "男");
db.update(RuiXin.TNAME, values, RuiXin.DATE+"=?" , new String[]{"time=1368497006097"});
}
}
package com.gtl.data.two;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBlite extends SQLiteOpenHelper {
public DBlite(Context context) {
super(context, RuiXin.DBNAME, null, RuiXin.VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+RuiXin.TNAME+"(" +
RuiXin.TID+" integer primary key autoincrement not null,"+
RuiXin.USERNAME+" text not null," +
RuiXin.DATE+" interger not null,"+
RuiXin.SEX+" text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+RuiXin.TNAME);
onCreate(db);
}
public void add(String username,String date,String sex){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(RuiXin.USERNAME, username);
values.put(RuiXin.DATE, date);
values.put(RuiXin.SEX, sex);
db.insert(RuiXin.TNAME,"",values);
}
public void del(){
SQLiteDatabase db = getWritableDatabase();
db.delete(RuiXin.TNAME,RuiXin.DATE+"=?" , new String[]{"time=1368497000287"});
}
public void update(){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(RuiXin.USERNAME, "louis koo");
values.put(RuiXin.DATE, "today");
values.put(RuiXin.SEX, "男");
db.update(RuiXin.TNAME, values, RuiXin.DATE+"=?" , new String[]{"time=1368497006097"});
}
}
MyProvider.java類
[html]
package com.gtl.data.two;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
public class MyProvider extends ContentProvider {
DBlite dBlite;
SQLiteDatabase db;
private static final UriMatcher sMatcher;
static{
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(RuiXin.AUTOHORITY,RuiXin.TNAME, RuiXin.ITEM);
sMatcher.addURI(RuiXin.AUTOHORITY, RuiXin.TNAME+"/#", RuiXin.ITEM_ID);
}
@Override
public boolean onCreate() {
this.dBlite = new DBlite(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
db = dBlite.getWritableDatabase();
Cursor c;
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
c = db.query(RuiXin.TNAME, projection, selection, selectionArgs, null, null, null);
break;
case RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(1);
c = db.query(RuiXin.TNAME, projection,
RuiXin.TID+"="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""),
selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public String getType(Uri uri) {
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
return RuiXin.CONTENT_TYPE;
case RuiXin.ITEM_ID:
return RuiXin.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = dBlite.getWritableDatabase();
long rowId;
if(sMatcher.match(uri)!=RuiXin.ITEM){
throw new IllegalArgumentException("Unknown URI"+uri);
}
rowId = db.insert(RuiXin.TNAME,RuiXin.TID,values);
if(rowId>0){
Uri noteUri=ContentUris.withAppendedId(RuiXin.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
return noteUri;
}
throw new IllegalArgumentException("Unknown URI"+uri);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
db = dBlite.getWritableDatabase();
int count = 0;
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
count = db.delete(RuiXin.TNAME,selection, selectionArgs);
break;
case RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(1);
count = db.delete(RuiXin.TID,
RuiXin.TID+"="+id+(!TextUtils.isEmpty(RuiXin.TID="?")?"AND("+selection+')':""),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = dBlite.getWritableDatabase();
int count;
switch (sMatcher.match(uri)){
case RuiXin.ITEM:
count = db.update(RuiXin.TNAME, values, selection, selectionArgs);
break;
case RuiXin.ITEM_ID:
String Id = uri.getPathSegments().get(1);
count = db.update(RuiXin.TNAME, values,
RuiXin.TID + "=" + Id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
package com.gtl.data.two;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
public class MyProvider extends ContentProvider {
DBlite dBlite;
SQLiteDatabase db;
private static final UriMatcher sMatcher;
static{
sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI(RuiXin.AUTOHORITY,RuiXin.TNAME, RuiXin.ITEM);
sMatcher.addURI(RuiXin.AUTOHORITY, RuiXin.TNAME+"/#", RuiXin.ITEM_ID);
}
@Override
public boolean onCreate() {
this.dBlite = new DBlite(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
db = dBlite.getWritableDatabase();
Cursor c;
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
c = db.query(RuiXin.TNAME, projection, selection, selectionArgs, null, null, null);
break;
case RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(1);
c = db.query(RuiXin.TNAME, projection,
RuiXin.TID+"="+id+(!TextUtils.isEmpty(selection)?"AND("+selection+')':""),
selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public String getType(Uri uri) {
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
return RuiXin.CONTENT_TYPE;
case RuiXin.ITEM_ID:
return RuiXin.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db = dBlite.getWritableDatabase();
long rowId;
if(sMatcher.match(uri)!=RuiXin.ITEM){
throw new IllegalArgumentException("Unknown URI"+uri);
}
rowId = db.insert(RuiXin.TNAME,RuiXin.TID,values);
if(rowId>0){
Uri noteUri=ContentUris.withAppendedId(RuiXin.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
return noteUri;
}
throw new IllegalArgumentException("Unknown URI"+uri);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
db = dBlite.getWritableDatabase();
int count = 0;
switch (sMatcher.match(uri)) {
case RuiXin.ITEM:
count = db.delete(RuiXin.TNAME,selection, selectionArgs);
break;
case RuiXin.ITEM_ID:
String id = uri.getPathSegments().get(1);
count = db.delete(RuiXin.TID,
RuiXin.TID+"="+id+(!TextUtils.isEmpty(RuiXin.TID="?")?"AND("+selection+')':""),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = dBlite.getWritableDatabase();
int count;
switch (sMatcher.match(uri)){
case RuiXin.ITEM:
count = db.update(RuiXin.TNAME, values, selection, selectionArgs);
break;
case RuiXin.ITEM_ID:
String Id = uri.getPathSegments().get(1);
count = db.update(RuiXin.TNAME, values,
RuiXin.TID + "=" + Id + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""),
selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
主類Test.java類
[html]
package com.gtl.data.two;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.gtl.R;
public class Test extends Activity implements OnClickListener{
private DBlite dBlite1 ;
private ContentResolver contentResolver;
private Button addbtn,delbtn,querybtn,updatebtn;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dBlite1 = new DBlite(this);
setContentView(R.layout.contentprovide);
addbtn = (Button)findViewById(R.id.btnadd);
delbtn = (Button)findViewById(R.id.btndelete);
querybtn = (Button)findViewById(R.id.btnquery);
updatebtn = (Button)findViewById(R.id.btnupdate);
addbtn.setOnClickListener(this);
delbtn.setOnClickListener(this);
querybtn.setOnClickListener(this);
updatebtn.setOnClickListener(this);
lv = (ListView)findViewById(R.id.lv);
}
private List<String> strlist;
@Override
public void onClick(View v) {
if(v==addbtn){
dBlite1.add("carman lee","time="+System.currentTimeMillis(),"女");
}else if(v==delbtn){
dBlite1.del();
}else if(v==querybtn){
//通過contentResolver進行查找
strlist = new ArrayList<String>();
contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI, new String[] {RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX }, null, null, null);
while (cursor.moveToNext()) {
strlist.add(cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)));
}
startManagingCursor(cursor); //查找後關閉游標
ArrayAdapter<String> adpater = new ArrayAdapter<String>(Test.this,
android.R.layout.simple_expandable_list_item_1,strlist);
lv.setAdapter(adpater);
}else if(v==updatebtn){
dBlite1.update();
}
}
}
package com.gtl.data.two;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.gtl.R;
public class Test extends Activity implements OnClickListener{
private DBlite dBlite1 ;
private ContentResolver contentResolver;
private Button addbtn,delbtn,querybtn,updatebtn;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dBlite1 = new DBlite(this);
setContentView(R.layout.contentprovide);
addbtn = (Button)findViewById(R.id.btnadd);
delbtn = (Button)findViewById(R.id.btndelete);
querybtn = (Button)findViewById(R.id.btnquery);
updatebtn = (Button)findViewById(R.id.btnupdate);
addbtn.setOnClickListener(this);
delbtn.setOnClickListener(this);
querybtn.setOnClickListener(this);
updatebtn.setOnClickListener(this);
lv = (ListView)findViewById(R.id.lv);
}
private List<String> strlist;
@Override
public void onClick(View v) {
if(v==addbtn){
dBlite1.add("carman lee","time="+System.currentTimeMillis(),"女");
}else if(v==delbtn){
dBlite1.del();
}else if(v==querybtn){
//通過contentResolver進行查找
strlist = new ArrayList<String>();
contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI, new String[] {RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX }, null, null, null);
while (cursor.moveToNext()) {
strlist.add(cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)));
}
startManagingCursor(cursor); //查找後關閉游標
ArrayAdapter<String> adpater = new ArrayAdapter<String>(Test.this,
android.R.layout.simple_expandable_list_item_1,strlist);
lv.setAdapter(adpater);
}else if(v==updatebtn){
dBlite1.update();
}
}
}
接下來在另外一個項目中進行測試,共用一個數據庫
MainActivity.java類
[html]
package com.example.contentprovidertest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ContentResolver contentResolver;
private List<String> strlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lv = new ListView(this);
//通過contentResolver進行查找
strlist = new ArrayList<String>();
contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI, new String[] {RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX }, null, null, null);
while (cursor.moveToNext()) {
strlist.add(cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)));
}
startManagingCursor(cursor); //查找後關閉游標
ArrayAdapter<String> adpater = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,strlist);
lv.setAdapter(adpater);
setContentView(lv);
}
}
package com.example.contentprovidertest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private ContentResolver contentResolver;
private List<String> strlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lv = new ListView(this);
//通過contentResolver進行查找
strlist = new ArrayList<String>();
contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(
RuiXin.CONTENT_URI, new String[] {RuiXin.USERNAME,
RuiXin.DATE,RuiXin.SEX }, null, null, null);
while (cursor.moveToNext()) {
strlist.add(cursor.getString(cursor.getColumnIndex(RuiXin.USERNAME))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.DATE))
+ " "+ cursor.getString(cursor.getColumnIndex(RuiXin.SEX)));
}
startManagingCursor(cursor); //查找後關閉游標
ArrayAdapter<String> adpater = new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,strlist);
lv.setAdapter(adpater);
setContentView(lv);
}
}
將參數也復制到該項目工程中去
清單文件AndroidManifest.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gtl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gtl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
[html]
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
[html]
<activity android:name=".data.two.Test">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.dir/vnd.ruixin.login" />
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.item/vnd.ruixin.login" />
</intent-filter>
</activity>
<provider android:name=".data.two.MyProvider"
android:authorities="com.gtl.data.two"></provider>
<activity android:name=".data.two.Test">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.dir/vnd.ruixin.login" />
</intent-filter>
<intent-filter>
<data android:mimeType="vnd.android.cursor.item/vnd.ruixin.login" />
</intent-filter>
</activity>
<provider android:name=".data.two.MyProvider"
android:authorities="com.gtl.data.two"></provider>
[html]
</application>
lt;/manifest>
</application>
</manifest>
把github上的PagerSlidingTabStrip稍作修改: tab的文字顏色選中變色(原版文字不變色) 栗子:http://download.csdn.ne
首先來看效果: 一、實現原理在實現過程中,主要考慮整個界面由若干個字母組成的子母線條組成,這樣的話把固定數量的字母封裝成一個字母線條,而每個字母又封裝成一個對象,這樣的話
看了很長時間Vold存儲模塊的相關知識,也深入的研究一段時間的Android源碼,打算把自己看過的經驗之貼、參考資料和自己的一些見解,以帖子的形式發出來,供有興趣的同仁們
[Android]仿京東手機端類別頁京東手機端的類別標簽頁, 是一個左側滑動可選擇類別, 右側一個類別明細的列表聯動頁面. 當用戶選擇左側選項, 可在右側顯示更多選項來選