Quyida src/com.example.MyApplication/MainActivity.java o'zgartirilgan asosiy faoliyat faylining mazmuni keltirilgan . Ushbu fayl asosiy hayot aylanish usullarining har birini o'z ichiga olishi mumkin. Biz ilova bilan foydalanuvchi o'zaro aloqasini boshqarish uchun ikkita yangi onClickAddName() va onClickRetrieveStudents() usullarini qo'shdik. package.com _ _ misol . Mening ilovam ;
androidni import qilish . to'r . Uri ; androidni import qilish . os . to'plam ; androidni import qilish . ilova . Faoliyat ;
androidni import qilish . mazmuni . ContentValues ; androidni import qilish . mazmuni . CursorLoader ;
androidni import qilish . ma'lumotlar bazasi . kursor ;
androidni import qilish . ko'rish . menyu ; androidni import qilish . ko'rish . ko'rish ;
androidni import qilish . vidjet . Matnni tahrirlash ; androidni import qilish . vidjet . tushdi ;
umumiy sinf MainActivity faollikni kengaytiradi {
@Override himoyalangan void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState );
setContentView ( R. layout . activity_main ) ; } public void onClickAddName ( Ko'rinishni ko'rish ) { // Yangi talaba yozuvini qo'shish ContentValues qiymatlari = yangi ContentValues ();
qadriyatlar . qo'ying ( StudentsProvider
. NAME , (( EditText ) findViewById ( R . id . editText2 )). getText (). toString ());
qadriyatlar . put ( StudentsProvider . GRADE , (( EditText ) findViewById ( R . id . editText3 )). getText (). toString ());
Uri uri = getContentResolver (). kiritish ( StudentsProvider . CONTENT_URI , qiymatlar );
Tost . makeText ( getBaseContext (),
uri . toString (), Toast . LENGTH_LONG ). ko'rsatish (); } public void onClickRetrieveStudents ( View view ) { // Talaba yozuvlarini olish String URL = "content://com.example.MyApplication.StudentsProvider" ;
Uri talabalari = Uri . tahlil qilish ( url ); Kursor c = managedQuery ( talabalar , null , null , null , "nom" );
agar ( c . moveToFirst ()) { qilish { Tost . makeText ( bu ,
c . getString ( c . getColumnIndex ( StudentsProvider . _ID )) + ", " + c . getString ( c . getColumnIndex ( StudentsProvider . NAME )) + ", " + c . getString ( c .
getColumnIndex ( StudentsProvider . GRADE )), Tost . LENGTH_SHORT ). ko'rsatish (); } while ( c . moveToNext ()); } } }
com.example.MyApplication paketida yangi StudentsProvider.java faylini yarating va quyida src/com.example.MyApplication/StudentsProvider.java mazmuni mavjud -
package.com _ _ misol . Mening ilovam ;
java importi . util . HashMap ;
androidni import qilish . mazmuni . ContentProvider ; androidni import qilish . mazmuni . ContentUris ; androidni import qilish . mazmuni . ContentValues ; androidni import qilish . mazmuni . Kontekst ; androidni import qilish . mazmuni . UriMatcher ;
androidni import qilish . ma'lumotlar bazasi . kursor ; androidni import qilish . ma'lumotlar bazasi . SQLException ;
androidni import qilish . ma'lumotlar bazasi . sqlite . SQLiteDatabase ; androidni import qilish . ma'lumotlar bazasi . sqlite . SQLiteOpenHelper ; androidni import qilish . ma'lumotlar bazasi . sqlite . SQLiteQueryBuilder ;
androidni import qilish . to'r . Uri ; androidni import qilish . matn . TextUtils ;
umumiy sinf StudentsProvider ContentProvider kengaytiradi { statik final String PROVIDER_NAME = "com.example.MyApplication.StudentsProvider" ; statik yakuniy String URL = "content://" + PROVIDER_NAME + "/students" ; statik yakuniy Uri CONTENT_URI = Uri . tahlil qilish ( url );
statik final String _ID = "_id" ; statik final String NAME = "ism" ; statik yakuniy String GRADE = "baho" ;
@Override public void onCreate ( SQLiteDatabase
JDB ) { db . execSQL ( CREATE_DB_TABLE ); }
@Override public void onUpgrade ( SQLiteDatabase JDB , int oldVersion , int newVersion ) {
db . execSQL ( "AGAR MAVJUD BO'LSA JADVALNI TO'CHIRISH" + STUDENTS_TABLE_NAME );
onCreate ( jB ); } }
@Override public boolean onCreate () { Kontekst konteksti = getContext (); DatabaseHelper dbHelper = yangi DatabaseHelper ( kontekst );
/**
* Yozilishi mumkin bo'lgan ma'lumotlar bazasini yarating, bu uni ishga tushiradi
* agar u allaqachon mavjud bo'lmasa yaratish.
*/
db = dbHelper . getWritableDatabase (); qaytish ( db == null )? noto'g'ri : rost ; }
@Ommaviy Uri qo'shimchasini bekor qilish ( Uri uri , ContentValues qiymatlari ) { /**
* Yangi talaba rekordini qo'shing
*/ uzun qator ID = JB . kiritish ( STUDENTS_TABLE_NAME , "" , qiymatlar );
/**
* Yozuv muvaffaqiyatli qo'shilsa
*/ if ( rowID > 0 ) { Uri _uri = ContentUris . withAppendedId ( CONTENT_URI , rowID );
getContext (). getContentResolver (). notifyChange ( _uri , null ); return_uri ; _ }
yangi SQLExceptionni oching ( " + uri ga yozuv qo'shib bo'lmadi ); }
@Override umumiy kursor so'rovi ( Uri uri , String [] proyeksiya , String tanlash , String [] selectionArgs , String sortOrder ) { SQLiteQueryBuilder qb = yangi SQLiteQueryBuilder ();
qb . setTables ( STUDENTS_TABLE_NAME );
switch ( uriMatcher . match ( uri )) { case STUDENTS :
qb . setProjectionMap ( STUDENTS_PROJECTION_MAP ); sindirish ;
Case STUDENT_ID :
qb . appendWhere ( _ID + "=" + uri .getPathSegments (). olish ( 1 ) ); sindirish ;
standart : }
if ( sortOrder == null || sortOrder == "" ){ /**
* Odatiy bo'lib talaba ismlari bo'yicha tartiblash
*/
sortOrder = NAME ; }
Kursor c = qb . so'rov ( db , proyeksiya , tanlash ,
selectArgs , null , null , sortOrder ); /**
* o'zgarishlar uchun URI kontentini ko'rish uchun ro'yxatdan o'ting
*/
c . setNotificationUri ( getContext (). getContentResolver (), uri ); qaytish c ; }
@Override public int delete ( Uri uri , String tanlash , String [] selectionArgs ) { int count = 0 ; switch ( uriMatcher . match ( uri )) { case STUDENTS :
count = db . o'chirish ( STUDENTS_TABLE_NAME , tanlash , tanlashArgs ); sindirish ;
case STUDENT_ID : String identifikatori = uri . getPathSegments (). olish ( 1 );
hisoblash = JB . o'chirish ( STUDENTS_TABLE_NAME , _ID + " = " + id + (! TextUtils . isEmpty ( tanlov ) ? "
AND (" + tanlov + ')' : "" ), selectArgs ); tanaffus ; sukut bo'yicha : yangi IllegalArgumentException ( "Noma'lum URI" + uri ) ni oching ; } getContext (). getContentResolver (). notifyChange ( uri , null ); qaytish soni ; }
@Ommaviy int yangilanishini bekor qilish ( Uri uri , ContentValues qiymatlari , String tanlash , String [] selectionArgs ) { int count = 0 ; switch ( uriMatcher . match ( uri )) { case Talabalar :
count = db . yangilash ( STUDENTS_TABLE_NAME , qiymatlar , tanlov ,
AND (" + tanlov + ')' : "" ), selectArgs ); tanaffus ; sukut bo'yicha : yangi IllegalArgumentException ( "Noma'lum URI" + uri ) ni oching ; }
getContext (). getContentResolver (). notifyChange ( uri , null ); qaytish soni ; }
@Override public String getType ( Uri uri ) { switch ( uriMatcher . match ( uri )){ /**
* Barcha talaba yozuvlarini oling
*/ case STUDENTS : return "vnd.android.cursor.dir/vnd.example.students" ; /**
* Muayyan talabani oling
*/ case STUDENT_ID : "vnd.android.cursor.item/vnd.example.students" ni qaytaring ; sukut bo'yicha : yangi IllegalArgumentExceptionni oching ( "Qo'llab-quvvatlanmaydigan URI: " + uri ); } } }