2013-09-30 62 views
1

我試圖從我的數據庫檢索並顯示一行到一個活動。我想知道是否可以使用Getters和Setter來傳遞值。我能夠輕鬆地檢索數據,但問題是,我在使用getter和setter傳遞值時遇到問題。這是我在LogCat中獲得的。使用Getters和Setter顯示來自SQLite的單個數據

09-22 13:46:33.573: D/Enter Activity(3110): You have entered Score Activity 
09-22 13:46:34.177: D/Data Retreival(3110): Getting Data 
09-22 13:46:34.183: D/User(3110): 1 XyLoL 0 0 null null 
09-22 13:46:34.183: D/Database(3110): Database Close 
09-22 13:46:34.194: D/USER INFO(3110): 0 null 0 0 null null 
09-22 13:46:34.194: W/ResourceType(3110): No package identifier when getting value for resource number 0x00000000 
09-22 13:46:34.203: D/AndroidRuntime(3110): Shutting down VM 
09-22 13:46:34.203: W/dalvikvm(3110): threadid=1: thread exiting with uncaught exception (group=0x40014760) 
09-22 13:46:34.223: E/AndroidRuntime(3110): FATAL EXCEPTION: main 
09-22 13:46:34.223: E/AndroidRuntime(3110): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xylol.o_chabeta/com.xylol.o_chabeta.Enter_Score}: android.content.res.Resources$NotFoundException: String resource ID #0x0 

我只想問,如果它

User_Score.java(活動)

public class Enter_Score extends Activity implements OnClickListener { 

    ScoreDataSource datasource; 
    UserHandler user; 

    //Textview Declaration 

    TextView username; 
    TextView score; 
    TextView questions_left; 
    TextView best_cat; 
    TextView worst_cat; 

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

     username = (TextView) findViewById(R.id.stats_username); 
     score = (TextView) findViewById(R.id.stat_score); 
     questions_left = (TextView) findViewById(R.id.stat_questions_left); 
     best_cat = (TextView) findViewById(R.id.stats_best); 
     worst_cat = (TextView) findViewById(R.id.stats_worst); 

     datasource = new ScoreDataSource(this); 
     datasource.open(); 
     Log.d("Data Retreival", "Getting Data"); 
     datasource.getScore(); 
     datasource.close(); 


     UserHandler user = new UserHandler(); 

     long user_id = user.getUSER_ID(); 
     String user_n =user.getUSER_NAME(); 
     int user_s =user.getUSER_SCORE(); 
     int user_ql= user.getUSER_QUESTIONS_LEFT(); 
     String user_b = user.getUSER_BEST_CATEGORY() ; 
     String user_w = user.getUSER_WORST_CATEGORY(); 

     Log.d("USER INFO", user_id+ " " + user_n + " " + user_s + " " + user_ql + " " + user_b + " " + user_w); 

     username.setText(user_n); 
     score.setText(user_s); 
     questions_left.setText(user_ql); 
     best_cat.setText(user_b); 
     worst_cat.setText(user_w); 


     Button b = (Button) findViewById(R.id.back); 
     b.setOnClickListener(this); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.enter__score, menu); 
     return true; 
    } 

    @Override 
    public void onClick(View v) { 

     if(v.getId() == R.id.back){ 
      Intent i = new Intent(Enter_Score.this, MainActivity.class); 
      startActivity(i); 
      finish(); 
     } 

    } 



} 

ScoreDataSource.java(數據Retrival)

public class ScoreDataSource extends Enter_Score{ 


    SQLiteOpenHelper dbhelper; 
    SQLiteDatabase database; 


    private static final String[] allColumns = { 
     O_chaDBOpenHelper.USER_ID, 
     O_chaDBOpenHelper.USER_NAME, 
     O_chaDBOpenHelper.USER_SCORE, 
     O_chaDBOpenHelper.USER_QUESTIONS_LEFT, 
     O_chaDBOpenHelper.USER_BEST_CATEGORY, 
     O_chaDBOpenHelper.USER_WORST_CATEGORY 
    }; 

    public ScoreDataSource(Context context){ 

     dbhelper = new O_chaDBOpenHelper(context); 
    } 

    public void open() { 
     database = dbhelper.getReadableDatabase(); 
    } 

    public void close() { 
     Log.d("Database", "Database Close"); 
     dbhelper.close(); 
    } 

