2017-03-13 6 views
0

私はMainActivityに2つのTextViewを持っており、最初のTextViewを自分の最高得点に変更できますが、2番目のTextViewを次の最高得点に変更することはできません(私はゲームを作っています)。私が悩んでいるコードは、DBHandlerクラスのupdateDatabase()メソッドにあります。SQLiteを使って別のTextViewにジャンプするには?

MainActivity

public class MainActivity extends Activity { 

//sqlite 
//EditText input; 
//first high score 
static TextView productText; 
static TextView productText2; 
MyDBHandler dbHandler; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //sqlite 
    //input = (EditText)findViewById(R.id.editText_ID); 
    productText = (TextView)findViewById(R.id.textView1); 
    productText2 = (TextView)findViewById(R.id.textView2); 
    //GameScreen gameScreen = new GameScreen(); 
    dbHandler = new MyDBHandler(this, null, null, 1); 
    dbHandler.updateDatabase(); 
} 

public void sendMessage(View view) 
{ 
    Intent intent = new Intent(MainActivity.this, GameScreen.class); 
    startActivity(intent); 
} 

}

GameActivity

public class GameScreen extends Activity { 

private TextView time; 
private ImageButton start; 
private ImageButton gameButton; 
private ImageButton button2; 
private CountDownTimer countDownTimer; 
public static int count = 0; 
public static int countPass = 0; 
public String countPassString = ""; 
MyDBHandler dbHandler; 
//MainActivity mainActivity = new MainActivity(); 

final Handler handler = new Handler(); 
final Runnable r = new Runnable() { 
    public void run() { 
     cancel(); 
    } 
}; 




private View.OnClickListener btnClickListener = new View.OnClickListener(){ 

    @Override 
    public void onClick(View v) { 

     switch(v.getId()) { 
      case R.id.start_ID: 
       start(); 
       break; 
      case R.id.gameButton_ID: 
       gameButton(); 
       break; 
      case R.id.button2_ID: 
       button2(); 
       break; 
     } 

    } 


}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_game_screen); 


    start = (ImageButton) findViewById(R.id.start_ID); 
    start.setOnClickListener(btnClickListener); 
    time = (TextView) findViewById(R.id.time); 
    gameButton = (ImageButton) findViewById(R.id.gameButton_ID); 
    gameButton.setOnClickListener(btnClickListener); 
    button2 = (ImageButton) findViewById(R.id.button2_ID); 
    button2.setOnClickListener(btnClickListener); 

    button2.setVisibility(View.GONE); 

} 

public void start(){ 

    count = 0; 
    start.setVisibility(View.GONE); 

    time.setText("100"); 
    //this doesnt work and makes app crash when you hit start button 

    countDownTimer = new CountDownTimer(100 * 1000, 1000) { 
     @Override 
     public void onTick(long millsUntilFinished){ 
      time.setText("" + millsUntilFinished/1000); 

      //turns textview string to int 
      int foo = Integer.parseInt(time.getText().toString()); 

      if(foo == 93) { 
       time.setVisibility(View.GONE); 
       button2.setVisibility(View.VISIBLE); 
      } 
      else if(foo == 97) { 
       gameButton.animate().translationX(200).setDuration(5000).start(); // move away 
      } 
      else if(foo == 90){ 
       gameButton.animate().translationY(200).setDuration(5000).start(); // move away 
      } 

      if(foo % 2 == 0){ 

       handler.postDelayed(r, 1000); 

      } 
     } 

     public void onFinish() { 
      start.setVisibility(View.VISIBLE); 
      time.setVisibility(View.VISIBLE); 
      time.setText("Done !"); 

      //need to check if score will reset if you win the game 

      Toast.makeText(getApplicationContext(), "You scored " + count, Toast.LENGTH_LONG).show(); 
      gameButton.clearAnimation(); 
      gameButton.animate().translationX(0).setDuration(500).start(); //move back 
      gameButton.animate().translationY(0).setDuration(500).start(); //move back 
      button2.setVisibility(View.GONE); 
     } 
    }; 
    countDownTimer.start(); 
} 

