Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

InLab Semina

Android Semin InLab
by

Shin DongMin

on 14 April 2010

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of InLab Semina

package com.sdm.ContentProviderDemo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

public class Provider extends ContentProvider{

public int delete(Uri uri, String selection, String[] selectionArgs) { return 0; }
public String getType(Uri uri) { return null; }
public Uri insert(Uri uri, ContentValues values) { return null; }
public boolean onCreate() { return false; }
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {return null; }
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) { return 0; }
}
public class Provider extends ContentProvider {

private static final String DATABASE_NAME="InLab.db";
private static final int CONSTANTS=1;
private static final int CONSTANT_ID=2;
private static final UriMatcher MATCHER;
private static HashMap<String, String>
CONSTANTS_LIST_PROJECTION;

public static final class Constants implements BaseColumns {
public static final Uri CONTENT_URI
= Uri.parse("content://com.sdm.ContentProviderDemo.Provider/InLab");

public static final String DEFAULT_SORT_ORDER="name";
public static final String NAME="name";
public static final String PHONE="phone";
}

static {
MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
MATCHER.addURI("com.sdm.ContentProviderDemo.Provider", "InLab", CONSTANTS);
MATCHER.addURI("com.sdm.ContentProviderDemo.Provider", "InLab/#", CONSTANT_ID);

CONSTANTS_LIST_PROJECTION=new HashMap<String, String>();
CONSTANTS_LIST_PROJECTION.put(Provider.Constants._ID, Provider.Constants._ID);
CONSTANTS_LIST_PROJECTION.put(Provider.Constants.NAME, Provider.Constants.NAME);
CONSTANTS_LIST_PROJECTION.put(Provider.Constants.PHONE, Provider.Constants.PHONE);
} public String getDbName() {
return(DATABASE_NAME);
}

public int getDbVersion() {
return(1);
}

private SQLiteDatabase db;

@Override
public boolean onCreate() {
db=(new DatabaseHelper(getContext())).getWritableDatabase();
return (db == null) ? false : true;
}

@Override
public String getType(Uri url) {
if (isCollectionUri(url)) {
return(getCollectionType());
}

return(getSingleType());
} public Uri insert(Uri url, ContentValues initialValues) {
long rowID;
ContentValues values;

if (initialValues!=null) {
values=new ContentValues(initialValues);
} else {
values=new ContentValues();
}

if (!isCollectionUri(url)) {
throw new IllegalArgumentException(" URL : " + url);
}

for (String colName : getRequiredColumns()) {
if (values.containsKey(colName) == false) {
throw new IllegalArgumentException("Column : "+colName);
}
}

populateDefaultValues(values);

rowID=db.insert(getTableName(), getNullColumnHack(), values);

if (rowID > 0) {
Uri uri=ContentUris.withAppendedId(getContentUri(), rowID);
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}

throw new SQLException("Failed to insert row into " + url);
} @Override
public Cursor query(Uri url, String[] projection, String selection, String[]
selectionArgs, String sort) {

SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
qb.setTables(getTableName());

if (isCollectionUri(url)) {
qb.setProjectionMap(getDefaultProjection());
} else {
qb.appendWhere(getIdColumnName()+"="+url.getPathSegments().get(1));
}

String orderBy;

if (TextUtils.isEmpty(sort)) {
orderBy=getDefaultSortOrder();
} else {
orderBy=sort;
}

Cursor c=qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
c.setNotificationUri(getContext().getContentResolver(), url);
return c;
} @Override
public int delete(Uri url, String where, String[] whereArgs) {
int count;
long rowId=0;

if (isCollectionUri(url)) {
count=db.delete(getTableName(), where, whereArgs);
} else {

String segment=url.getPathSegments().get(1);
rowId=Long.parseLong(segment);
count=db.delete(getTableName(), getIdColumnName()+"="
+ segment + (!TextUtils.isEmpty(where) ? " AND (" + where
+ ')' : ""), whereArgs);
}

getContext().getContentResolver().notifyChange(url, null);
return count;
} @Override
public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
int count;

if (isCollectionUri(url)) {
count=db.update(getTableName(), values, where, whereArgs);
} else {

String segment=url.getPathSegments().get(1);
count=db.update(getTableName(), values, getIdColumnName()+"="
+ segment+ (!TextUtils.isEmpty(where) ? " AND (" + where+ ')' : ""), whereArgs);
}

getContext().getContentResolver().notifyChange(url, null);
return count;
} private boolean isCollectionUri(Uri url) {
return(MATCHER.match(url)==CONSTANTS);
}

