Androidda Sqlite ma'lumotlar bazasi bilan ishlash . (2 - qism)



Assalomu alaykum Hurmatli o’quvchilar!
Oldingi darsimizni davom ettiramiz sizlar bilan.
Bu darsimizda kiritgan malumotlar ustida tahrirlash ishlarini amalga oshiramiz.
Ya’ni bularga:
  Tanlangan ma’lumotni o’zgartirish
  Tanlangan ma’lumotni o’chirish

  
Oldingi maqolani o’qigan bo’lsangiz, biz database dagi ma’lumotlarni ListView ga chiqargan edik.
Endi o’sha ListView dagi ma’lumotlarni har bittasi bilan alohida ishlashni, tahrirlashni o’rganamiz.


Oldingi maqoladagi dastur kodini ko’chirib olgan bo’lsangiz ushani ochamiz va davom ettiramiz.

  Qilayotgan ishimiz bo’yicha ozroq tasavvurga ega bo’lishimiz uchun quydagi rasmga e’tibor berishingizni so’rayman;
  
Ko’rib turganingizdek ushbu kichkina oynachani AlertDialog deyishadi, bu haqda keyingi maqolalarda albatta batafsil to’xtalib o’tamiz. AlertDialog da 2 ta EditText va 3 ta tugma bor:
  Cancel — bekor qilish
  Update — ma’lumotni yangilash
  Delete — ma’lumotni o’chirish
  
  3ta tugma bor.
  
  Endi Activity ni ochib ushbu rasmdagi AlertDialog ni yasaymiz.
  
              AlertDialog.Builder myDialog
                      = new AlertDialog.Builder(SqliteActivity.this);

  
   //Dialog ga sarlavha berish
  myDialog.setTitle("Delete/Edit?");

Ushbu AlertDialogimiz quyidagi komponentalardan tashkil topgan:
TextView
EditText
Button


Endi bu Alert uchun Layout yaratamiz, yani kod orqali:
TextView ni yaratamiz va joylashtiramiz:
TextView dialogTxt_id = new TextView(MyActivity.this);
            ViewGroup.LayoutParams dialogTxt_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
            dialogTxt_id.setText("#" + String.valueOf(item_id));


EditText ni yaratamiz va joylashtiramiz:
final EditText dialogC1_id = new EditText(MyActivity.this);
            ViewGroup.LayoutParams dialogC1_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogC1_id.setLayoutParams(dialogC1_idLayoutParams);
            dialogC1_id.setText(item_content1);


            final EditText dialogC2_id = new EditText(MyActivity.this);
            ViewGroup.LayoutParams dialogC2_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogC2_id.setLayoutParams(dialogC2_idLayoutParams);
            dialogC2_id.setText(item_content2);


Endi ushbu komponentalarni hammasini LinearLayoutga joylaymiz:
LinearLayout layout = new LinearLayout(MyActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(dialogTxt_id);
            layout.addView(dialogC1_id);
            layout.addView(dialogC2_id);
            myDialog.setView(layout);


AlertDialog ning yarimi tayyor, endi tugmalarni joylashtirib chiqamiz:
Chap tarafga tugma quyish uchun yani Cancel tugmasi uchun va ushbu tugma bosilganda bajariladigan ishlar:
myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });


O'rtadagi tugma ya'ni Update tugmasini hosil qilamiz:
myDialog.setNeutralButton("Update", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    String value1 = dialogC1_id.getText().toString();
                    String value2 = dialogC2_id.getText().toString();
                    mySQLiteAdapter.update_byID(item_id, value1, value2);
                    updateList();
                }
            });


Bu yerda qanday jarayon sodir bo'ladi degan savol paydo bo'lishi mumkin. Update tugmasi,Tanlangan ma'lumotni o'zgartirgandan keyin, EditText1 va EditText2 dagi qiymatlarni olib bazadagi bazadagi malumotni yangilab qo'yadi.

O'ng tarafdagi tugma ya'ni Delete tugmasi, ma'lumotni o'chirish uchun hizmat qiladi:
myDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    mySQLiteAdapter.delete_byID(item_id);
                    updateList();
                }
            });

Tanlangan ma'lumotni o'chiradi va listView ni update(yangilaydi)qiladi.

va ohirida:
myDialog.show();


