vendredi 8 janvier 2016

How to retrieve integer data from SQLite and show it in textview?

I'm new in SQLite and I have this code that shows the points in textview which retrieves the data from the database and if I click the button, it should retrieve the data, add some integers and append it to the database. However whenever I run the app, the app crashes.

Here's my mainActivity

import android.widget.TextView;

public class MainActivity extends Activity {

  DatabaseHelper mydb;
  private static TextView txt_stars;

  @
  Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mydb = new DatabaseHelper(this);
    onButtonClickListener();
    txt_stars = (TextView) findViewById(R.id.txtStars);
    //I already clean my code here
  }

  public void onButtonClickListener {

    button_next = (Button) findViewById(R.id.btnNext);
    button_next.setOnClickListener(
      new View.OnClickListener() {
      @Override
        public void onClick(View v) {
          AlertDialog.Builder alert = new AlertDialog.Builder(context);
          alert.setMessage("You have earned 50 stars");
          alert.setCancelable(false);
          alert.setPositiveButton("next", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              //retrieve data, add 50, append data and return to main with a textview of 50 stars. How do I do this?
            }
          });
          AlertDialog alertDialog = alert.create();
          alertDialog.show();
        }
      }
    );

  }


}

Here's my DatabaseHelper.java

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 Cursor getAllData() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select stars from" + TABLE_NAME, null);
    return res;
  }

}

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire