# Android - CP ( Content Provider ) 제공자, 접근자 : android, CP, Content Provider, 제공자, 접근자
```java
package exam.Data;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.net.*;
import android.text.*;
public class EWProvider extends ContentProvider {
static final Uri CONTENT_URI = Uri.parse("content://exam.Data.EnglishWord/word");
static final int ALLWORD = 1;
static final int ONEWORD = 2;
static final UriMatcher Matcher;
static {
Matcher = new UriMatcher(UriMatcher.NO_MATCH);
Matcher.addURI("exam.Data.EnglishWord", "word", ALLWORD);
Matcher.addURI("exam.Data.EnglishWord", "word/*", ONEWORD);
}
SQLiteDatabase mDB;
public boolean onCreate() {
WordDBHelper helper = new WordDBHelper(getContext());
mDB = helper.getWritableDatabase();
return true;
}
public String getType(Uri uri) {
if (Matcher.match(uri) == ALLWORD) {
return "vnd.EnglishWord.Data.cursor.item/word";
}
if (Matcher.match(uri) == ONEWORD) {
return "vnd.EnglishWord.Data.cursor.dir/words";
}
return null;
}
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String sql;
// 전체에 대한 쿼리 명령
sql = "SELECT eng, han FROM dic";
// 단어 선택 where절 추가
if (Matcher.match(uri) == ONEWORD) {
sql += " where eng = '" + uri.getPathSegments().get(1) + "'";
}
Cursor cursor = mDB.rawQuery(sql, null);
return cursor;
}
public Uri insert(Uri uri, ContentValues values) {
long row = mDB.insert("dic", null, values);
if (row > 0) {
Uri notiuri = ContentUris.withAppendedId(CONTENT_URI, row);
getContext().getContentResolver().notifyChange(notiuri, null);
return notiuri;
}
return null;
}
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
//*
switch (Matcher.match(uri)) {
case ALLWORD:
count = mDB.delete("dic", selection, selectionArgs);
break;
case ONEWORD:
String where;
where = "eng = '" + uri.getPathSegments().get(1) + "'";
if (TextUtils.isEmpty(selection) == false) {
where += " AND" + selection;
}
count = mDB.delete("dic", where, selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
//*/
/*
String sql;
// 전체에 대한 쿼리 명령
sql = "DELETE FROM dic";
// 단어 선택 where절 추가
if (Matcher.match(uri) == ONEWORD) {
sql += " where eng = '" + uri.getPathSegments().get(1) + "'";
}
mDB.execSQL(sql);
return 1;
//*/
}
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (Matcher.match(uri)) {
case ALLWORD:
count = mDB.update("dic", values, selection, selectionArgs);
break;
case ONEWORD:
String where;
where = "eng = '" + uri.getPathSegments().get(1) + "'";
if (TextUtils.isEmpty(selection) == false) {
where += " AND " + selection;
}
count = mDB.update("dic", values, where, selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
package exam.Data;
import android.app.*;
import android.content.*;
import android.database.*;
import android.net.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import exam.AndroidExam.*;
public class CallWordCP extends Activity {
static final String WORDURI = "content://exam.Data.EnglishWord/word";
EditText mText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_callwordcp);
mText = (EditText)findViewById(R.id.edittext);
findViewById(R.id.readall).setOnClickListener(mClickListener);
findViewById(R.id.readone).setOnClickListener(mClickListener);
findViewById(R.id.insert).setOnClickListener(mClickListener);
findViewById(R.id.delete).setOnClickListener(mClickListener);
findViewById(R.id.update).setOnClickListener(mClickListener);
}
Button.OnClickListener mClickListener = new Button.OnClickListener() {
public void onClick(View v) {
ContentResolver cr = getContentResolver();
switch (v.getId()) {
// 전부 읽기
case R.id.readall:
Cursor cursor = cr.query(Uri.parse(WORDURI), null, null, null, null);
String Result = "";
while (cursor.moveToNext()) {
String eng = cursor.getString(0);
String han = cursor.getString(1);
Result += (eng + " = " + han + "\n");
}
if (Result.length() == 0) {
mText.setText("Empyt Set");
} else {
mText.setText(Result);
}
cursor.close();
break;
// 하나만 읽기
case R.id.readone:
Cursor cursor2 = cr.query(Uri.parse(WORDURI + "/boy"),
null, null, null, null);
String Result2 = "";
if (cursor2.moveToFirst()) {
String eng = cursor2.getString(0);
String han = cursor2.getString(1);
Result2 += (eng + " = " + han + "\n");
}
if (Result2.length() == 0) {
mText.setText("Empyt Set");
} else {
mText.setText(Result2);
}
cursor2.close();
break;
// 삽입
case R.id.insert:
ContentValues row = new ContentValues();
row.put("eng", "school");
row.put("han", "학교");
cr.insert(Uri.parse(WORDURI), row);
mText.setText("Insert Success");
break;
// 삭제
case R.id.delete:
cr.delete(Uri.parse(WORDURI), null, null);
mText.setText("Delete Success");
break;
// 수정
case R.id.update:
ContentValues row2 = new ContentValues();
row2.put("han", "핵교");
cr.update(Uri.parse(WORDURI + "/school"), row2, null, null);
mText.setText("Update Success");
break;
}
}
};
}
```
'mobile > android src' 카테고리의 다른 글
Android - EditLimit (0) | 2019.08.09 |
---|---|
Android - EditableTest (0) | 2019.08.08 |
Android - Call Other Intent (0) | 2019.08.06 |
Android - CalcService : android, CalcService (0) | 2019.07.25 |
Android - Event Keyboard (0) | 2019.07.17 |
댓글