简体   繁体   English

Android - 在本地数据库中存储评级值、旋转器值

[英]Android - storing the values of rating, spinner value in local database

I am new to android programming and I am trying out a small programming exercise involving database storage.我是 android 编程的新手,我正在尝试一个涉及数据库存储的小编程练习。

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.对于微调器中的每个条目,我想要 select 对该条目的评级,最后当单击上传按钮时,我希望所有条目都存储在数据库中。 So that when the app is opened again, the stored ratings are shown.这样当再次打开该应用程序时,就会显示存储的评分。

I have come up with the following design for the layout我为布局提出了以下设计

d

ratingLayout.xml 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以及对应的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);
    }
}

Could someone let me know how to proceed forward with choosing the ratings and uploading all the ratings at once to the database?有人可以让我知道如何继续选择评级并将所有评级一次上传到数据库吗?

Databases are a huge topic in Android, and we can't exactly cover all that goes into it in a single post.数据库是 Android 中的一个大主题,我们无法在一篇文章中涵盖所有内容。 I'd suggest you use a Room database.我建议您使用 Room 数据库。 See this codelab:请参阅此代码实验室:

https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0 https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0

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

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