首页 > 解决方案 > 当用户按下提交按钮时如何保存用户的评分和评论?

问题描述

我是一个初学者程序员,我正在开发一个显示一些项目的应用程序。用户可以点击任何项目,阅读它的描述,也可以对它进行评分和评论。我创建了一个评分栏和一个评论部分(还有一个显示平均评分的文本视图)。

我希望在每个用户按下按钮 mSendFeedback 时保存他们的评分和评论。我怎样才能做到这一点?

项目页面的 Java 文件

public class ItemDemo extends AppCompatActivity {
    RatingBar mRatingBar;
    TextView mRatingScale;
    TextView averageRating;
    EditText mFeedback;
    Button mSendFeedback;
    int i=0;
    float total = 0;
    float average;

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

        mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
        mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
        mFeedback = (EditText) findViewById(R.id.etFeedback);
        mSendFeedback = (Button) findViewById(R.id.btnSubmit);


        mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                mRatingScale.setText(String.valueOf(v));
                switch ((int) ratingBar.getRating()) {
                    case 1:
                        mRatingScale.setText("Bad");
                        break;
                    case 2:
                        mRatingScale.setText("Okay");
                        break;
                    case 3:
                        mRatingScale.setText("Not bad");
                        break;
                    case 4:
                        mRatingScale.setText("Good enough");
                        break;
                    case 5:
                        mRatingScale.setText("Perfect");
                        break;
                    default:
                        mRatingScale.setText("");
                }
            }
        });

        mSendFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRatingBar.getRating() == 0.0) {
                    Toast.makeText(EventDemo.this, "Please enter a rating", Toast.LENGTH_LONG).show();
                } else {
                    mFeedback.setText("");
                    mRatingBar.setRating(0);
                    i=i+1;
                    total += mRatingBar.getRating();
                    average = total /i ;
                    Toast.makeText(EventDemo.this, "Thanks for the rating", Toast.LENGTH_SHORT).show();
                }
            }
        });
        TextView textView = (TextView) findViewById(R.id.averageRating);
        textView.setText("average rating: " +average+"/5 stars");
    }



}

项目页面的 XML 文件

 <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="177dp"
                app:srcCompat="@drawable/item1" />

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/imageView4"
                android:layout_marginTop="19dp"
                android:gravity="center|center_horizontal"
                android:text="Item 1"
                android:textAllCaps="false"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:textColor="@color/design_default_color_primary_dark"
                android:textColorHighlight="@color/colorPrimaryDark"
                android:textColorLink="@color/design_default_color_primary"
                android:textStyle="bold"
                tools:layout_centerHorizontal="true" />

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView11"
                android:layout_marginTop="16dp"
                android:gravity="center"
                android:text="This is the description of item 1"
                android:textColor="@color/colorAccent"
                android:textStyle="italic"
                tools:layout_centerHorizontal="true" />

            <RatingBar
                android:id="@+id/ratingBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/textView12"
                android:layout_gravity="center"
                android:numStars="5"
                android:stepSize="1" />

            <TextView
                android:id="@+id/tvRatingScale"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/ratingBar"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/averageRating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/tvRatingScale"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:textSize="16sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/etFeedback"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/averageRating"
                android:gravity="top"
                android:hint="add a comment"
                android:inputType="textMultiLine"
                android:lines="5" />

            <Button
                android:id="@+id/btnSubmit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:background="#e57373"
                android:text="Submit Feedback"
                android:textColor="@android:color/white" />

标签: javaandroidbuttonsavecomments

解决方案


如果你能提到你想保存它会很好吗?在本地还是在某些服务器/云上?我假设第二个选项作为用户给出的评级的目的是它也应该对其他用户可见。

如果是这种情况,我建议使用Firebase 实时数据库

你应该做的是为它创建一个类,比如:

class Rating {
    int value;
    Date date;
}

您必须将给定的评级存储在上述类的对象中。然后,您可以将这些对象存储在 Firebase 实时数据库中。

如果您有用户注册过程,请也存储用户 ID 以避免重复评分。

为此,使用 Firebase 非常简单方便。按照他们使用 Android Studio 插件的示例,您将在几分钟内完成此操作。


推荐阅读