我有這個應用程序,當MainActivity被調用時,一個文本視圖將顯示當前的數據信息或更確切地說,如果COL1(星號)爲空,它應該在列星中寫入0。然後,當播放和到達時,分數應該改變的時候,比如添加分數,它應該覆蓋列中的數據。如何在SQLite中寫入/覆蓋和讀取數據整數並在TextView中顯示?
這裏是我的代碼爲我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 Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select stars from" + TABLE_NAME, null);
return res;
}
}
所以,我怎麼能顯示它的TextView以及如何讀取數據,計算,然後用新的數據覆蓋它嗎?
至於我的MainActivity
public class MainActivity extends Activity {
private static TextView txt_stars;
DatabaseHelper mydb;
@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onButtonClickListener();
mydb = new DatabaseHelper(this);
txt_stars = (TextView) findViewById(R.id.txtStars);
//this where the score will show
}
public void onButtonClickListener() {
//some buttons
//game start
}
}
至於如果答案是正確的
public class OneFifteenActivity extends Activity {
private static Button button_next;
private static TextView txt_ans;
final Context context = this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one_fifteen);
onButtonClickListener();
}
public void onButtonClickListener() {
txt_ans = (EditText) findViewById(R.id.edittxtAns);
button_next = (Button) findViewById(R.id.btnNext);
button_next.setOnClickListener(
new View.OnClickListener() {@
Override
public void onClick(View v) {
if (txt_ans.getText().toString().equals("dai")) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage("Message here");
alert.setCancelable(false);
alert.setPositiveButton("next", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent nextForm = new Intent(".MainActivity");
startActivity(nextForm);
//computation here
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage("Message here");
alert.setCancelable(false);
alert.setPositiveButton("next", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Congratulations!");
alert.setMessage("You have earned 50 stars");
alert.setCancelable(false);
alert.setPositiveButton("next", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent nextForm = new Intent(".MainActivity");
startActivity(nextForm);
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
}
}
}
);
}
}
我已經困在這裏了幾個星期,有人可以幫我將計算活動?
非常感謝。
你能把你的完整活動代碼放在這裏嗎? – thedarkpassenger
已更新它。 – kcNeko