실행 안됨 실패 - 참고용
앞 블로그의 어플에서 추가
ContentProvider 를 추가한다.
1. DB를 제공하는 어플
class MemberInfoProvider extends ContentProvider
package com.example.choi.mystudy27_1;
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;
import android.util.Log;
/**
* Created by choi on 2017-03-16.
*/
public class MemberInfoProvider extends ContentProvider {
final static String TAG = "MemberInfoProvider";
//저작자 표시
private static final String AUTHORITY = "com.example.choi.mystudy27_1.MemberInfoProvider";
private static final String PATH = "memberInfo";
private static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH);
private final static int INFO_ALL = 1;
private final static int INFO_ONE = 2;
private static UriMatcher matcher = null;
static {
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI(AUTHORITY, PATH, INFO_ALL);
matcher.addURI(AUTHORITY, PATH + "/*", INFO_ONE);
}
SQLiteDatabase db = null;
@Override
public boolean onCreate() {
MemberInfoHelper memberInfoHelper = new MemberInfoHelper(getContext());
db = memberInfoHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
StringBuffer query = new StringBuffer();
query.append("SELECT uname, uid, upw FROM member");
if(matcher.match(uri) == INFO_ONE) {
query.append(" WHERE uid = '" + uri.getPathSegments().get(1) + "'");
}
Log.i(TAG, "query : " + query.toString());
Cursor cursor = db.rawQuery(query.toString(), null);
return cursor;
}
@Override
public String getType(Uri uri) {
Log.i(TAG, "getType()");
if (matcher.match(uri) == INFO_ALL) {
return "vnd.example.choi.mystudy27_1.cursor.item/member";
}
if (matcher.match(uri) == INFO_ONE) {
return "vnd.example.android_27_2_ex1.cursor.dir/member";
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long row = db.insert("member", null, values);
if(row > 0) {
Uri uri2 = ContentUris.withAppendedId(CONTENT_URI, row);
getContext().getContentResolver().notifyChange(uri2, null);
return uri2;
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
// switch (matcher.match(uri)) {
// case INFO_ALL:
// count = db.delete("member", selection, selectionArgs);
// break;
//
// case INFO_ONE:
// String where = "";
// where = "uid = '" + uri.getPathSegments().get(1) + "'";
// if(TextUtils.isEmpty(selection) == false) {
// where += " AND" + selection;
// }
// count = db.delete("memeber", where, selectionArgs);
// break;
// }
//
// getContext().getContentResolver().notifyChange(uri, null);
StringBuffer query = new StringBuffer();
query.append("DELETE FROM member");
if(matcher.match(uri) == INFO_ONE) {
query.append( " WHERE uid = '" + uri.getPathSegments().get(1) + "'");
}
db.execSQL(query.toString());
count = 1;
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
switch (matcher.match(uri)) {
case INFO_ALL:
count = db.update("mamber", values, selection, selectionArgs);
break;
case INFO_ONE:
String where = "";
where = "uid = '" + uri.getPathSegments().get(1) + "'";
if(TextUtils.isEmpty(selection) == false) {
where += " AND" + selection;
}
count = db.update("member", values, where, selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
AndroidMainfest.xml
<provider android:name="MemberInfoProvider"
android:authorities="com.example.choi.mystudy27_1.MemberInfoProvider">
</provider>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.choi.mystudy27_1"
>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="MemberInfoProvider"
android:authorities="com.example.choi.mystudy27_1.MemberInfoProvider">
</provider>
</application>
</manifest>
2. DB를 제공받는 어플
class MainActivity
package com.example.choi.mystudy27_2;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
final static String TAG="MainActivity";
private static final String AUTHORITY = "com.example.choi.mystudy27_1.MemberInfoProvider";
private static final String PATH = "memberInfo";
private static final String UID="ghi";
private static Uri CONTENT_URI_MEMBERS=Uri.parse("content://"+AUTHORITY+"/"+PATH);
private static Uri CONTENT_URI_MEMBER=Uri.parse("content://"+AUTHORITY+"/"+PATH+"/"+UID);
ContentResolver cr=null;
Button btnInsert, btnDelete, btnUpdate, btnSelectAll, btnSelectOne;
EditText tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cr=getContentResolver();
btnInsert=(Button)findViewById(R.id.bt_insert);
btnDelete=(Button)findViewById(R.id.bt_delete);
btnUpdate=(Button)findViewById(R.id.update);
btnSelectAll=(Button)findViewById(R.id.bt_selet_all);
btnSelectOne=(Button)findViewById(R.id.bt_selet_one);
tvResult=(EditText)findViewById(R.id.tv_result);
btnInsert.setOnClickListener(listener);
btnSelectOne.setOnClickListener(listener);
btnSelectAll.setOnClickListener(listener);
btnUpdate.setOnClickListener(listener);
btnDelete.setOnClickListener(listener);
}
View.OnClickListener listener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_insert:
try{
ContentValues values=new ContentValues();
values.put("uname", "홍길자");
values.put("uid", UID);
values.put("upw", 789);
cr.insert(CONTENT_URI_MEMBERS, values);
Toast.makeText(MainActivity.this, "INSERT OK!", Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "INSERT NG!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.bt_selet_one:
Cursor cursor2=null;
try{
cursor2=cr.query(CONTENT_URI_MEMBER, null, null, null, null);
StringBuffer sb=new StringBuffer();
if(cursor2.moveToFirst()){
}
tvResult.setText(sb.toString());
Toast.makeText(MainActivity.this, "SELECT_ONE OK!", Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "SELECT_ONE NG!", Toast.LENGTH_SHORT).show();
}finally {
try {
if(cursor2 !=null)cursor2.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
break;
case R.id.bt_selet_all:
Cursor cursor =null;
try {
cursor=cr.query(CONTENT_URI_MEMBERS, null, null, null, null);
StringBuffer sb =new StringBuffer();
while (cursor.moveToNext()){
String uname=cursor.getString(0);
String uid=cursor.getString(1);
int upw=cursor.getInt(2);
sb.append("uname : " + uname +
", uid : " + uid + ", upw :" +
upw + "\n");
}
tvResult.setText(sb.toString());
Toast.makeText(MainActivity.this, "SELECT_ALL OK!", Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
Toast.makeText(MainActivity.this, "SELECT_ALL NG!", Toast.LENGTH_SHORT).show();
}finally {
try {
if(cursor !=null)cursor.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
break;
}
}
};
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.choi.mystudy27_2.MainActivity">
<!--읽기전용-->
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:inputType="textMultiLine"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/tv_result"
android:layout_weight="1"
android:focusableInTouchMode="false"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
<Button
android:text="INSERT"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/bt_insert"
android:background="@android:color/holo_blue_dark"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
<Button
android:text="SELECT One"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/bt_selet_one"
android:background="@android:color/holo_red_dark"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
<Button
android:text="update"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/update"
android:background="@android:color/holo_orange_dark"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
<Button
android:text="DELETE"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/bt_delete"
android:background="@android:color/holo_green_dark"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
<Button
android:text="Select all"
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/bt_selet_all"
android:background="@android:color/holo_purple"
android:textColor="@android:color/background_light"
android:layout_weight="1"
/>
</LinearLayout>
</LinearLayout>
댓글 ( 4)
댓글 남기기