private void cancel(){ 
    if(countDownTimer != null){ 
     countDownTimer.cancel(); 
     countDownTimer = null; 
     start.setVisibility(View.VISIBLE); 
     time.setVisibility(View.VISIBLE); 
     gameButton.clearAnimation(); 
     gameButton.animate().translationX(0).setDuration(500).start(); //move back 
     gameButton.animate().translationY(0).setDuration(500).start(); //move back 
     button2.setVisibility(View.GONE); 

     //holds score at end of game 
     countPass = count; 
     //reset score for new game 
     count = 0; 
     Toast.makeText(getApplicationContext(), "You scored " + countPass, Toast.LENGTH_LONG).show(); 
     countPassString = Integer.toString(countPass); 
     dbHandler = new MyDBHandler(this, null, null, 1); 
     //mainActivity.productText.setText(countPassString); 
     dbHandler.addButtonClicked(countPassString); 
    } 
} 

private void gameButton(){ 


    int foo = Integer.parseInt(time.getText().toString()); 

    if(foo % 2 == 0) { 
     final Toast toast = Toast.makeText(getApplicationContext(), "+1", Toast.LENGTH_SHORT); 
     toast.show(); 

     // makes +1 toast half a second 
     Handler gameButtonToastHandler = new Handler(); 
     gameButtonToastHandler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       toast.cancel(); 
      } 
     }, 500); 

     handler.removeCallbacks(r); 
     ++count; 
    } 
    else{ 
     cancel(); 
    } 
} 

private void button2(){ 
    cancel(); 
} 

}

DBHandler

public class MyDBHandler extends SQLiteOpenHelper { 

private static final int DATABASE_VERSION = 4; 
private static final String DATABASE_NAME = "products.db"; 
public static final String TABLE_PRODUCTS = "products"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_PRODUCTNAME = "productname"; 

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { 
    super(context, DATABASE_NAME, factory, DATABASE_VERSION); 
} 


@Override 
public void onCreate(SQLiteDatabase db) { 
    String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COLUMN_PRODUCTNAME + " TEXT " + 
      ");"; 
    db.execSQL(query); 
} 

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

//add a new row to the database 
public void addProduct(Products product){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues values = new ContentValues(); 
    values.put(COLUMN_PRODUCTNAME, product.get_productname()); 
    db.insert(TABLE_PRODUCTS, null, values); 
    db.close(); 
} 

//code coming from MainActivity 
public void addButtonClicked(String highscore1){ 
    Products product = new Products(highscore1); 
    addProduct(product); 
    updateDatabase(); 
} 

//code coming from MainActivity 
public void updateDatabase(){ 
    if(MainActivity.productText.toString() == ""){ 
     String dbString = databaseToString(); 
     MainActivity.productText.setText(dbString); 
    } 
    else if(MainActivity.productText2.toString() == "2:"){ 
     String dbString = databaseToString(); 
     MainActivity.productText2.setText(dbString); 
    } 
    else{ 
     String dbString = databaseToString(); 
     MainActivity.productText.setText(dbString); 
    } 

} 

//delete a product from the database 
public void deleteProduct(String productName){ 
    SQLiteDatabase db = getWritableDatabase(); 
    db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";"); 
} 


//print out the database as a string 
public String databaseToString(){ 
    String dbString = ""; 
    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1"; 


    //Cursor point to location in your results 

    Cursor c = db.rawQuery(query, null); 
    //Move to the first row in your results 
    c.moveToFirst(); 

    while(!c.isAfterLast()){ 
     if(c.getString(c.getColumnIndex("productname")) != null){ 
      dbString += c.getString(c.getColumnIndex("productname")); 
      dbString += "\n"; 
     } 
     c.moveToNext(); 
    } 


    db.close(); 
    return dbString; 

} 

}

製品

public class Products { 
private int _id; 
private String _productname; 

public Products(){ 

} 

public Products(String productname){ 
    this._productname = productname; 
} 

public void set_id(int _id) { 
    this._id = _id; 
} 

public void set_productname(String _productname) { 
    this._productname = _productname; 
} 

public int get_id() { 
    return _id; 
} 

public String get_productname() { 
    return _productname; 
} 

}

答えて

0

あなたはSQLiteOpenHelperからUIコンポーネントを操作すべきではありません。このヘルパーをJUSTデータベース操作で残しておいてください。あなたの活動について、以下のクエリのカーソルをつかんでください。

String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1"; 

あなたのUIを更新してください。

メインのUIスレッドでこれを実行しないように注意してください。 Loaders

+1

"以下のクエリのカーソルを取得する"とはどういう意味ですか? –

関連する問題