کانال بله, جهت پشتیبانی و اطلاع رسانی کانال بله, جهت پشتیبانی و اطلاع رسانی
عضویت

پایگاه داده ی SQlite و content provider (قسمت دوم)

 

clip_image001

دوره آموزش برنامه نویسی اندروید

کلیه حقوق مادی و معنوی این مقاله متعلق به آموزشگاه تحلیل داده می باشد و هر گونه استفاده غیر قانونی از آن پیگرد قانونی دارد.

 

آموزش- پایگاه داده ی SQlite و content provider (بخش2)

آموزش-  پایگاه داده ی SQlite

ایجاد پروژه

پروژه ی جدیدی به نام de.vogella.android.sqlite.first با activity ای به نام TestDatabaseActivity ایجاد کنید.

پایگاه داده و مدل داده ای

در وهله ی اول کلاس MySQLiteHelper را ایجاد کنید، کلاس بیان شده مسئول ایجاد پایگاه داده است. متد onUpgrade () اصولاً همه ی داده های موجود را حذف کرده و جدول را بازسازی می کند و همچنین چندین ثابت (constant) ویژه ی اسم جدول و ستون های آن تعریف می کند.

package de.vogella.android.sqlite.first;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
 
public static final String TABLE_COMMENTS = "comments";
 
public static final String COLUMN_ID = "_id";
 
public static final String COLUMN_COMMENT = "comment";
 
private static final String DATABASE_NAME = "commments.db";
 
private static final int DATABASE_VERSION = 1;
 
// Database creation sql statement
 
private static final String DATABASE_CREATE = "create table "
         
+ TABLE_COMMENTS + "(" + COLUMN_ID
          +
" integer primary key autoincrement
، " + COLUMN_COMMENT
          +
" text not null);";

 
public MySQLiteHelper(Context context) {
   
super(context، DATABASE_NAME، null، DATABASE_VERSION);
  }
  @Override
 
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }
  @Override
 
public void onUpgrade(SQLiteDatabase db، int oldVersion، int newVersion) {
    Log.w(MySQLiteHelper.
class.getName()،
           
"Upgrading database from version " + oldVersion + " to "
                   
+ newVersion + "، which will destroy all old data");
    db.execSQL(
"DROP TABLE IF EXISTS "
+ TABLE_COMMENTS);
    onCreate(db);
  }
}

کلاس comment را ایجاد کنید. این کلاس مدل ماست و حامل داده هایی است که در پایگاه داده ذخیره کرده و در رابط کاربری به نمایش می گذاریم.

package de.vogella.android.sqlite.first;
public class Comment {
 
private long id;
 
private String comment;
 
public long getId() {
   
return id;
  }
 
public void setId(long id) {
   
this.id = id;
  }
 
public String getComment() {
   
return comment;
  }
 
public void setComment(String comment) {
   
this.comment = comment;
  }
 
// Will be used by the ArrayAdapter in the ListView
 
@Override
 
public String toString() {
   
return
comment;
  }
}

حال کلاس commentDataSource را ایجاد کنید. کلاس مزبور DAO ما محسوب می شود و اتصال به پایگاه داده را حفظ کرده و از قابلیت افزودن Comment جدید و بازیابی تمامی comment ها پشتیبانی می کند.

package de.vogella.android.sqlite.first;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class CommentsDataSource {
 
// Database fields
 
private SQLiteDatabase database;
 
private MySQLiteHelper dbHelper;
 
private String[] allColumns = { MySQLiteHelper.COLUMN_ID
،
          MySQLiteHelper.COLUMN_COMMENT };
 
public CommentsDataSource(Context context) {
    dbHelper =
new MySQLiteHelper(context);
  }
 
public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }
 
public void close() {
    dbHelper.close();
  }
 
public Comment createComment(String comment) {
    ContentValues values =
new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT،
comment);
   
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS، null،
            values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS
،
            allColumns
