How to create a findAll() in android ? I've tried this but I can not achieve it ;

public List findAll() { Cursor c = mDb.rawQuery( "select * from " + TABLE_NAME, "", "" ); }

Here is Medicament.java

public class Medicament {
    private long   id;
    private String nom;
    private String fabriquant;
    private String pharmacie;
    private String description;
    private float prix;

    public Medicament( long id, String nom, String fabriquant, String pharmacie, String description, float prix ) {
        super();
        this.id = id;
        this.nom = nom;
        this.fabriquant = fabriquant;
        this.pharmacie = pharmacie;
        this.description = description;
        this.prix = prix;
    }

    public long getId() {
        return id;
    }

    public void setId( long id ) {
        this.id = id;
    }

    public String getNom() {
        return nom;
    }

    public void setNom( String nom ) {
        this.nom = nom;
    }

    public String getFabriquant() {
        return fabriquant;
    }

    public void setFabriquant( String fabriquant ) {
        this.fabriquant = fabriquant;
    }

    public String getPharmacie() {
        return pharmacie;
    }

    public void setPharmacie( String pharmacie ) {
        this.pharmacie = pharmacie;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription( String description ) {
        this.description = description;
    }

    public float getPrix() {
        return prix;
    }

    public void setPrix( float prix ) {
        this.prix = prix;
    } }

EDIT: Please do not forget the return statement.

Thanks in advance.

有帮助吗?

解决方案

Try somethiing like that in Your SQLiteOpenHelper:

public List<Medicament> findAll() {
    List<Medicament> medicaments = new ArrayList<Medicament>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery( "SELECT * FROM " + TABLE_NAME,null);
    if (cursor.moveToFirst()) {
        do {
            Medicament medicament = new Medicament();

            /*fill here medicament object with values from row, 
            cursor.getString(0) = id value, cursor.getString(1) etc...
            for example medicament.setName(cursor.getString(1))*/

            medicaments.add(medicament);

        }
        while (cursor.moveToNext());
    }
    return medicaments;
}

EDIT :

Remember to use methods properly for value type in a cell, for example for Integer values use cursor.getInt(COLUMN_NUMBER), for String cursor.getString(COLUMN_NUMBER) etc.

其他提示

Do this way

Cursor c = mDb.rawQuery( "select * from " + TABLE_NAME,null);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top