編輯:關於Android編程
package com.example.domain; public class Person { private int id; private String name; public Person() { super(); } public Person(int personid, String name) { this.id = personid; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.example.service; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.domain.Person; public class PersonService { private DBOpenHelper dbOpenHelper; //private Context context; public PersonService(Context context) { this.dbOpenHelper = new DBOpenHelper(context); } public void save(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL("insert into person (name) values (?)", new Object[] { person.getName() }); } public void update(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL("update person set name=? where personid=?", new Object[] { person.getName(), person.getId() }); } public void delete(Integer id) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL("delete from person where personid=?", new Object[] { id.toString() }); } public Person find(Integer id) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person where personid=?", new String[] { id.toString() }); if (cursor.moveToFirst()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("personid")); return new Person(personid, name); } return null; } public ListgetScrollDate(Integer offset, Integer maxResult) { List persons = new ArrayList (); SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from person limit ?,?", new String[] { offset.toString(), maxResult.toString() }); while (cursor.moveToNext()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("personid")); Person person = new Person(personid, name); persons.add(person); } cursor.close(); return persons; } public long getCount() { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.rawQuery("select count(*) from person ", null); cursor.moveToFirst(); return cursor.getLong(0); } }
package com.example.service; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.domain.Person; public class OtherPersonService { private DBOpenHelper dbOpenHelper; public OtherPersonService(Context context) { this.dbOpenHelper = new DBOpenHelper(context); } public void save(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", person.getName()); db.insert("person", null, values); } public void update(Person person) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name", person.getName()); db.update("person", values, "personid=?", new String[] { (new Integer( person.getId())).toString() }); } public void delete(Integer id) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.delete("person", "personid=?", new String[] { id.toString() }); } public Person find(Integer id) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[] { "personid", "name" }, "personid=?", new String[] { id.toString() }, null, null, null); if (cursor.moveToFirst()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("personid")); return new Person(personid, name); } return null; } public ListgetScrollDate(Integer offset, Integer maxResult) { List persons = new ArrayList (); SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.query("person", null, null, null, null, null, null, offset + "," + maxResult); while (cursor.moveToNext()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("personid")); Person person = new Person(personid, name); persons.add(person); } cursor.close(); return persons; } public long getCount() { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); Cursor cursor = db.query("person", new String[] { "count(*)" }, null, null, null, null, null); cursor.moveToFirst(); return cursor.getLong(0); } }
package com.example.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { private static String databasename = "datatemp"; private static int databaseversion = 1; public DBOpenHelper(Context context) { super(context, databasename, null, databaseversion); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS person (personid integer primary key autoincrement, name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { } }
package com.example.db; import com.example.service.DBOpenHelper; 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; public class PersonProvider extends ContentProvider { private DBOpenHelper dbOpenHelper; private static final UriMatcher MATCHER = new UriMatcher( UriMatcher.NO_MATCH); private static final int PERSONS = 1; private static final int PERSON = 2; static { MATCHER.addURI("org.provider", "person", PERSONS); MATCHER.addURI("org.provider", "person/#", PERSON); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.delete("person", selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if (selection != null && !"".equals(selection)) { where = selection + "and" + where; } count = db.delete("person", where, selectionArgs); return count; default: throw new IllegalArgumentException("Unknown Uri" + uri.toString()); } } @Override public String getType(Uri uri) { switch (MATCHER.match(uri)) { case PERSONS: return "vnd.android.curson.dir/person"; case PERSON: return "vnd.android.curson.item/person"; default: throw new IllegalArgumentException("Unknown Uri" + uri.toString()); } } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: long rowid = db.insert("person", "personid", values); Uri insertUri = ContentUris.withAppendedId(uri, rowid); return insertUri; default: throw new IllegalArgumentException("Unknown Uri" + uri.toString()); } } @Override public boolean onCreate() { this.dbOpenHelper = new DBOpenHelper(this.getContext()); return false; } @Override public Cursor query(Uri uri, String[] columns, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: return db.query("person", columns, selection, selectionArgs, null, null, sortOrder); case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if (selection != null && !"".equals(selection)) { where = selection + "and" + where; } return db.query("person", columns, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unknown Uri" + uri.toString()); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int count = 0; switch (MATCHER.match(uri)) { case PERSONS: count = db.update("person", values, selection, selectionArgs); return count; case PERSON: long id = ContentUris.parseId(uri); String where = "personid=" + id; if (selection != null && !"".equals(selection)) { where = selection + "and" + where; } count = db.update("person", values, where, selectionArgs); return count; default: throw new IllegalArgumentException("Unknown Uri" + uri.toString()); } } } /* =============================Adroid JUnit test ContentProvider; public class AccessContentProvider extends AndroidTestCase { public void testInsert() throws Throwable { private static final String TAG = "AccessContentProvider"; ContentResolver contentResolver = this.getContext() .getContentResolver(); Uri insertUri = Uri.parse("content://org.provider/person"); ContentValues values = new ContentValues(); values.put("name", "xiaosan"); values.put("amount", 90); Uri uri = contentResolver.insert(insertUri, values); Log.i(TAG, uri.toString()); } public void testUpdate() throws Throwable { ContentResolver contentResolver = this.getContext() .getContentResolver(); Uri updateUri = Uri.parse("content://org.provider/person/1"); ContentValues values = new ContentValues(); values.put("name", "xiaosan"); values.put("amount", 90); contentResolver.update(updateUri, values, null, null); } public void testSelect() throws Throwable { ContentResolver contentResolver = this.getContext() .getContentResolver(); Uri selectUri = Uri.parse("content://org.provider/person"); Cursor cursor = contentResolver.query(selectUri, null, null, null, "personid desc"); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("personid")); String name = cursor.getString(cursor.getColumnIndex("name")); int amount = cursor.getInt(cursor.getColumnIndex("amount")); Log.i(TAG, "id" + id + ",name" + name + ",amount" + amount); } } public void testDelete() throws Throwable { ContentResolver contentResolver = this.getContext() .getContentResolver(); Uri deleteUri = Uri.parse("content://org.provider/person/1"); contentResolver.detele(deleteUri, null, null); } } //// this.getContext().getContentResolver().notifyChange(uri,null); =================================== */
Android中自定義View的實現比較簡單,無非就是繼承父類,然後重載方法,即便如此,在實際編碼中難免會遇到一些坑,我把自己遇到的一些問題和解決方法總結一下,希望對廣大
我們知道iOS上的應用,狀態欄的顏色總能與應用標題欄顏色保持一致,用戶體驗很不錯,那安卓是否可以呢?若是在安卓4.4之前,答案是否定的,但在4.4之後,谷歌允許開發者自定
手機qq聊天記錄裡有重要的信息要怎麼導出?手機qq聊天記錄保存在哪個文件夾,我們一起來看看吧!手機qq導出聊天記錄方法:一、導出與好友的聊天記錄: 操作方
下面就是結果圖(每種狀態用一個表情圖片表示):一、主頁面的布局文件如下:<RelativeLayout xmlns:android=http://schemas.a