Endi ushbu yozganlarimizni hammasini ListView ning Item ini bosilgan holat, yani ro'yhatdagi har bir item bosilgan holatda ishga tushuvchi OnItemClickListener metodini ichiga joylaymiz:

private ListView.OnItemClickListener listContentOnItemClickListener
            = new ListView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // TODO Auto-generated method stub

            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            final int item_id = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.KEY_ID));
            String item_content1 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.ism));
            String item_content2 = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.familya));

            AlertDialog.Builder myDialog
                    = new AlertDialog.Builder(MyActivity.this);

            myDialog.setTitle("Delete/Edit?");

            TextView dialogTxt_id = new TextView(MyActivity.this);
            ViewGroup.LayoutParams dialogTxt_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogTxt_id.setLayoutParams(dialogTxt_idLayoutParams);
            dialogTxt_id.setText("#" + String.valueOf(item_id));

            final EditText dialogC1_id = new EditText(MyActivity.this);
            ViewGroup.LayoutParams dialogC1_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogC1_id.setLayoutParams(dialogC1_idLayoutParams);
            dialogC1_id.setText(item_content1);

            final EditText dialogC2_id = new EditText(MyActivity.this);
            ViewGroup.LayoutParams dialogC2_idLayoutParams
                    = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            dialogC2_id.setLayoutParams(dialogC2_idLayoutParams);
            dialogC2_id.setText(item_content2);

            LinearLayout layout = new LinearLayout(MyActivity.this);
            layout.setOrientation(LinearLayout.VERTICAL);
            layout.addView(dialogTxt_id);
            layout.addView(dialogC1_id);
            layout.addView(dialogC2_id);
            myDialog.setView(layout);

            myDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    mySQLiteAdapter.delete_byID(item_id);
                    updateList();
                }
            });

            myDialog.setNeutralButton("Update", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {
                    String value1 = dialogC1_id.getText().toString();
                    String value2 = dialogC2_id.getText().toString();
                    mySQLiteAdapter.update_byID(item_id, value1, value2);
                    updateList();
                }
            });

            myDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                // do something when the button is clicked
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

            myDialog.show();
        }};


Activity dagi onCreate metodiga ushbu qatorni qo'shib qo'yishni unutmang:
listContent.setOnItemClickListener(listContentOnItemClickListener);


Endi SqliteAdapter ni ichida yuqorida ishlatgan update_byID va delete_byID metodlarini yaratamiz, bular, tanlangan item ni ID sini olib database ga so'rov yuboradi, va usha qaytgan natijani tahrirlab yana saqlab quyadi yoki o'sha tanlangan ID li ma'lumotni o'chirib tashlaydi

public void delete_byID(int id){
        sqLiteDatabase.delete(MYDATABASE_TABLE, KEY_ID+"="+id, null);
    }

    public void update_byID(int id, String v1, String v2){
        ContentValues values = new ContentValues();
        values.put(KEY_CONTENT1, v1);
        values.put(KEY_CONTENT2, v2);
        sqLiteDatabase.update(MYDATABASE_TABLE, values, KEY_ID+"="+id, null);
    }


Endi ishga tushirsak ham bo'ladi:


Endi sizda yana savol paydo bo'lishi mumkin. Database telefonning qayerida saqlanadi, uni olish mumkinmi, ko'rish, ichidagi ma'lumotlarni o'zgartirish mumkin degan?

Men IntellIJ IDEA Community Edition ni ishlataman, Tools->Android->Monitor(DDMS included) ga kirsangiz quydigi oyna chiqadi:

File Explorer ga kirib, «data/date/sizning_dasturingiz_packet_nomi/databases/sizning_databaseingiz



O'ng taraf qizil aylanani ichidagi tugmalar yordamida database ni kompyuterga olish mumkin,va uni Sqlite Database Browser dasturi yordamida ochib o'zgartirishingiz mumkin, va uni qaytadan ush joyga ham qo'yib qo'ysangiz bo'ladi, bu juda oson.

Dastur kodini ko'chirishni unutmang. Ko'chirib olish.
Keyingi darslarimizni kuzatib boring, Sqlite database bo'yicha darslar hali tugamadi…

Yii2 frameworkda frontend va backend sozlanmalarini sozlash(Apache)

Yii2 framework 2 xil template mavjud; Bularni yaxshi bilsangiz kerak, bular basic va advanced; Bularni farqlarini quyidagi rasm orqali bil...