如何在使用评级栏时使用自定义列表视图将最高评分的项目显示到另一个活动中?

时间:2012-12-12 05:46:13

标签: android android-layout android-intent android-emulator android-widget

我制作了一个Android评分栏程序来接受用户评分,计算总评分并将其显示在指标评级栏中。

现在我想在评分栏计划中添加两个功能:

  1. 我想在ListView中使用我现有的评级栏
  2. 其次希望在MyRatings等其他活动中显示从高到低评级的项目
  3. 我的Java代码: -

    public class InteractiveRatingBarActivity extends Activity implements
    OnRatingBarChangeListener {
    RatingBar getRatingBar;
    RatingBar setRatingBar;
    TextView countText;
    int count;
    float curRate;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        findViewsById();
    
        setRatingBar.setRating(curRate);
        getRatingBar.setOnRatingBarChangeListener(this);
    }  
    
    private void findViewsById() {
        getRatingBar = (RatingBar) findViewById(R.id.getRating);
        setRatingBar = (RatingBar) findViewById(R.id.setRating);
        countText = (TextView) findViewById(R.id.countText);
    }
    
    public void onRatingChanged(RatingBar rateBar, float rating,
            boolean fromUser) {
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
                / ++count));
        Toast.makeText(InteractiveRatingBarActivity.this,
                "New Rating: " + curRate, Toast.LENGTH_SHORT).show();
        setRatingBar.setRating(curRate);
        countText.setText(count + " Ratings");
    }
    }
    

    main.xml中:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <RatingBar
        android:id="@+id/getRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="1.0" />
    
    <RatingBar
        android:id="@+id/setRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/getRating"
        android:isIndicator="true"
        android:numStars="5"
        android:stepSize="0.1" />
    
    <TextView
        android:id="@+id/countText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/getRating"
        android:layout_toRightOf="@+id/setRating"
        android:paddingLeft="10dp" />
    </RelativeLayout>
    

0 个答案:

没有答案