2016-01-26 21 views
0

我有這個應用程序,當MainActivity被調用時,一個文本視圖將顯示當前的數據信息或更確切地說,如果COL1(星號)爲空,它應該在列星中寫入0。然後,當播放和到達時,分數應該改變的時候,比如添加分數,它應該覆蓋列中的數據。如何在SQLite中寫入/覆蓋和讀取數據整數並在TextView中顯示?

這裏是我的代碼爲我DatabaseHelper

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

public static final String DATABASE_NAME = "star.db"; 
public static final String TABLE_NAME = "stars_table"; 
public static final String COL1 = "stars"; 


public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, 1); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table" + TABLE_NAME + "("+COL1+")"); 
} 

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

public boolean insertData(Integer stars){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL1, stars); 
    long result = db.insert(TABLE_NAME, null, contentValues); 
    if(result == -1) 
     return false; 
    else 
     return true; 
} 

public Cursor getAllData(){ 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select stars from" + TABLE_NAME, null); 
    return res; 
} 

} 

所以,我怎麼能顯示它的TextView以及如何讀取數據,計算,然後用新的數據覆蓋它嗎?

至於我的MainActivity

public class MainActivity extends Activity { 

    private static TextView txt_stars; 
    DatabaseHelper mydb; 

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

    mydb = new DatabaseHelper(this); 

    txt_stars = (TextView) findViewById(R.id.txtStars); 
    //this where the score will show 
    } 

    public void onButtonClickListener() { 
    //some buttons 
    //game start 
    } 
} 

至於如果答案是正確的

public class OneFifteenActivity extends Activity { 

    private static Button button_next; 
    private static TextView txt_ans; 
    final Context context = this; 

    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.one_fifteen); 
    onButtonClickListener(); 
    } 
    public void onButtonClickListener() { 

    txt_ans = (EditText) findViewById(R.id.edittxtAns); 
    button_next = (Button) findViewById(R.id.btnNext); 
    button_next.setOnClickListener(
     new View.OnClickListener() {@ 
     Override 
     public void onClick(View v) { 
      if (txt_ans.getText().toString().equals("dai")) { 

      AlertDialog.Builder alert = new AlertDialog.Builder(context); 
      alert.setMessage("Message here"); 
      alert.setCancelable(false); 
      alert.setPositiveButton("next", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        Intent nextForm = new Intent(".MainActivity"); 
        startActivity(nextForm); 
        //computation here 

       } 
      }); 
      AlertDialog alertDialog = alert.create(); 
      alertDialog.show(); 

      } else { 
      AlertDialog.Builder alert = new AlertDialog.Builder(context); 
      alert.setMessage("Message here"); 
      alert.setCancelable(false); 
      alert.setPositiveButton("next", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        AlertDialog.Builder alert = new AlertDialog.Builder(context); 
        alert.setTitle("Congratulations!"); 
        alert.setMessage("You have earned 50 stars"); 
        alert.setCancelable(false); 
        alert.setPositiveButton("next", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         Intent nextForm = new Intent(".MainActivity"); 
         startActivity(nextForm); 
         } 
        }); 
        AlertDialog alertDialog = alert.create(); 
        alertDialog.show(); 
       } 
      }); 
      AlertDialog alertDialog = alert.create(); 
      alertDialog.show(); 
      } 
     } 
     } 
    ); 
    } 

} 

我已經困在這裏了幾個星期,有人可以幫我將計算活動?

非常感謝。

+0

你能把你的完整活動代碼放在這裏嗎? – thedarkpassenger

+0

已更新它。 – kcNeko

回答

1

我想更新您的表並添加點,你可以創建一個函數「加分」的作品與此類似:

public void _addPoints(){ 
db.rawQuery("UPDATE ? FROM "+TABLE_NAME+" SET ?=?+1", new String[]{COL1, COL1, COL1}); //Here I don't know if you have multiple users or players where this query must be used, if there are 
please add the needed WHERE x=y and subsitute the 1 for your desired increase value. 
} 

現在在TextView中顯示您必須閱讀值可以使返回的星數的函數:

Cursor c=db.query(TABLE_NAME, COL1, null, null, null, null); //if you have only 1 player, if you have more you must fill the WHERE parameters (check API for that). 
c.moveToNext(); 
int points=c.getInt(0); //or anything else you need 
return points; 

然後設置TextView的文本:

txt_stars.setText(Integer.toString(points)); 

讓我知道如果這有幫助,也許我不明白這個問題,以及我認爲。

+0

爲了更好的理解,我更新了上面的代碼。我應該將這個函數添加到DatabaseHelper類嗎? – kcNeko

+0

對於我讀過的,最好的做法是創建一個新類,並讓DatabaseHelper成爲它的一個子類(這樣您就不會錯誤地修改任何DatabaseHelper方法),並且您可以添加函數_updateTable。 直接在你做DatabaseHelper類可能會伎倆,但我不認爲這是最好的做法。請讓我知道這是否適合你。 – LuisE

+0

對不起,我在AS有點新,你能告訴我怎麼做嗎? – kcNeko

1

根據你的要求,使DatabaseHelper類,你可以繼續這樣的一個子類:

class Functions{ 
public update(){ 
//the update code I already provided 
} 
class DatabaseHelper extends SQLiteOpenHelper{ 
//the code of your databaseHelper 
} 
} 

正如你所看到的,類DatabaseHelper是類函數中,被稱爲一個子類,它有您可以使用所有變量(即使它們是私有的)和DatabaseHelper類的函數來完成函數類中所需的任何函數,而類DatabseHelper不能用於程序中除函數類之外的其他任何地方。

0

檢查出一些信息,您可以使用此代碼:

private String[] checkAll(){ 
    String tam[] = new String[5]; 
    Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null); 
    if (cursor.getCount() != 0) { 
     if (cursor.moveToFirst()) { 
      do { 
       tam[0] = cursor.getString(cursor 
         .getColumnIndex("_id")); 

       tam[1] = cursor.getString(cursor 
         .getColumnIndex("column1Name")); 

       tam[2] = cursor.getString(cursor 
         .getColumnIndex("column2Name")); 

       tam[3] = cursor.getString(cursor 
         .getColumnIndex("column3Name")); 

       tam[4] = cursor.getString(cursor 
         .getColumnIndex("column4Name")); 
      } while (cursor.moveToNext()); 
     } 
    } else { 
     // Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_LONG).show(); 
    } 
    cursor.close(); 
    return tam; 
}// checkAll 

然後在mainactivity或任何你想要它

String arrray[] = checkAll(); 
textview.setText(arrray[SomePosition]); 
Toast.makeText(getApplicationContext,"HERE IS A MESSAGE TO THE USER "+arrray[SomePosition], Toast.LENGTH_LONG).show(); 

最後,如果要更新,使用此:

String[] args = new String[]{"1"}; // String to complete the condition(where) 
     db.execSQL("UPDATE " + TABLE_NAME + " SET Column1Name='" + someVariable + "', Column2Name='"+SomeOtherVariable+"' WHERE idNameColumn=?", args); 
相關問題