gpt4 book ai didi

java - 时间未保存在数据库中

转载 作者:行者123 更新时间:2023-12-01 14:47:17 24 4
gpt4 key购买 nike

好的,这是我的场景。我的游戏结束后,会向用户提供他完成游戏的时间,之后我的dialog.theme Activity 就会弹出以供用户输入用户名。该名称存储在 sqlite 数据库中。现在我的问题是……时机不对。时间存储在 double 类型的变量中。当我将该值发送到我的 RezultatVreme.class 时,它工作正常(然后我在弹出 Activity 中使用它,在其中向用户呈现时间......它发生在我的输入名称弹出窗口之前)。现在,我使用相同的值并将其发送到我的 ImePopup.class,并在 use 输入他的名字时使用该类来传递时间值并将其输入到我的数据库中。但这不起作用。我的名字输入得很好,但时间总是0。我尝试使用 double 、整数和长整型,但结果相同。这是我的游戏类,其中的一部分,我在其中展示弹出窗口并将值传递给其他类:

long tEnd = System.currentTimeMillis();
long tDelta = tEnd - tStart;
elapsedSeconds = tDelta / 1000.0;

Intent i = new Intent(getApplicationContext(), RezultatVreme.class);
i.putExtra("novoVreme", elapsedSeconds);
startActivity(i);
Intent ip = new Intent(getApplicationContext(), ImePopup.class);
ip.putExtra("score", elapsedSeconds);
startActivity(ip);

这是我的弹出类:

public class ImePopup extends Activity implements OnClickListener{

EditText ime;
Button ok, odustani;
double score;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.imepopup);

ok = (Button) findViewById(R.id.btOK);
odustani = (Button) findViewById(R.id.btOdustani);
ime = (EditText) findViewById(R.id.etIme);

ok.setOnClickListener(this);
odustani.setOnClickListener(this);
}


public void onClick(View arg0) {
switch (arg0.getId()){
case R.id.btOK:
boolean didItWork = true;
try{
String name = ime.getText().toString();

OffDBHelper entry = new OffDBHelper(ImePopup.this);
entry.open();
entry.createEntry(name, score);
entry.close();
break;
}catch(Exception e){
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Greska!");
TextView tv = new TextView(this);
tv.setText("Greska u upisu");
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Uspesno!");
TextView tv = new TextView(this);
tv.setText("Upisali ste se!");
d.setContentView(tv);
d.show();
}
}
case R.id.btOdustani:

break;
}
}
}

还有我的 OffDBHelper 类:

public class OffDBHelper {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_SCORE = "score";

private static final String DATABASE_NAME = "highscores";
private static final String DATABASE_TABLE = "highscorestable";
public static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper{

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

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_SCORE + " DOUBLE NOT NULL);"
);
}

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

}

public OffDBHelper(Context c){
ourContext = c;
}

public OffDBHelper open() throws Exception{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}

public long createEntry(String name, double score) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCORE, score);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_SCORE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iScore = c.getColumnIndex(KEY_SCORE);

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iScore) + "\n";
}

return result;


}
}

最佳答案

您可以像这样调用 ImePopup 中的 DBHelper

OffDBHelper entry = new OffDBHelper(ImePopup.this);
entry.open();
entry.createEntry(name, score);
entry.close();

但你永远不会向变量 score 写入数据,因此它的初始值始终为 0。

您需要首先从Intent中提取“分数”

关于java - 时间未保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15296274/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com