1

inHow do i retrieve data from sqlite database from adapter class and insert to listview???

I'm having difficulty, been coding day & night and googling non-stop but i just don't understand the links when i open them up. I just start learning android programming recently on my own...

I have attach my code below

public Cursor RetrieveActivityCursor(String NRIC){
    String where = NRIC;

Cursor cursor = _db.query(DATABASE_TABLE,new String[]{ MOBILE_HOUSE_VISIT_ID, ELDERLY_NAME,
        ELDERLY_NRIC, ELDERLY_DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS} , where, null, null, null, null);
//Cursor cursor = _db.query(DATABASE_TABLE, new String[] { ELDERLY_NAME,
        //ELDERLY_NRIC, DATE_TIME, PHYSICAL_HEALTH_STATUS, MENTAL_HEALTH_STATUS }, null, null, null, null, null);
return cursor;
}

this code is from my adapter class and have to pass in nric and return datetime value inserted into listview

im not too sure how to code to call this method.

3 Answers 3

1

If you are extending the SqliteOpenHelper for the database handling and using another class to which extends Activity, in which you have your ListView then, in your Activity class make the object of your class like,

 YourDBClass helperDB;
 helperDB = new HelperDB(YourActivityClass.this);

And to retrive the data from the Database.

Make Cursor reference like,

 Cursor cursor;

And then, do like this,

 cursor = helperDB.RetrieveActivityCursor(NRIC);
 cursor.moveToFirst();
 while(!cursor.isAfterLast()) {
    // here you have to collect the data in the collection object.
    cursor.moveToNext();
 }
 cursor.close();

That's it! And you have done with retriving data from database

Sign up to request clarification or add additional context in comments.

Comments

0

you need iterate cursor, like this:

Cursor cursor = RetrieveActivityCursor(NRIC);
if (cursor != null && cursor.moveToNext())
do {
    String str = cursor.getString(cursor.getColumnIndex("your_column"));

   // do something, your code

} while (cursor.moveToNext());

Comments

0

Where you like to retrieve the date value do the below over there.

 Cursor cursor = RetrieveActivityCursor(NRIC); 
 if(mCursor.moveToFirst()) {            
     do{ 
         String date_time = cursor.getString(cursor.getColumnIndex(column_name));
     } while(mCursor.moveToNext());
 }
 cursor.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.