Android – 将评分和下拉列表数值存储在本地数据库中

huangapple go评论76阅读模式
英文:

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

Android – 将评分和下拉列表数值存储在本地数据库中

ratingLayout.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/button&quot;
        android:layout_width=&quot;276dp&quot;
        android:layout_height=&quot;60dp&quot;
        android:layout_marginBottom=&quot;144dp&quot;
        android:text=&quot;Upload Skills&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.496&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot; /&gt;

    &lt;Spinner
        android:id=&quot;@+id/spinner&quot;
        android:layout_width=&quot;258dp&quot;
        android:layout_height=&quot;64dp&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/button&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintHorizontal_bias=&quot;0.452&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        app:layout_constraintVertical_bias=&quot;0.54&quot; /&gt;

    &lt;RatingBar
        android:id=&quot;@+id/ratingBar2&quot;
        android:layout_width=&quot;242dp&quot;
        android:layout_height=&quot;51dp&quot;
        app:layout_constraintBottom_toTopOf=&quot;@+id/button&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toBottomOf=&quot;@+id/spinner&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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&lt;String&gt; arrayList = new ArrayList&lt;&gt;(Arrays.asList(&quot;Java&quot;, &quot;C++&quot;, &quot;C&quot;, &quot;Python&quot;, &quot;Ruby&quot;, &quot;JavaScript&quot;));
        ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;&gt;(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

huangapple
  • 本文由 发表于 2020年9月2日 13:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63698991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定