How to put the result of database class into another activity

478 views Asked by At

I am a beginner in android application development. I am now handling a project of reminder application. I am attaching my db Helper class here. My question is, how can i pass the data in database when inserted or updated in another activity named ResultModel.class, from I have to fetch the details in the particular database. can anyone help me?

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;
        }



}
1

There are 1 answers

0
fmt.Println.MKO On

I suppose you use a loader to load your data.

if so, this loader has an ID. so in your edit Activity/Fragment, you can get this Loader from the LoaderManager, and it an edit or delete is done, you call the loaders: onContentChanged() method.

this will force the loader to reload the data, instead of displaying the "old" one

you can read more about loaders here: http://developer.android.com/guide/components/loaders.html