Skip to content Skip to sidebar Skip to footer

How To Fetch Data From Sqlite And Store It To List Column And Convert To .pdf Automatically

I am very novice programmer in Android. My query is to fetch data from SQLite and then reflect all data in to list column . now when i click on button, that all data must be conver

Solution 1:

for your first purpose to get data from SQLite DB into arraylist, here is an example below:

publicclassDatabaseHelper_NotificationextendsSQLiteOpenHelper
{
publicStringTAG="DatabaseHelper:";
privatestaticfinalStringDATABASE_NAME="Notifications";
privatefinal Context cntxtDBContext;
finalstaticintDATABASE_VERSION=2;
finalString_TableName="Notifications";
finalString_RowID="RowID";
finalString_NotificationID="NotificationID";
finalString_NotificationText="NotificationText";
finalString_NotificationRead="NotificationRead";

/**
 * the default constructor of this class
 * 
 * @param context
 *             the context in which this object is to be used
 */publicDatabaseHelper_Notification(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.cntxtDBContext = context;
    // strDB_path = cntxtDBContext.getDatabasePath(strDB_NAME).toString();
}

@OverridepublicvoidonCreate(SQLiteDatabase db)
{
    StringCREATE_CONTACTS_TABLE="CREATE TABLE " + _TableName + "(" + _RowID + " INTEGER PRIMARY KEY," + _NotificationID
            + " INTEGER NOT NULL  DEFAULT (0)," + _NotificationText + " TEXT" + "," + _NotificationRead + "  BOOL NOT NULL DEFAULT (0))";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    if (newVersion > oldVersion)
    {
        db.execSQL("DROP TABLE IF EXISTS " + _TableName);

        // Create tables again
        onCreate(db);
    }

}

/**
 * gets all notification news from notifications database
 * 
 * @return the list of the notification present in the database
 */public ArrayList<NotificationCls> getAllNotifications()
{
    ArrayList<NotificationCls> alNotifications = newArrayList<NotificationCls>();
    ;
    CursorcurData=null;
    SQLiteDatabasedb=null;
    try
    {
        db = this.getReadableDatabase();
        StringstrSelectQuery="SELECT * FROM " + _TableName;
        curData = db.rawQuery(strSelectQuery, null);
        if (curData.getCount() > 0)
        {
            curData.moveToFirst();
            do
            {
                NotificationClsnoti=newNotificationCls();
                noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
                noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
                booleanbRead= curData.getInt(curData.getColumnIndex(_NotificationRead)) == 0 ? false : true;
                noti.setbNotificationRead(bRead);
                alNotifications.add(noti);
            }
            while (curData.moveToNext());
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (curData != null)
        {
            curData.close();
        }
        if (db != null)
        {
            db.close();
        }

    }
    return alNotifications;
}

/**
 * insert the notification message in the database
 * 
 * @param Msg
 *             the message received in push notification
 * @param id
 *             the id on which the notification was sent
 * @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
 */publicbooleaninsertNotification(String Msg, int id)
{
    longlid= -1;
    SQLiteDatabasedb=null;
    try
    {
        db = this.getWritableDatabase();
        ContentValuesvalues=newContentValues();
        values.put(_NotificationID, id);
        values.put(_NotificationText, Msg);
        db.insert(_TableName, null, values);
        db.close();
    }
    catch (SQLException e)
    {
        lid = -1;
        e.printStackTrace();
    }
    catch (Exception e)
    {
        lid = -1;
        e.printStackTrace();
    }
    finally
    {
        if (db != null)
            db.close();
    }
    returnlid== -1 ? false : true;
}

/**
 * this method will delete all the records associated with the specified notification id
 * 
 * @param iNotificationiID
 *             the id on which the notification was sent
 * @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
 */publicbooleandeleteNotification(int iNotificationiID)
{
    intresult=0;
    SQLiteDatabasedb=this.getWritableDatabase();
    result = db.delete(_TableName, _NotificationID + " = " + iNotificationiID, newString[] {});
    db.close();
    if (result > 0)
    {
        returntrue;
    }
    else
    {
        returnfalse;
    }
}

/**
 * this method will delete all the records from the database
 * 
 * @return this method will return <code>true</code> if the DB transaction was successful else it will return <code>false</code>
 */publicbooleandeleteAllNotifications()
{
    intresult=0;
    SQLiteDatabasedb=this.getWritableDatabase();
    result = db.delete(_TableName, null, newString[] {});
    db.close();
    if (result > 0)
    {
        returntrue;
    }
    else
    {
        returnfalse;
    }
}

/**
 * this method will return the number of records present in the database
 * 
 * @return the number of records present in the database
 */publicintgetNotificationsCount()
{
    StringcountQuery="SELECT  * FROM " + _TableName;
    SQLiteDatabasedb=this.getReadableDatabase();
    Cursorcursor= db.rawQuery(countQuery, null);
    cursor.close();
    // return countreturn cursor.getCount();
}

publicbooleanmarkNotificationAsRead(int iNotificationiID)
{
    // TODO Auto-generated method stubintresult= -1;
    SQLiteDatabasedb=this.getWritableDatabase();

    ContentValuesvalues=newContentValues();
    values.put(_NotificationRead, 1);

    // updating row
    result = db.update(_TableName, values, _NotificationID + " = ?", newString[] { String.valueOf(iNotificationiID) });
    db.close();
    if (result > 0)
    {
        returntrue;
    }
    else
    {
        returnfalse;
    }
}

publicbooleanmarkAllNotificationAsRead()
{
    // TODO Auto-generated method stubintresult= -1;
    SQLiteDatabasedb=this.getWritableDatabase();

    ContentValuesvalues=newContentValues();
    values.put(_NotificationRead, 1);

    // updating row
    result = db.update(_TableName, values, null, newString[] {});
    db.close();
    if (result > 0)
    {
        returntrue;
    }
    else
    {
        returnfalse;
    }
}

/**
 * gets all notification news from notifications database
 * 
 * @return the list of the notification present in the database
 */public ArrayList<NotificationCls> getAllUnreadNotifications()
{
    ArrayList<NotificationCls> alNotifications = newArrayList<NotificationCls>();
    ;
    CursorcurData=null;
    SQLiteDatabasedb=null;
    try
    {
        db = this.getReadableDatabase();
        StringstrSelectQuery="SELECT * FROM " + _TableName + " WHERE " + _NotificationRead + "=0";
        curData = db.rawQuery(strSelectQuery, null);
        if (curData.getCount() > 0)
        {
            curData.moveToFirst();
            do
            {
                NotificationClsnoti=newNotificationCls();
                noti.setStrNotificationText(curData.getString(curData.getColumnIndex(_NotificationText)));
                noti.setiNotificationiID(curData.getInt(curData.getColumnIndex(_NotificationID)));
                alNotifications.add(noti);
            }
            while (curData.moveToNext());
        }
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (curData != null)
        {
            curData.close();
        }
        if (db != null)
        {
            db.close();
        }

    }
    return alNotifications;
}

and this is the entity class:

publicclassNotificationCls
{
String strNotificationText;
int iNotificationiID;
boolean bNotificationRead = false;

publicStringgetStrNotificationText()
{
    return strNotificationText;
}

publicvoidsetStrNotificationText(String strNotificationText)
{
    this.strNotificationText = strNotificationText;
}

public int getiNotificationiID()
{
    return iNotificationiID;
}

publicvoidsetiNotificationiID(int iNotificationiID)
{
    this.iNotificationiID = iNotificationiID;
}

publicbooleanisbNotificationRead()
{
    return bNotificationRead;
}

publicvoidsetbNotificationRead(boolean bNotificationRead)
{
    this.bNotificationRead = bNotificationRead;
}

}

to get all data from SQLite in arraylist write below code in your activity:

DatabaseHelper_NotificationdatabaseHelper_notification=newDatabaseHelper_Notification(this);
ArrayList<NotificationCls> alNotifications = databaseHelper_notification.getAllNotifications();

I hope this helps you...

Post a Comment for "How To Fetch Data From Sqlite And Store It To List Column And Convert To .pdf Automatically"