vendredi 8 janvier 2016

Fetching data from sqlite and display as listview

I am trying to fetch data from sq lite database and display as list. I tried different solutions given previously but no luck.

When I try to display the list, I continuously get GC-CONCURRENT FREED. After sometime , I get out of memory error and the app stops responding.

DataBaseHelper.class

public class DataBaseHandler extends SQLiteOpenHelper {

private static final int Database_Version = 1;
private static final String Database_Name = "remindme";
static final String Table_RemindMe = "reminders";
 static final String Key_Id = "id";
static final String Key_Title = "title";
static final String Key_Description = "description";
 static final String Key_Date = "date";
List<RemindMe> reminderList = new ArrayList<RemindMe>();


public DataBaseHandler(Context context) {
    super(context, Database_Name, null, Database_Version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CREATE_REMINDME_TABLE = "CREATE TABLE " + Table_RemindMe + "("
            + Key_Id + " INTEGER PRIMARY KEY AUTOINCREMENT," + Key_Title
            + " TEXT, " + Key_Date + " TEXT," + Key_Description + " TEXT" + ")";
    db.execSQL(CREATE_REMINDME_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
   db.execSQL("DROP TABLE IF EXIXTS" + Table_RemindMe);
     onCreate(db);
}

void addRemindMe(RemindMe remindme){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Key_Title, remindme.getTitle());
    values.put(Key_Description, remindme.getDescription());
    values.put(Key_Date, remindme.getDate());
    db.insert(Table_RemindMe, null, values);
    db.close();

}

public List<RemindMe> getReminder(){
    List<RemindMe> reminderList = new ArrayList<RemindMe>();
    String selectQuery = "Select * FROM " + Table_RemindMe ;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    try{

    if(cursor.moveToFirst()){
        do{
            RemindMe reminder = new RemindMe();
            reminder.setId(Integer.parseInt(cursor.getString(0)));
            reminder.setTitle(cursor.getString(1));
            reminder.setDescription(cursor.getString(2));
            reminder.setDate(cursor.getString(3));
            reminderList.add(reminder);
        }while(cursor.moveToLast());
    }

    }
    finally{
        cursor.close();
    }
    db.close();
    return reminderList;
}

List Class

 DataBaseHandler db=new DataBaseHandler();
Log.d("Reading", "Reading Reminders");
 List<RemindMe> reminders= db.getReminder();

 for(RemindMe r:reminders){
 HashMap<String, String> map = new HashMap<String, String>();
  map.put("Title", r.title);
  map.put("Desc", r.description);

  Items.add(map);
}


ListAdapter adapter=new SimpleAdapter(this,Items,android.R.layout.simple_expandable_list_item_1,new String[]{"Iitle" , "Desc"},null);

    lv.setAdapter(adapter);

Please help me to solve this issue.

Aucun commentaire:

Enregistrer un commentaire