时间不保存在数据库中

时间:2013-03-08 14:28:31

标签: java android sqlite

好的,这是我的情景。在我的游戏结束后,向用户提供他完成游戏的时间,并且在此之后我的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;


    }
}

2 个答案:

答案 0 :(得分:1)

你在ImePopup中调用DBHelper就像那样

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

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

您需要从Intent第一个

中提取“得分”

答案 1 :(得分: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();