    public void getScore() { 

     Cursor c = database.query(O_chaDBOpenHelper.USER_TABLE, allColumns, 
       null, null, null, null, null); 
       c.moveToFirst(); 

     int iid = c.getColumnIndex(O_chaDBOpenHelper.USER_ID); 
     int iusername = c.getColumnIndex(O_chaDBOpenHelper.USER_NAME); 
     int iscore = c.getColumnIndex(O_chaDBOpenHelper.USER_SCORE); 
     int iql = c.getColumnIndex(O_chaDBOpenHelper.USER_QUESTIONS_LEFT); 
     int ibc = c.getColumnIndex(O_chaDBOpenHelper.USER_BEST_CATEGORY); 
     int iwc = c.getColumnIndex(O_chaDBOpenHelper.USER_WORST_CATEGORY); 

     // Get and Set Valueszzz 

     long id = c.getLong(iid); 
     String username = c.getString(iusername); 
     int score = c.getInt(iscore); 
     int questions_left = c.getInt(iql); 
     String best_cat = c.getString(ibc); 
     String worst_cat = c.getString(iwc); 

     Log.d("User", id+ " " + username+ " " + score+ " " + questions_left + " " + best_cat + " " + worst_cat); 

     UserHandler user = new UserHandler(); 

     user.setUSER_ID(id); 
     user.setUSER_NAME(username); 
     user.setUSER_SCORE(score); 
     user.setUSER_QUESTIONS_LEFT(questions_left); 
     user.setUSER_BEST_CATEGORY(best_cat); 
     user.setUSER_WORST_CATEGORY(worst_cat); 


    } 

} 

UserHandler.java(吸氣劑和安裝員)

public class UserHandler { 

    private long USER_ID; 
    private String USER_NAME; 
    private int USER_SCORE; 
    private int USER_QUESTIONS_LEFT; 
    private String USER_BEST_CATEGORY; 
    private String USER_WORST_CATEGORY; 

    public UserHandler(){ 

    } 

    public UserHandler(int ID, String NAME, String GENDER, String COUNTRY, int SCORE, int QUESTION_LEFT, String BEST_CATEGORY, String WORST_CATEGORY){ 
     this.USER_ID = ID; 
     this.USER_NAME = NAME; 
     this.USER_SCORE = SCORE; 
     this.USER_QUESTIONS_LEFT = QUESTION_LEFT; 
     this.USER_BEST_CATEGORY = BEST_CATEGORY; 
     this.USER_WORST_CATEGORY = WORST_CATEGORY; 
    } 

    public long getUSER_ID() { 
     return USER_ID; 
    } 

    public void setUSER_ID(long uSER_ID) { 
     this.USER_ID = uSER_ID; 
    } 

    public String getUSER_NAME() { 
     return USER_NAME; 
    } 

    public void setUSER_NAME(String uSER_NAME) { 
     this.USER_NAME = uSER_NAME; 
    } 

    public int getUSER_SCORE() { 
     return USER_SCORE; 
    } 

    public void setUSER_SCORE(int uSER_SCORE) { 
     this.USER_SCORE = uSER_SCORE; 
    } 

    public int getUSER_QUESTIONS_LEFT() { 
     return USER_QUESTIONS_LEFT; 
    } 

    public void setUSER_QUESTIONS_LEFT(int uSER_QUESTIONS_LEFT) { 
     this.USER_QUESTIONS_LEFT = uSER_QUESTIONS_LEFT; 
    } 

    public String getUSER_BEST_CATEGORY() { 
     return USER_BEST_CATEGORY; 
    } 

    public void setUSER_BEST_CATEGORY(String uSER_BEST_CATEGORY) { 
     this.USER_BEST_CATEGORY = uSER_BEST_CATEGORY; 
    } 

    public String getUSER_WORST_CATEGORY() { 
     return USER_WORST_CATEGORY; 
    } 

    public void setUSER_WORST_CATEGORY(String uSER_WORST_CATEGORY) { 
     this.USER_WORST_CATEGORY = uSER_WORST_CATEGORY; 
    } 

} 

回答

0

您在User_Score.java和ScoreDataSource.java中創建的UserHandler對象是不同的。因此你沒有得到正確的價值。

也許你可以創建UserHandler類Singleton。

+0

我該怎麼做? –

+0

@John:如果你問的是單身人士,只需要谷歌它,你會得到很多有用的鏈接。 –

+0

@JohnMichaelJames:我想指出,如果你的問題得到完全解決,那麼只需將其標記爲已接受。 –