private HashMap<String, String> getDefaultProjection() {
return(CONSTANTS_LIST_PROJECTION);
}

private String getTableName() {
return("InLab");
}

private String getIdColumnName() {
return("_id");
}

private String getDefaultSortOrder() {
return("name");
}

private String getCollectionType() {
return("vnd.android.cursor.dir/vnd.sdm.InLab");
}

private String getSingleType() {
return("vnd.android.cursor.item/vnd.sdm.InLab");
}

private String[] getRequiredColumns() {
return(new String[] {"name"});
}

private void populateDefaultValues(ContentValues values) {
Long now=Long.valueOf(System.currentTimeMillis());
Resources r=Resources.getSystem();

if (values.containsKey(Provider.Constants.PHONE) == false) {
values.put(Provider.Constants.PHONE, "NULL");
}
}

private String getNullColumnHack() {
return("name");
}

private Uri getContentUri() {
return(Provider.Constants.CONTENT_URI);
} private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='InLab'", null);

try {
if (c.getCount()==0) {
db.execSQL("CREATE TABLE InLab (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone TEXT);");

ContentValues cv=new ContentValues();

cv.put(Constants.NAME, "");
cv.put(Constants.PHONE, "01043703393");
db.insert(getTableName(), getNullColumnHack(), cv);

cv.put(Constants.NAME, "");
cv.put(Constants.PHONE, "01732819333");
db.insert(getTableName(), getNullColumnHack(), cv);

cv.put(Constants.NAME, "");
cv.put(Constants.PHONE, "01883211321");
db.insert(getTableName(), getNullColumnHack(), cv);
}
} finally {
c.close();
}
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("Constants", "Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS InLab");
onCreate(db);
}
}
ContentProviderDemo Code public class dbTestDemo extends ListActivity {
private Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

SQLiteDatabase dbase = this.openOrCreateDatabase("AddresBook", MODE_PRIVATE, null);
dbase.execSQL("drop table if exists users");
dbase.execSQL("create table users( _id integer primary key autoincrement, " +
"name text not null, phone text not null);" );

dbase.execSQL("insert into users (name, phone) values('','010-4370-3393');" );

ContentValues insertValue = new ContentValues();
insertValue.put("name", "JangsuHan");
insertValue.put("phone", "010-8183-2271");
dbase.insert("users", null, insertValue);

insertValue.put("name", "JunChul");
insertValue.put("phone", "010-9382-9311");
dbase.insert("users", null, insertValue);

cursor = dbase.query("users", new String[]{"name","phone"},null,null,null,null,"_id","5"); SQLite Demo ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();

if(cursor != null)
if(cursor.moveToFirst()){
do{
HashMap<String, String> item = new HashMap<String, String>();
for(int i = 0 ; i < cursor.getColumnCount(); i++ ){
item.put(cursor.getColumnName(i), cursor.getString(i));
}
list.add(item);
}while(cursor.moveToNext());
}

SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.main,
new String[]{"name", "phone"},
new int[] {R.id.TextView01, R.id.TextView02});
this.setListAdapter(adapter);

if(dbase != null)
dbase.close();

}
} Insert Menu Delete Menu Thank you~ ganana
Full transcript