mardi 26 janvier 2016

How to write/overwrite and read data integer in SQLite and show in TextView?

I have this app that when the MainActivity is called, the a textview will show the current data information or rather the score, if the COL1 (stars) is null it should write 0 in the column stars. Then when played and reached it's point when the score should change, like adding score, it should overwrite the data in the column.

Here's my code for my DatabaseHelper

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "star.db";
public static final String TABLE_NAME = "stars_table";
public static final String COL1 = "stars";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + "("+COL1+")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(Integer stars){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL1, stars);
    long result =  db.insert(TABLE_NAME, null, contentValues);
    if(result == -1)
        return false;
    else
        return true;
}

public ArrayList<Model> getAllData() {

    cartList.clear();

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("select * from stars_table", null); 
    if (cursor.getCount() != 0) {
        if (cursor.moveToFirst()) {
            do {
                Model item = new Model();
                item.status =  cursor.getString(cursor.getColumnIndex("stars")); 


                cartList.add(item);

            } while (cursor.moveToNext());
        }
    }
    cursor.close();
    db.close();
    return cartList;

    }

}

So how can I show it in textview and also how to read the data, compute, then overwrite it with new data?

txt_stars = (TextView) findViewById(R.id.txtStars);

I've been stuck here for weeks, can somebody help me?

Thanks a lot.

Aucun commentaire:

Enregistrer un commentaire