Android:RatingBar - 如何向数据库添加评级?
菜鸟又来了。
我遵循了 Google Dev 给出的示例(并阅读了一些其他教程),了解如何将评级栏添加到您的表单/应用程序中。然而,我无法在网上找到任何示例,说明如何获取给定的评级并将其作为值添加到 SQLite 数据库中。
目前,您只需在 EditText 字段中输入数字即可添加自己的评级。然而这并不理想,RatingBar 似乎是一个更理想的解决方案。我怀疑它与 OnClick() 有关,但我不完全确定我是初学者。
有什么想法吗?
评级栏:
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();
}
});
添加行:
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您有一个应用程序。
让我们进一步假设您有一个用户。
让我们继续讨论这个问题,假设您在应用程序中有一个活动,用户将在其中填写数据,并且目标是让这些数据最终进入 SQLite 数据库。
在某个地方,以某种方式,用户必须向您指示“请保存此数据”。它可以通过按钮、菜单选择、或离开活动(例如,
onStop()
)或其他方式。我将其称为“触发器”。在您的 EditText 场景中,当触发器发生时,您将对 SQLiteDatabase 执行
insert()
或update()
或其他操作 包含通过getText()
检索的EditText
内容。在您的
RatingBar
场景中,当触发发生时,您将对SQLiteDatabase 执行
与通过insert()
或update()
或其他操作getRating()
检索的RatingBar
内容。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 (e.g.,
onStop()
), or whatever. I will call this the "trigger".In your
EditText
scenario, when the trigger occurs, you would do aninsert()
orupdate()
or whatever to theSQLiteDatabase
with the contents of theEditText
, retrieved viagetText()
.In your
RatingBar
scenario, when the trigger occurs, you would do aninsert()
orupdate()
or whatever to theSQLiteDatabase
with the contents of theRatingBar
, retrieved viagetRating()
.