2013-03-08 41 views
0

好的,這是我的場景。在我的比賽結束後,一個用戶被提供他完成比賽的時間,之後我的dialog.theme活動彈出用於用戶名輸入。該名稱存儲在sqlite數據庫中。現在我的問題....時間不是。時間存儲在一個雙重類型的變量中。當我將該值發送到我的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; 


    } 
} 

回答

1

您調用DBHelper在ImePopup像

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

但你永遠不將數據寫入到您的變量score所以它總是它的初始值是0

你需要提取你的「分數」走出Intent第一

+0

你是對的。我完全忘了那件小事。非常感謝。如果有人需要,我會在最後一篇文章中發佈我的更正。 – user2083882 2013-03-08 14:46:56

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(); 
相關問題