، MySQLiteHelper.COLUMN_ID + " = " + insertId، null،
           
null، null، null);
    cursor.moveToFirst();
    Comment newComment = cursorToComment(cursor);
    cursor.close();
   
return newComment;
  }
 
public void deleteComment(Comment comment) {
   
long id = comment.getId();
    System.out.println(
"Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS،
MySQLiteHelper.COLUMN_ID
            +
" = " + id، null);
  }
 
public List<Comment> getAllComments() {
    List<Comment> comments =
new ArrayList<Comment>();
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS،

            allColumns
، null، null، null، null، null);
    cursor.moveToFirst();
   
while (!cursor.isAfterLast()) {
      Comment comment = cursorToComment(cursor);
      comments.add(comment);
      cursor.moveToNext();
    }
   
// make sure to close the cursor
   
cursor.close();
   
return comments;
  }
 
private Comment cursorToComment(Cursor cursor) {
    Comment comment =
new Comment();
    comment.setId(cursor.getLong(
0));
    comment.setComment(cursor.getString(
1));
   
return comment;
  }
}

 رابط کاربری

فایل طرح بندی main.xml را در پوشه ی res/layout به کد زیر تغییر دهید. layout نام برده دارای دو دکمه برای حذف و اضافه ی comment و یک ListView ویژه ی نمایش comment های جاری است. متن comment بعد توسط یک تولید کننده ی تصادفی (random generator) کوچک در activity مربوط ایجاد می شود.

 <?xml version="1.0" encoding="utf-8" ?>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="match_parent"

              android:layout_height="match_parent"

              android:orientation="vertical">

    <linearlayout android:id="@+id/group"

                  android:layout_width="wrap_content"

                  android:layout_height="wrap_content">

        <button android:id="@+id/add"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Add New"

                android:onclick="onClick" />

        <button android:id="@+id/delete"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="Delete First"

                android:onclick="onClick" />

    </linearlayout>

    <listview android:id="@android:id/list"

              android:layout_width="match_parent"

              android:layout_height="wrap_content"

              android:text="@string/hello" />

</linearlayout>

اکنون کلاس TestDatabaseActivity را اصلاح کنید. در اینجا برای نمایش داده از یک ListActivity استفاده می کنیم.

package de.vogella.android.sqlite.first;
import java.util.List;
import java.util.Random;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
public class TestDatabaseActivity extends ListActivity {
 
private CommentsDataSource datasource;
  @Override
 
public void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    datasource =
new CommentsDataSource(this);
    datasource.open();
    List<Comment> values = datasource.getAllComments();
   
// use the SimpleCursorAdapter to show the
    // elements in a ListView
   
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this
،
            android.R.layout.simple_list_item_1
، values);
    setListAdapter(adapter);
  }
 
// Will be called via the onClick attribute
  // of the buttons in main.xml
 
public void onClick(View view) {
    @SuppressWarnings(
"unchecked")
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
    Comment comment =
null;
   
switch (view.getId()) {
     
case R.id.add:
        String[] comments =
new String[] { "Cool"، "Very nice"، "Hate it" };
       
int nextInt = new Random().nextInt(3);
       
// save the new comment to the database
       
comment = datasource.createComment(comments[nextInt]);
        adapter.add(comment);
       
break;
     
case R.id.delete:
       
if (getListAdapter().getCount() > 0) {
          comment = (Comment) getListAdapter().getItem(
0);
          datasource.deleteComment(comment);
          adapter.remove(comment);
        }
       
break;
    }
    adapter.notifyDataSetChanged();
  }
  @Override
 
protected void onResume() {
    datasource.open();
   
super.onResume();
  }
  @Override
 
protected void onPause() {
    datasource.close();
   
super.onPause();
  }
}

1394/07/27 6021 3681
رمز عبور : tahlildadeh.com یا www.tahlildadeh.com
نظرات شما

نظرات خود را ثبت کنید...