有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

用户在手机上创建的java存储数据。请看这篇文章

我的情况是下一个。我正在创建一个应用程序,当你可以做一个调查,然后,你可以看到调查答案,点击按钮,在应用程序中,而不去电话文件或类似的东西。我已经创建了大约55%的应用程序,但其他45%是最重要的。有多少可能性,或者我可以做些什么来实现这一点,当一个人点击应用程序内的按钮时,可以看到调查答案。我做了界面、按钮、文本视图等。。我创建了一个数据库,认为创建它,就有可能实现我想要的。我希望当你可以做一个调查,然后按下一个按钮,打开一个新的活动,并看到调查的答案时,你可以做一个系统。当您关闭应用程序并再次打开时,调查答案不会消失,它们仍然保存。谢谢

以下是我的调查代码,XML和JAVA:

Button sig = (Button) findViewById(R.id.env); // cargo el boton
    encuestadoSQLiteHelper encuestado = new encuestadoSQLiteHelper(this, "DBEncuestado", null, 1); // base de datos
    final SQLiteDatabase db = encuestado.getWritableDatabase();


    sig.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = getApplicationContext();
            EditText preg5 = (EditText) findViewById(R.id.editText2);
            EditText preg4 = (EditText) findViewById(R.id.editText);
            Spinner cur = (Spinner) findViewById(R.id.spinnerp);
            EditText nom = (EditText) findViewById(R.id.editText3);
            RadioGroup gruporadio = (RadioGroup) findViewById(R.id.Grupo1);
            RadioGroup gruporadio2 = (RadioGroup) findViewById(R.id.Grupo2);
            RadioGroup gruporadio3 = (RadioGroup) findViewById(R.id.Grupo3);
            String strNombre = nom.getText().toString();
            String nombre = "PabloGonzalez";
            String curso =  cur.getSelectedItem().toString();
            String strPregCinco = preg5.getText().toString();
            String strPregCuatro = preg4.getText().toString();

            if(gruporadio.getCheckedRadioButtonId() == -1 || gruporadio2.getCheckedRadioButtonId() == -1  || gruporadio3.getCheckedRadioButtonId() == -1 )
            {
                Toast.makeText(context,"¡No marcaste ninguna respuesta!",Toast.LENGTH_LONG).show();
            }

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

            else if(db == null)
            {
                Toast.makeText(context, "No funciona la base de datos.", Toast.LENGTH_LONG).show();
                db.close();
            }

            else
            {
                Intent i = new Intent (encuesta.this, res8.class);
                i.putExtra("nombre", strNombre);
                startActivity(i);
                Intent pas = new Intent(encuesta.this, MainActivity.class);
                Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
                startActivity(pas);
                db.execSQL("INSERT INTO Encuestado (nombre) " +
                        "VALUES ('" + nombre +"')");
                db.close();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            File sd = Environment.getExternalStorageDirectory();
                            File data = Environment.getDataDirectory();

                            if (sd.canWrite()) {
                                String currentDBPath = "/data/com.example.pablo.myapplication/databases/DBEncuestad‌​o";
                                String backupDBPath = "backdatabase.sqlite";
                                File currentDB = new File(data, currentDBPath);
                                File backupDB = new File(sd, backupDBPath);

                                if (currentDB.exists()) {
                                    FileChannel src = new FileInputStream(currentDB).getChannel();
                                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                                    dst.transferFrom(src, 0, src.size());
                                    src.close();
                                    dst.close();
                                }
                            }
                        } catch (Exception e) {
                            System.out.println("error in data base copy:"+e);
                        }
                    }
                }).start();
            }
        }
    });

}

}

如果你能看到,有一个数据库,但我不知道这是不是必要的或是

XML:

<ScrollView
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_alignParentTop="true"
    安卓:layout_alignParentLeft="true"
    安卓:layout_alignParentStart="true">
<LinearLayout
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:orientation="vertical" >

    <TextView
        安卓:text="Nombre del encuestado:"
        安卓:id="@+id/nomb"
        安卓:textSize="16dp"
        安卓:textColor="#000000"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <EditText
        安卓:id="@+id/editText3"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="2dp"
        安卓:layout_marginTop="2dp"
        安卓:ems="15"
        安卓:inputType="textPersonName"
        安卓:text="" />

    <TextView
        安卓:text="Curso:"
        安卓:textSize="16dp"
        安卓:textColor="#000000"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <Spinner
        安卓:id="@+id/spinnerp"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginBottom="5dp"
        安卓:layout_marginTop="5dp"/>

    <TextView
    安卓:text="1. ¿Cuánto tiempo tarda usted desde su casa al colegio?"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:textSize="16dp"
    安卓:textColor="#000000"
    安卓:id="@+id/textView" />



    <RadioGroup
        安卓:id="@+id/Grupo1"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" >
        <RadioButton
            安卓:id="@+id/radioButton"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="1/2 hora a 1 hora" />

        <RadioButton
            安卓:id="@+id/radioButton1"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="1 hora o 2 horas" />

        <RadioButton
            安卓:id="@+id/radioButton2"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="2 horas o más" />
    </RadioGroup>

