0

我是android application开发的初学者。我现在正在处理一个项目reminder application。我在db Helper class这里附上我的。我的问题是,如何datadatabase插入或更新另一个activity命名ResultModel.classdatabase. 谁能帮我?

package com.example.reminderapp;

import java.util.ArrayList;

import android.app.PendingIntent;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;



public class   DatabaseHelper extends SQLiteOpenHelper {


    private static final String DATABASE_NAME    = "reminder_data";
    private static final String DATABASE_TABLE   = "reminder_details";
    private static final String DATABASE_VERSION = "1";

    public static final String REM_TITLE         = "title";
    public static final String REM_BODY          = "details";
    public static final String REM_DATE          = "date";
    public static final String REM_TIME          = "time";
    public static final String REM_MAIL          = "mail";
    public static final String REM_MSG           = "msg";
    public static final String REM_ALARM_STATUS  = "alarm_status";
    public static final String REM_MAIL_STATUS   = "mail_status";
    public static final String REM_MSG_STATUS    = "msg_status";
    public static  final String REM_ROWID        = "id";
    private SQLiteDatabase remDatabase;
    private DatabaseHelper dbHelper;
    PendingIntent i;
    Intent intent;
    int requestCode;
    private Context remContext;

    public DatabaseHelper(Context context) 
        {
         super(context, DATABASE_NAME , null, 1);
         remContext = context;
        }


    private static final String DATABASE_CREATE =
                        "create table " + DATABASE_TABLE +" (" 
                        + REM_ROWID +   " integer primary REM autoincrement, "   + REM_TITLE +
                        " text not null, "                       + REM_BODY + 
                        " text not null, "                       + REM_DATE + 
                        " text not null, "                       + REM_TIME + 
                        " text not null"                         + REM_MAIL +
                        " text not null"                         + REM_MSG  +
                        " text not null);"; 




    @Override
    public void onCreate(SQLiteDatabase db) 
        {

            db.execSQL(DATABASE_CREATE);
        }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }

//    
//    public DatabaseHelper open() throws SQLException 
//      {
//          dbHelper = new DatabaseHelper(remContext);
//          remDatabase = dbHelper.getWritableDatabase();
//          return this;
//      }
    public Cursor getData(int id)
        {
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null );
            return res;
         }

    public void close() 
        {
            dbHelper.close();
        }


    public long insertReminder(String title, String body, String reminderDate, String reminderTime, 
                               Boolean alarmStatus, Boolean mailStatus, Boolean smsStatus, 
                               String mailId, String mobileNo) 
            {
                SQLiteDatabase db = this.getWritableDatabase();
                ContentValues initialValues = new ContentValues();
                initialValues.put(REM_TITLE, title);
                initialValues.put(REM_BODY, body);
                initialValues.put(REM_DATE, reminderDate); 
                initialValues.put(REM_TIME, reminderTime);
                initialValues.put(REM_ALARM_STATUS, alarmStatus);
                initialValues.put(REM_MAIL_STATUS, mailStatus);
                initialValues.put(REM_MSG_STATUS, smsStatus);
                initialValues.put(REM_MAIL, mailId);
                initialValues.put(REM_MSG,  mobileNo);
//              intent.putExtra("requestCode", requestCode);   
                Toast.makeText(remContext, "reminder created", Toast.LENGTH_SHORT).show();
                return remDatabase.insert(DATABASE_TABLE, null, initialValues); 


            }

    public Integer deleteReminder(Integer reminderId) 
        {
        i = PendingIntent.getActivity(remContext, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Toast.makeText(remContext, "reminder deleted", Toast.LENGTH_SHORT).show();
        Log.d("database", "deleted");
        return remDatabase.delete( DATABASE_TABLE,"id = ? ", new String[]  { Integer.toString(reminderId) });

        }

    public boolean updateReminder (long id, String title, String body, String reminderDate, String reminderTime, 
                                   Boolean alarmStatus, Boolean mailStatus, Boolean smsStatus, String mailId, 
                                   String mobileNo, int requestCode)
        {
           SQLiteDatabase db = this.getWritableDatabase();
           ContentValues contentValues = new ContentValues();
           contentValues.put(REM_TITLE, title);
           contentValues.put(REM_BODY, body);
           contentValues.put(REM_DATE, reminderDate);
           contentValues.put(REM_TIME, reminderTime);
           contentValues.put(REM_ALARM_STATUS, alarmStatus);
           contentValues.put(REM_MAIL_STATUS, mailStatus);
           contentValues.put(REM_MSG_STATUS, smsStatus);
           contentValues.put(REM_MAIL, mailId);
           contentValues.put(REM_MSG, mobileNo);

//         i = PendingIntent.getBroadcast(remContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
           db.update("reminder_details", contentValues, "id = ? ", new String[] { Long.toString(id) } );
           Toast.makeText(remContext, "reminder updated", Toast.LENGTH_SHORT).show();

           return true;
        }



}
4

1 回答 1

0

我想您使用加载器来加载数据。

如果是这样,这个加载器有一个 ID。所以在你的编辑Activity/Fragment中,你可以从LoaderManager中得到这个Loader,编辑或删除就完成了,你调用loaders:onContentChanged()方法。

这将强制加载程序重新加载数据,而不是显示“旧”数据

你可以在这里阅读更多关于加载器的信息:http: //developer.android.com/guide/components/loaders.html

于 2015-06-18T06:36:54.367 回答