MainActivity.java
int averageRating = db.getUserRating(usersAddr);
Log.d("rating value",""+averageRating);
TextView view = (TextView) findViewById(R.id.rating);
view.setText("Rating:"+averageRating);
ll = (LinearLayout)findViewById(R.id.ll);
Databasehelper.java
public int getUserRating(String userMac)
{
int avgRating = 0 ;
String selectQuery = "SELECT AVG(rate) as average FROM " + TABLE_RATING + " WHERE "
+ KEY_RECEIVERID + "='" + userMac+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor!= null)
{
if (cursor.moveToFirst()) {
Log.d("CURSOR POSITION", ""+cursor.getPosition());
avgRating = cursor.getInt((cursor.getPosition()));
Log.d("average", ""+cursor.getInt((cursor.getPosition())));
System.out.println(cursor.getInt((cursor.getPosition())));
}
}
return avgRating;
}
XML文件
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
以下是从数据库中检索平均评分的代码。我将其显示为文本。但我想以评级栏的形式显示评级。你能帮助我吗?
答案 0 :(得分:0)
像这样评级栏:
<RatingBar
android:id="@+id/ratingBar1"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后相应地更改您的活动:
public class MainActivity extends Activity
{
RatingBar ratingBar;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
ratingBar.setRating(4); //put your value here like ratingBar.setRating(averageRating);
// Set ChangeListener to Rating Bar
ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
Toast.makeText(getApplicationContext(),"Your Selected Ratings : " + String.valueOf(rating),Toast.LENGTH_LONG).show();
}
});
}
}