0

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

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;


    }
}
4

2 回答 2

1

你像这样在 ImePopup 中调用 DBHelper

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

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

你需要从Intent第一个中提取你的“分数”

于 2013-03-08T14:34:34.967 回答
0
double score;
double scoreR;



Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {
           scoreR = extras.getDouble("score", 0);
        }


OffDBHelper entry = new OffDBHelper(ImePopup.this);
            entry.open();
            entry.createEntry(name, scoreR);
            entry.close();
于 2013-03-08T14:47:47.317 回答