2014-09-01 28 views
0

嗨,我試圖使用SQL數據庫來顯示評級系統的新用戶。我可以讓用戶輸入名稱和費率,並將其保存到數據庫中。但當我去顯示數據庫中的一切,它不顯示評級欄,它顯示「[email protected]在Java中使用Sql Lite數據庫顯示評級欄

如果任何人都可以幫助我只顯示評級欄,這將是太棒了。 繼承人使用的一些代碼的副本。

Button previousEntries, save, getinfo1, editT,deleteT; 
    EditText date, diaryEntry,searchrow; 
    public RatingBar placerating; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 



     searchrow = (EditText) findViewById(R.id.EditText); 
     date = (EditText) findViewById(R.id.etExProg); 
     diaryEntry = (EditText) findViewById(R.id.etEntry1); 
     placerating = (RatingBar)findViewById(R.id.ratingBar); 


     getinfo1 = (Button) findViewById(R.id.getinfo); 
     editT = (Button) findViewById(R.id.edit); 
     deleteT = (Button) findViewById(R.id.delete); 
     previousEntries = (Button) findViewById(R.id.previousentry1); 
     save = (Button) findViewById(R.id.save1); 

     placerating.setOnClickListener(Test.this); 
     deleteT.setOnClickListener(Test.this); 
     editT.setOnClickListener(Test.this); 
     getinfo1.setOnClickListener(this); 
     previousEntries.setOnClickListener(this); 
     save.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View view){ 
     //switch statement for different options 
     switch (view.getId()) 
     { 
      case R.id.save1: 

        //get data from the edit text inputs 
        String theDate = date.getText().toString(); 
        String dEntry = String.valueOf(placerating); 

        //create an instance of class Entries 
        Entries entry = new Entries(Test.this); 
        entry.open(); 

        entry.createEntry(theDate, dEntry); 
        entry.close(); 



         //once save is succesful move out to exercise programmes class 
         Intent i = new Intent(Test.this,ExerciseProgrammes.class); 
         startActivity(i); 
        } 
       } 

      case R.id.previousentry1: 
       //if user selects all diary entries, show them. 
       Intent i = new Intent(Test.this,SQLReader.class); 
       startActivity(i); 
       break; 

這是設置和保存數據庫的類。謝謝!

//setting variable for the table 
public static final String KEY_ROWID = "_id"; 
public static final String KEY_EXERCISE_ROUT = "exercise_routine"; 
public static final String KEY_EXERCISE_INFO = "personal_info"; 

//setting variables for the database, the name 
public static final String DATABASE_NAME = "ProgressTracker"; 
public static final String DATABASE_TABLE = "userTable"; 
public static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 



//dbhelper class which set sup database 
private static class DbHelper extends SQLiteOpenHelper{ 
    public DbHelper(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    //first time dbhelper class called it goes here, and creates the table 
    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_EXERCISE_ROUT + " TEXT NOT NULL, " + 
       KEY_EXERCISE_INFO + " TEXT NOT NULL);" 
     ); 
    } 

    @Override 
    //any other time dbhelper class called, it goes here and updates table 
    public void onUpgrade(SQLiteDatabase db, int i, int i2) 
    { 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 
} 

public Entries(Context c){ //class name 
    ourContext = c; 
}//Entries 

public Entries open(){ //class name 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
}//open 

public void close(){ 
    ourHelper.close(); 
}//close 

//using the data passed in from parameters from exercisediary.class, insert data into table 
public long createEntry(String theDate, String dEntry){ 
    //setting up new contentValues varibale 
    ContentValues cv = new ContentValues(); 

    //cv.put method puts the info passed in from parameters in the cv variable 
    cv.put(KEY_EXERCISE_ROUT, theDate); 
    cv.put(KEY_EXERCISE_INFO, dEntry); 
    //entering the new values into teh table 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
}//CreateEntry 

public String getData(){ 
    String [] columns = new String []{KEY_ROWID, KEY_EXERCISE_ROUT, KEY_EXERCISE_INFO}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    //setting variables for each column in the database that needs to be returned 
    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iDate = c.getColumnIndex(KEY_EXERCISE_ROUT); 
    int iInfo = c.getColumnIndex(KEY_EXERCISE_INFO); 

    for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){ 
    result = result + c.getString(iRow)+ "."+c.getString(iDate)+c.getString(iInfo);    
    }       

    //return result to print out for user to see 
    return result; 
}//getData 

回答

0

一個RatingBar對象不是一個字符串,它沒有任何意義,試圖將其轉換成一個。

一個評級吧不是一個值,它一個值。 要獲取值,調用getRating()

float dEntry = placerating.getRating(); 
0

什麼你現在正在做的是你節省數據庫對象的描述。將String.valueOf(對象);將返回這個對象的可讀描述。如果你想保存額定值的值,那麼你必須使用getRating()方法,它將以浮點形式返回當前的額定值。

更改這些行:

entry.createEntry(theDate, placerating.getRating()); 

    public long createEntry(String theDate, float dEntry){ 
    } 

    @Override 
    //Change datatype to REAL 
    public void onCreate(SQLiteDatabase db) 
    { 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_EXERCISE_ROUT + " TEXT NOT NULL, " + 
       KEY_EXERCISE_INFO + " REAL)" 
     ); 
    } 

public String getData(){ 

    float iInfo = c.getColumnIndex(KEY_EXERCISE_INFO); 
}