vendredi 29 mai 2015

Get Data from Database and change Image

I have an Andorid App with Google Maps and Markers. If you click at one marker you get to an "Home Screen" of the Location where you can start a quiz with a Button. After going through 5 Multiple Choice Questions the Score is shown (1 - 5 Points). To this Point everthing was working. Now I want to save the Score in a SQLite Database and change an image at the "Home Screen" if the Score is 5. Now everytime I test and click on a Marker to open the "Home Screen"(where the image should be changed if Score is = 5) of a specific location the App crashes. Until now there is no data in the table because I never ran through a quiz to save a score. I would be very thankful if someone finds a mistake!

Here is my Code:

Database Helper:

public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 7;
private static final String DATABASE_NAME = "CE";

public static final String SCORE_TABLE = "score";
public static final String COLUMN_ID = "ID";
public static final String COLUMN_SCORE = "SCORE";
public static final String COLUMN_MARKERID = "MARKERID";

 private SQLiteDatabase dbase;

 public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}
@Override
public void onCreate(SQLiteDatabase db) {
    //Hier alle Tables erstellen

    String create_query = "CREATE TABLE IF NOT EXITS " + SCORE_TABLE + " ( "
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_SCORE + " INTEGER, "
            + COLUMN_MARKERID + " TEXT) ";
    db.execSQL(create_query);
}

public void addScore (DbHelper dbh, Integer score, String markerID) {
    dbase = dbh.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_SCORE, score);
    cv.put(COLUMN_MARKERID, markerID);
    dbase.insert(SCORE_TABLE, null, cv);
}

public Cursor getScore(DbHelper dbh) {
    dbase = dbh.getReadableDatabase();
    String columns[] = {COLUMN_SCORE, COLUMN_MARKERID};
    Cursor cursor = dbase.query(SCORE_TABLE, columns, null, null, null, null, null);
    return cursor;
}

Write the Score into the Database after completing the Quiz in ResultActivity:

public class ResultActivity extends Activity {

String markerID;
int score;
TextView t=(TextView)findViewById(R.id.textResult);
Button saveButton = (Button) findViewById(R.id.saveButton);
Context context = this;

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

    Bundle b = getIntent().getExtras();
    score = b.getInt("score");
    markerID = b.getString("markerID");
}

saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DbHelper dbh = new DbHelper(context);
            dbh.addScore(dbh,score,markerID);
        }
    });
}

Look at the "Home Screen" what Score is saved on this marker:

public class Discover extends ActionBarActivity {

TextView InfoHeadline;
ImageView ImageDone;
Button QuizButton;

String markerID;

Context context;

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

    InfoHeadline = (TextView)findViewById(R.id.InfoUeberschrift);
    ImageDone =(ImageView)findViewById(R.id.imageDone);
    QuizButton = (Button)findViewById(R.id.QuizButton);

//get markerID from previous google maps activity
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            markerID= null;
        } else {
            markerID= extras.getString("MarkerID");
        }
    } else {
        markerID = (String) savedInstanceState.getSerializable("MarkerID");
    }

    InfoHeadline.setText(markerID);

    DbHelper dbh  = new DbHelper(context);
    Cursor cursor = dbh.getScore(dbh);
    cursor.moveToFirst();
    do {
        if (Integer.parseInt(cursor.getString(0))== 5 && InfoHeadline.toString().equals(cursor.getString(1))){
            ImageDone.setImageResource(R.drawable.markerdone);
        }
    }while(cursor.moveToNext());
}

public void StartQuiz (View v) {
    Intent intent = new Intent(Discover.this,QuizActivity.class);
    intent.putExtra("MarkerID", markerID);
    startActivity(intent);
}
}

Aucun commentaire:

Enregistrer un commentaire