英文:
Android - storing the values of rating, spinner value in local database
问题
以下是翻译好的内容:
我对Android编程还不熟悉,我正在尝试一个涉及数据库存储的小型编程练习。
我有一个简单的页面,其中有一个下拉选择元素(Spinner)和一个评分条(Rating Bar)。对于Spinner中的每个条目,我想选择一个评分,并且最终在点击上传按钮时,我希将所有条目存储在数据库中。这样,当再次打开应用程序时,存储的评分将被显示出来。
我已经设计了以下布局:
(布局截图的图片链接这里不可显示)
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="上传技能"
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);
}
}
有人可以告诉我如何继续选择评分并一次性将所有评分上传到数据库吗?
英文:
I am new to android programming and I am trying out a small programming exercise involving database storage.
I have a simple page where there is a spinner element, rating bar. For each entry in the spinner I would like to select a rating against that entry and in the end when upload button is clicked, I want all the entries to be stored in database. So that when the app is opened again, the stored ratings are shown.
I have come up with the following design for the layout
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>
and the corresponding activity.java file is as follows
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);
}
}
Could someone let me know how to proceed forward with choosing the ratings and uploading all the ratings at once to the database?
答案1
得分: 1
数据库在Android中是一个广泛的话题,无法在一篇文章中详尽介绍所有内容。我建议您使用Room数据库。请参阅此代码实验:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0
英文:
Databases are a huge topic in Android, and we can't exactly cover all that goes into it in a single post. I'd suggest you use a Room database. See this codelab:
https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论