<TextView
    安卓:text="2. ¿Cuánto tiempo tarda desde el colegio a su casa?"
    安卓:layout_marginTop="20dp"
    安卓:textSize="16dp"
    安卓:textColor="#000000"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content" />

    <RadioGroup
        安卓:id="@+id/Grupo2"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" >
        <RadioButton
            安卓:id="@+id/radioButton3"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="1/2 hora a 1 hora" />

        <RadioButton
            安卓:id="@+id/radioButton4"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="1 hora o 2 horas" />

        <RadioButton
            安卓:id="@+id/radioButton5"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="2 horas o más" />
    </RadioGroup>

<TextView
    安卓:text="3. ¿Cuáles de las siguientes zonas de Bogotá está localizado su domicilio?"
    安卓:layout_marginTop="20dp"
    安卓:textSize="16dp"
    安卓:textColor="#000000"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:id="@+id/textView2" />

    <RadioGroup
        安卓:id="@+id/Grupo3"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" >
        <RadioButton
        安卓:id="@+id/radioButton6"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="Norte de Bogotá (calle 72 en adelante)" />

        <RadioButton
            安卓:id="@+id/radioButton7"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Suba (noroccidente)" />

        <RadioButton
            安卓:id="@+id/radioButton8"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Centro - Chapinero - Teusaquillo" />

        <RadioButton
            安卓:id="@+id/radioButton9"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Alrededores de Bogotá" />

        <RadioButton
            安卓:id="@+id/radioButton10"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Sur de Bogotá" />

        <RadioButton
            安卓:id="@+id/radioButton11"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:text="Occidente (Puente Aranada - Fontibón - Engativá)" />
    </RadioGroup>


<TextView
    安卓:text="4. Si usted fuera alcalde de Bogotá, ¿que solución propondría para mejorar la movilidad de Bogotá?"
    安卓:textColor="#000000"
    安卓:textSize="16dp"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="20dp"
    安卓:id="@+id/textView3" />

    <EditText
        安卓:id="@+id/editText"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentStart="true"
        安卓:layout_below="@+id/textView3"
        安卓:layout_marginTop="13dp"
        安卓:background="#e4e4e4"
        安卓:ems="10"
        安卓:inputType="textPersonName"
        安卓:paddingBottom="8dp"
        安卓:paddingLeft="10dp"
        安卓:paddingTop="2dp"
        安卓:textSize="16dp" />

<TextView
    安卓:text="5. ¿Cómo cree que influye la movilidad en la calidad de vida de las personas?"
    安卓:textColor="#000000"
    安卓:textSize="16dp"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="20dp"
    安卓:id="@+id/textView4"
    安卓:layout_below="@+id/editText"
    安卓:layout_alignParentLeft="true"
    安卓:layout_alignParentStart="true" />

    <EditText
        安卓:id="@+id/editText2"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentStart="true"
        安卓:layout_below="@+id/textView3"
        安卓:layout_marginTop="20dp"
        安卓:background="#e4e4e4"
        安卓:ems="10"
        安卓:inputType="textPersonName"
        安卓:paddingBottom="8dp"
        安卓:paddingLeft="10dp"
        安卓:paddingTop="2dp"
        安卓:textSize="16dp" />

    <Button
        安卓:text="Enviar encuesta"
        安卓:textSize="14dp"
        安卓:id="@+id/env"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content" />

    </LinearLayout>
</ScrollView>

我想展示答案的活动是res8。爪哇


共 (1) 个答案

  1. # 1 楼答案

    兄弟,只需创建一个像getter和setter这样的模型类,它首先具有像getQuestion()、setQuestion()、getAnswer()、setAnswer()这样的方法。然后,当用户回答你的调查问题时,只需创建一个模型类的对象,比如:

    ArrayList<MyModel> datalist = new ArrayList();
        MyModel model = new MyModel();
        model.setQuestion(list.get(i).question);
        model.setAnswer(list.get(i).answer);
        ....
        ....
      dataList.add(model);
    

    这样只需捕获所有用户输入的数据,而不需要数据库

    下面是你所有问题的清单

    当你想获取数据时。。。打电话给数据列表。得到(我)。获取问题和答案