简体   繁体   中英

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. 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.

Currently you can add your own rating by simply putting a number in an EditText field. However this is not ideal, and the RatingBar seems like a more ideal solution. I suspect it has something to do with OnClick() but Im not entirely sure being a beginner.

Any ideas?

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.

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. 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() .

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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