2013-03-08 4 views
0

확인을 클릭하십시오. 게임이 끝나면 사용자에게 게임이 끝난 시간이 표시되고 그 직후에 dialog.theme 활동이 사용자 이름 입력을 위해 팝업됩니다. 이름은 sqlite 데이터베이스에 저장됩니다. 그리고 지금 내 문제는 .... 시간이 아닙니다. 시간은 double 유형의 변수에 저장됩니다. RezultatVreme.class에 값을 전송하면 작동합니다 (사용자에게 시간을 표시하는 팝업 활동에서 사용합니다 ... 입력 이름 팝업이 나타나기 전에 발생합니다). 이제는 같은 값을 사용하여 ImePopup.class로 보내고 사용하면 시간 값을 전달하고 내 데이터베이스에 입력하기 위해 이름을 입력 할 때 해당 클래스를 사용합니다. 하지만 작동하지 않습니다. 내 이름은 괜찮 았어.하지만 시간은 항상 0이야. 나는 정수, 그리고 긴 두 배로 시도했지만, 같은 결과가 나온다.시간이 데이터베이스에 저장되지 않습니다.

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; 


    } 
} 

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); 

가 여기 내 팝업 클래스의 : 여기가 팝업을 제시하고 내 다른 클래스에 값을 전달 내 게임 클래스, 그것의 일부는,이다

답변

1

당신은 그

OffDBHelper entry = new OffDBHelper(ImePopup.this); 
entry.open(); 
entry.createEntry(name, score); 
entry.close(); 
처럼 ImePopup에 DBHelper를 호출

하지만 그렇게 항상 당신은 당신이 바로있는 Intent

+0

에서 당신의 "점수"를 추출 할 필요가 0

이다 초기 값을 가지고 당신의 변수 score에 데이터를 기록하지 않습니다. 나는 그 작은 일을 완전히 잊었다. 고마워. 다른 사람이 필요로 할 경우에 대비하여 마지막 수정본에 수정 사항을 게시합니다. – user2083882

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(); 
관련 문제