简体   繁体   English

Android:RatingBar-如何向数据库添加评级?

[英]Android: RatingBar - How to add rating to DB?

Noob here again. 菜鸟又来了。

I've followed the example given by Google Dev (and read a couple of other tutorials) on how to add a RatingBar to your form/app. 我遵循了Google Dev给出的示例(并阅读了其他一些教程),以了解如何在您的表单/应用程序中添加RatingBar。 However I have been unable to find any examples on the net, of how to take the Rating given and add it as a value to your SQLite database. 但是,我在网上找不到任何有关如何使用给定的Rating并将其作为值添加到SQLite数据库的示例。

Currently you can add your own rating by simply putting a number in an EditText field. 当前,您只需在EditText字段中输入数字即可添加自己的评分。 However this is not ideal, and the RatingBar seems like a more ideal solution. 但是,这并不理想,RatingBar似乎是一个更理想的解决方案。 I suspect it has something to do with OnClick() but Im not entirely sure being a beginner. 我怀疑这与OnClick()有关,但我不确定自己是初学者。

Any ideas? 有任何想法吗?

RatingBar: RatingBar:

final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
        ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Toast.makeText(AddBook.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();

        }
    });

AddRow: 添加行:

db.addRow
        (

                textFieldOne.getText().toString(),
                textFieldTwo.getText().toString(),
                textFieldThree.getText().toString(),
                textFieldFour.getText().toString(),
                textFieldFive.getText().toString(),
                textFieldSix.getText().toString()





        );

        // request the table be updated
        updateTable();

Let's assume that you have an app. 假设您有一个应用程序。

Let's further assume that you have a user. 让我们进一步假设您有一个用户。

Let's keep going out on this limb and assume that you have an an activity in the app, in which the user will fill in data, and that the objective is for this data to wind up in a SQLite database. 让我们继续前进,假设您在应用程序中有一个活动,用户将在其中填写数据,并且目标是在SQLite数据库中存储这些数据。

Somewhere, somehow, the user has to indicate to you "Please save this data". 用户必须以某种方式以某种方式向您指示“请保存此数据”。 It could be via a button, or a menu choice, or leaving the activity (eg, onStop() ), or whatever. 可以通过按钮或菜单选择,也可以通过退出活动(例如onStop() )等等。 I will call this the "trigger". 我将其称为“触发”。

In your EditText scenario, when the trigger occurs, you would do an insert() or update() or whatever to the SQLiteDatabase with the contents of the EditText , retrieved via getText() . 在您的EditText场景中,当发生触发器时,您将对insert() update()或对SQLiteDatabase进行任何操作,并使用通过getText()检索的EditText的内容。

In your RatingBar scenario, when the trigger occurs, you would do an insert() or update() or whatever to the SQLiteDatabase with the contents of the RatingBar , retrieved via getRating() . 在您的RatingBar方案中,当触发发生时,您将对insert() update()或对SQLiteDatabase进行任何处理,并使用通过getRating()检索到的RatingBar的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM