自制日历手机数据库(实现SQLiteOpenHelper)

自制日历手机数据库(实现SQLiteOpenHelper)

新建一个继承Activity类的SQLiteOpenHelperActivity,并设置布局文件为:sqliteopenhelper.xml。

首先定义布局文件:

<?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:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/shop_name"

            android:textSize="22sp" />

 

        <EditText

            android:id="@+id/sqliteopenhelper_shopname"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1" />

 

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/price"

            android:textSize="22sp" />

 

        <EditText

            android:id="@+id/sqliteopenhelper_price"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:inputType="numberDecimal" />

    </LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <Button

            android:id="@+id/sqliteopenhelper_add"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/add01"

            android:textSize="22sp" />

 

        <Button

            android:id="@+id/sqliteopenhelper_delete"

            style="@android:style/Widget.Button.Inset"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="@string/delete"

            android:textSize="22sp" />

    </LinearLayout>

 

    <TextView

        android:id="@+id/sqliteopenhelper_text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/show_information"

        android:textSize="22sp" />

 

    <ListView

        android:id="@+id/sqliteopenhelper_listview"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="22sp" />

 

</LinearLayout>

接着是创建一个SQLiteOpenHelper的帮助类:MySQliteOpenHelper类。

package lyx.feng.third;

 

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

 

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    private final static String DATA_BASENAME = "lyx.db";

    private final static int DATA_VERSION = 2;

    private String sql = "create table shop (_id integer primary key autoincrement,name varchar(50) not null,price double);";

 

    public MySQLiteOpenHelper(Context context) {

       super(context, DATA_BASENAME, null, DATA_VERSION);

    }

 

    @Override

    public void onCreate(SQLiteDatabase db) {

       db.execSQL(sql);

    }

 

    @Override

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

       db.execSQL("drop table if exists shop");

       onCreate(db);

    }

 

    // 查询操作

    public Cursor select() {

       SQLiteDatabase db = this.getReadableDatabase();

       Cursor cursor = db.query("shop", null, null, null, null, null, null);

       // db.close();

       return cursor;

    }

 

    public Cursor selectbyId(int id) {

       SQLiteDatabase db = this.getReadableDatabase();

       String selection = "_id = ?";

       String whereArgs[] = { Integer.toString(id) };

       Cursor cursor = db.query("shop",

              new String[] { "_id", "name", "price" }, selection, whereArgs,

              null, null, null);

       // db.close();

       return cursor;

    }

 

    // 插入操作

    public long insert(String name, float price) {

       SQLiteDatabase db = this.getWritableDatabase();

       ContentValues values = new ContentValues();

       values.put("name", name);

       values.put("price", price);

       long temp = db.insert("shop", null, values);

       db.close();

       return temp;

    }

 

    // 更新操作

    public void update(int _id, String name, String price) {

       SQLiteDatabase db = this.getWritableDatabase();

       ContentValues values = new ContentValues();

       values.put("name", name);

       values.put("price", price);

       String whereClause = "id = ?";

       String whereArgs[] = { Integer.toString(_id) };

       db.update("shop", values, whereClause, whereArgs);

       db.close();

    }

 

    // 删除操作

    public void delete(int id) {

       SQLiteDatabase db = this.getWritableDatabase();

       String whereClause = "_id = ?";

       String whereArgs[] = { Integer.toString(id) };

       db.delete("shop", whereClause, whereArgs);

       db.close();

    }

}

 

最后是Activity类:

package lyx.feng.third;

 

import lyx.feng.simpletextdemo.R;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.database.Cursor;

import android.os.Bundle;

import android.widget.SimpleCursorAdapter;

import android.text.TextUtils;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

 

public class SQLiteOpenHelperActivity extends Activity implements

       OnClickListener {

    private EditText shopname = null;

    private EditText price = null;

    private TextView info = null;

    private ListView listView = null;

    private Button add = null;

    private Button delete = null;

    private MySQLiteOpenHelper helper = null;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.sqliteopenhelper);

       this.shopname = (EditText) super

              .findViewById(R.id.sqliteopenhelper_shopname);

       this.price = (EditText) super.findViewById(R.id.sqliteopenhelper_price);

       this.info = (TextView) super.findViewById(R.id.sqliteopenhelper_text);

       this.add = (Button) super.findViewById(R.id.sqliteopenhelper_add);

       this.delete = (Button) super.findViewById(R.id.sqliteopenhelper_delete);

       this.listView = (ListView) super

              .findViewById(R.id.sqliteopenhelper_listview);

       this.helper = new MySQLiteOpenHelper(this);

       this.add.setOnClickListener(this);

       this.delete.setOnClickListener(this);

       this.showListView();

    }

 

    @Override

    public void onClick(View v) {

       switch (v.getId()) {

       case R.id.sqliteopenhelper_add:

           String _name = this.shopname.getText().toString();

           String _price = this.price.getText().toString();

           if (TextUtils.isEmpty(_name) || TextUtils.isEmpty(_price)) {

              Toast.makeText(this, "Not Null", Toast.LENGTH_SHORT).show();

              return;

           }

           this.helper.insert(_name, Float.parseFloat(_price));

           Toast.makeText(this, "Insert Success", Toast.LENGTH_SHORT).show();

           this.showListView();

           break;

       case R.id.sqliteopenhelper_delete:

 

           final int id = 4;

           Cursor cursor = this.helper.selectbyId(id);

           // Cursor移动到第一个

           cursor.moveToFirst();

           System.out.println("----------------" + cursor.getCount());

 

           String temp = "商品:" + cursor.getString(1) + ",价格:"

                  + cursor.getFloat(2);

           System.out.println(temp);

           new AlertDialog.Builder(this)

                  .setTitle("提示")

                  .setMessage(temp)

                  .setPositiveButton("确定",

                         new DialogInterface.OnClickListener() {

 

                            @Override

                            public void onClick(DialogInterface dialog,

                                   int which) {

                                helper.delete(id);

                            }

                         }).setNegativeButton("取消", null).create().show();

           break;

       default:

           break;

       }

    }

 

    public void showListView() {

       Cursor cursor = this.helper.select();

       @SuppressWarnings("deprecation")

       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,

              R.layout.sqliteopenhelper_list, cursor, new String[] { "_id",

                     "name", "price" }, new int[] {

                     R.id.sqliteopenhelper_list_id,

                     R.id.sqliteopenhelper_list_shopname,

                     R.id.sqliteopenhelper_list_price });

       this.listView.setAdapter(adapter);

       this.setInfo();

    }

 

    public void setInfo() {

       Cursor cursor = this.helper.select();

       double temp = 0;

       for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {

           temp = temp + cursor.getFloat(2);

       }

       this.info.setTextSize(18);

       this.info.setText("共计:" + cursor.getCount() + "件商品,总价为:" + temp + "");

    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值