Save data in an application entered by the user [closed]

2

There is some way for, for example, in a survey, the user to answer a series of questions, and then click on "Send survey", then click on "Surveys" and the information that you put in the previous survey appears . So on, but when the application is closed and reopened, the data is not lost. Is there any way to do it?

Currently this is the code I have:

Context context = getApplicationContext();
                int duration = Toast.LENGTH_SHORT;
                EditText nom = (EditText) findViewById(R.id.editText3);
                EditText preg5 = (EditText) findViewById(R.id.editText2);
                EditText preg4 = (EditText) findViewById(R.id.editText);
                RadioButton botonun = (RadioButton) findViewById(R.id.radioButton);
                RadioButton botondo = (RadioButton) findViewById(R.id.radioButton1);
                RadioButton botontr = (RadioButton) findViewById(R.id.radioButton2);
                RadioButton botoncu = (RadioButton) findViewById(R.id.radioButton3);
                RadioButton botonci = (RadioButton) findViewById(R.id.radioButton4);
                RadioButton botonse = (RadioButton) findViewById(R.id.radioButton5);
                RadioButton botonsi = (RadioButton) findViewById(R.id.radioButton6);
                RadioButton botonoc = (RadioButton) findViewById(R.id.radioButton7);
                RadioButton botonnu = (RadioButton) findViewById(R.id.radioButton8);
                RadioButton botondi = (RadioButton) findViewById(R.id.radioButton9);
                RadioButton botonon = (RadioButton) findViewById(R.id.radioButton10);
                RadioButton botondc = (RadioButton) findViewById(R.id.radioButton11);
                String strNombre = nom.getText().toString();
                String strPregCinco = preg5.getText().toString();
                String strPregCuatro = preg4.getText().toString();

                if(strNombre.matches("") || strPregCinco.matches("") || strPregCuatro.matches(""))
                {
                    Toast.makeText(context,"¡Dejaste campos vacíos!",Toast.LENGTH_LONG).show();
                }

                else if(botonun.isChecked() == false && botondo.isChecked() == false && botontr.isChecked() == false && botoncu.isChecked() == false && botonci.isChecked() == false
                        && botonse.isChecked() == false && botonsi.isChecked() == false && botonoc.isChecked() == false && botonnu.isChecked() == false && botondi.isChecked() == false
                        && botonon.isChecked() == false && botondc.isChecked() == false)
                {
                    Toast.makeText(context,"¡No marcaste algunas respuestas!",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Intent pas = new Intent(encuesta.this, MainActivity.class);
                    Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
                    startActivity(pas);
                }
            }

        });

The RadioButtons are multiple selection questions, and the EditText are open questions, so, in the mainactivity I have these buttons:

<Button
    android:text="Resultados profesores"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button0"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<Button
    android:text="@string/encuesta"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button4"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:elevation="0dp" />

<Button
    android:text="Resultados 11"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:layout_below="@+id/button0"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
android:text="Resultados 9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button3"
    android:layout_below="@+id/button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:text="Resultados 8"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:layout_below="@+id/button3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

The idea of this is a school project, an application where students from several courses on mobility are surveyed, and then click on "Results 8" and you will see all the results of the 8th grade students.

The question is: How can I implement the database system in my application so that it works in that way?

Thank you!

    
asked by Pablo Gonzalez 06.03.2017 в 03:04
source

2 answers

1

When conducting surveys regularly, we have a concentrator to exploit the information, obtaining and saving the data is done through a web service. I mention it by if you want to do this local you can save data in your application but it does not make any sense if this information is not concentrated somewhere outside it .

As an example: link

If you want to store data on the device, check the documentation.

Android: How to save files

    
answered by 06.03.2017 в 03:34
0

What you're looking for are the storage options on Android. The official documentation is already translated into Spanish.

If you have little data to store, such as String encuesta , you can use SharedPreferences . To load the saved results (if previous ones exist) for example in onCreate you use:

  SharedPreferences pref = getSharedPreferences();
  // el siguiente código te carga el valor guardado o "" si no hay nada guardado todavía
  String encuesta = pref.getString("encuesta", "");

and when you receive survey data:

  SharedPreferences.Editor editor = pref.edit();
  editor.putString("encuesta", encuesta);
  editor.commit();

For more complex data you can review the options for internal or external storage, or access an SQLite database (all documented in the link).

In the case of saving your data in a SQLite bbdd, you can implement a SQLiteOpenHelper and from there get the bbdd using getWritableDatabase() or getReadableDatabase() (for read only).

    
answered by 06.03.2017 в 03:25