안드로이드 개발 질문/답변
(글 수 45,052)
//프로바이더
package android.exam.provider;
import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.net.Uri;
public class PhoneProvider extends ContentProvider {
static final Uri CONTENT_URI = Uri.parse("content://android.exam.provider/phoneBook");
static final int ALL_ROW = 1;
static final int CONDITION_ROW = 2;
static final UriMatcher matcher;
Cursor cursor;
static{
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("android.exam.provider", "phoneBook", ALL_ROW);
matcher.addURI("android.exam.provider", "phoneBook/*", CONDITION_ROW);
}
SQLiteDatabase db;
@Override
public boolean onCreate()
{
DBHelper helper = new DBHelper(getContext());
db = helper.getWritableDatabase();
return true;
}
@Override
public String getType(Uri uri)
{
if(matcher.match(uri) == ALL_ROW)
{
return "vnd.provider.cursor.dir/phoneBook";
}
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
String sql = "select * from phone_list";
switch(matcher.match(uri))
{
case ALL_ROW:
cursor = db.rawQuery(sql,null);
break;
case CONDITION_ROW:
sql+= " where name like '%"+uri.getPathSegments().get(1)+"%' OR" +
" phoneNumber like '%"+uri.getPathSegments().get(1)+"%'";
break;
}
cursor = db.rawQuery(sql, null);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
long row = db.insert("phone_list", null, values);
if(row>0)
{
Uri notiuri = ContentUris.withAppendedId(CONTENT_URI, row);
getContext().getContentResolver().notifyChange(notiuri, null);
return notiuri;
}
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
int count=0;
count = db.delete("phone_list","_id = "+uri.getPathSegments().get(1) ,null);
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context)
{
super(context,"phoneBook.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table phone_list(_id integer primary key autoincrement, name text not null, phoneNumber text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("drop table if exists phone_list");
onCreate(db);
}
}
//리졸버
package android.exam.resolver;
import android.app.Activity; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast;
public class PhoneBook_ContentResolverActivity extends Activity {
static final String CONTENT_URI = "content://android.exam.provider/phoneBook";
EditText keyword;
Button searchBtn, addBtn;
ListView list;
ContentResolver cr;
Cursor cursor;
SimpleCursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
keyword = (EditText)findViewById(R.id.keyword);
searchBtn = (Button)findViewById(R.id.searchBtn);
addBtn = (Button)findViewById(R.id.addBtn);
list = (ListView)findViewById(R.id.list);
cr = getContentResolver();
cursor = cr.query(Uri.parse(CONTENT_URI), null, null, null, null);
//startManagingCursor(cursor);
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[] {"name","phoneNumber"}, new int[] {android.R.id.text1,android.R.id.text2});
list.setAdapter(adapter);
list.setOnItemClickListener(mListener);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
public void mClick(View v)
{
switch(v.getId())
{
case R.id.addBtn:
Intent intent = new Intent(PhoneBook_ContentResolverActivity.this, Add_activity.class);
startActivityForResult(intent, 0);
break;
case R.id.searchBtn:
String text = keyword.getText().toString();
cursor = cr.query(Uri.parse(CONTENT_URI+"/"+text), null, null, null, null);
adapter.changeCursor(cursor);
}
}
AdapterView.OnItemClickListener mListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final long _id = id;
Toast.makeText(getApplicationContext(), ""+id, Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(PhoneBook_ContentResolverActivity.this)
.setTitle("선택")
.setPositiveButton("편집", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("삭제", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
cr.delete(Uri.parse(CONTENT_URI+"/"+_id), null, null);
cursor = cr.query(Uri.parse(CONTENT_URI), null, null, null, null);
adapter.changeCursor(cursor);
Toast.makeText(getApplicationContext(), "delete ok!", Toast.LENGTH_SHORT).show();
}
})
.show();
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case 0:
if(resultCode == RESULT_OK)
{
String name = data.getStringExtra("name");
String phoneNumber = data.getStringExtra("number");
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("phoneNumber", phoneNumber);
cr.insert(Uri.parse(CONTENT_URI), cv);
cursor = cr.query(Uri.parse(CONTENT_URI), null, null, null, null);
adapter.changeCursor(cursor);
Toast.makeText(this, "insert ok!", Toast.LENGTH_SHORT).show();
}
break;
case 1:
break;
}
}
}도대체 왜 삽입, 삭제, 업데이트 후에
getContext().getContentResolver().notifyChange(notiuri, null);
이걸 해도 리졸버에 있는 리스트뷰에는 변화가 없을까요??
자꾸 변화가 없길래 쿼리를 다시 날려서 리스트뷰에 뿌리는데 뭔가 찜찜하네요 ㅜㅜ
왜 그런지 알려주세요 ..




nofifyChange의 의미는 해당 URI에 변화가 생겼으니 옵저버님들은 그리 아세요
라는 의미 입니다.
컨텐트 옵저버 등록해서 재쿼리 한 후 ListView 업데이트 하셨나요?
그 옵저버를 자동으로 관리해주는 코드가 /startManagingCursor(cursor); 이구요 ㅎㅎ
주석 처리 해 놓으셨네요