[英]Android - storing the values of rating, spinner value in local database
我是 android 编程的新手,我正在尝试一个涉及数据库存储的小编程练习。
我有一个简单的页面,其中有一个微调元素,评级栏。 对于微调器中的每个条目,我想要 select 对该条目的评级,最后当单击上传按钮时,我希望所有条目都存储在数据库中。 这样当再次打开该应用程序时,就会显示存储的评分。
我为布局提出了以下设计
ratingLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="276dp"
android:layout_height="60dp"
android:layout_marginBottom="144dp"
android:text="Upload Skills"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent" />
<Spinner
android:id="@+id/spinner"
android:layout_width="258dp"
android:layout_height="64dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.452"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.54" />
<RatingBar
android:id="@+id/ratingBar2"
android:layout_width="242dp"
android:layout_height="51dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner" />
</androidx.constraintlayout.widget.ConstraintLayout>
以及对应的activity.java文件如下
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ratingLayout);
Spinner spinner = findViewById(R.id.spinner);
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("Java", "C++", "C", "Python", "Ruby", "JavaScript"));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, arrayList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(arrayAdapter);
}
}
有人可以让我知道如何继续选择评级并将所有评级一次上传到数据库吗?
数据库是 Android 中的一个大主题,我们无法在一篇文章中涵盖所有内容。 我建议您使用 Room 数据库。 请参阅此代码实验室:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.