- Android- Populating a ListView with a CursorAdapter with SQLite

elektro

Well-known member
  • Apr 18, 2011
    6,856
    9,352
    113
    Tropical Island Of Sri Lanka
    වර ඇතුලට












    ඇන්ඩ්‍රොයිඩ් ඇප් ඩෙව් කරන අයගෙන් ලොකු හෙල්ප් එකක් ඕන..මම තාම අලුත් Android dev වලට. මට ඕන මචංලා list view එකකට කලින් හදලා තියෙන db (data දැනටමත් දාලා තියෙන) එකකින් data ගන්න cursoradapter එකක් යූස් කරලා.

    මම මේ ටියුටෝරියල් එක බැලුවා. https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter

    මේකේ Retrieving the Cursor කියන තැනයි ගැටලුව තියෙන්නේ. දැනට මම DB එක Access කරන්න class එකක් හදලා තියෙන්නේ.

    public class DBAccess {
    private SQLiteOpenHelper openHelper;
    private SQLiteDatabase database;
    private static DBAccess instance;

    public DBAccess(Context context) {

    this.openHelper = new MyDBClass(context);
    }

    public static DBAccess getInstance(Context context) {
    if (instance == null) {
    instance = new DBAccess(context);
    }
    return instance;
    }

    public void open() {
    this.database = openHelper.getWritableDatabase();
    }

    public void close() {
    if (database != null) {
    this.database.close();
    }
    }





    }

    මේ class එක ඇතුලට කොහොමද අපි (list view එක ගන්න ඕන query ඇතුලත්) retrieve cursor එක ලියන්නේ.

    අනික් අය බම්ප් එකක් දාගෙන යන්න.

     

    dushan135

    Active member
  • Oct 19, 2010
    524
    94
    28
    SQLiteOpenHelper class eka athule method ekak liyapan data ganna

    public Cursor getData(SQLiteHandler sqllite) {
    SQLiteDatabase sqDB = sqllite.getReadableDatabase();
    String[] columns = {column_name_1, column_name_2,column_name_3};

    Cursor cr = sqDB.query(TABLE_NAME, columns, null, null, null, null, null);

    return cr;
    }


    in passe uba oya class eken method eka access karapan me widiyata

    final SQLiteHandler sqLiteHandler = new SQLiteHandler(getApplicationContext());
    Cursor cr = sqLiteHandler.getData(sqLiteHandler);
    cr.moveToFirst();

    do {
    String name1 = cr.getString(0);
    String name2 = cr.getString(1);
    String name3 = cr.getString(2);
    } while (cr.moveToNext());
    cr.close();
     
    • Like
    Reactions: elektro

    elektro

    Well-known member
  • Apr 18, 2011
    6,856
    9,352
    113
    Tropical Island Of Sri Lanka
    SQLiteOpenHelper class eka athule method ekak liyapan data ganna

    public Cursor getData(SQLiteHandler sqllite) {
    SQLiteDatabase sqDB = sqllite.getReadableDatabase();
    String[] columns = {column_name_1, column_name_2,column_name_3};

    Cursor cr = sqDB.query(TABLE_NAME, columns, null, null, null, null, null);

    return cr;
    }


    in passe uba oya class eken method eka access karapan me widiyata

    final SQLiteHandler sqLiteHandler = new SQLiteHandler(getApplicationContext());
    Cursor cr = sqLiteHandler.getData(sqLiteHandler);
    cr.moveToFirst();

    do {
    String name1 = cr.getString(0);
    String name2 = cr.getString(1);
    String name3 = cr.getString(2);
    } while (cr.moveToNext());
    cr.close();



    thanx machoo mama balannam :D