简体   繁体   English

如何将列表视图单选按钮中的数据保存到 SQLite?

[英]How to save data from listview radiobutton to SQLite?

I want to create an app that used to take attendance.我想创建一个用于考勤的应用程序。 But I can't save the data that comes from the listview radio button to SQLite DB.但我无法将来自 listview 单选按钮的数据保存到 SQLite DB。 I already did the front side.我已经做了正面。 I need help at databasehelper.java class function to save on DB table.我需要 databasehelper.java class function 的帮助来保存数据库表。

I want to save all listview's radio button result when click save button.单击保存按钮时,我想保存所有列表视图的单选按钮结果。 I got the name list from SQLite using.java class extends BaseAdapter.我使用 SQLite 获得了名单。java class 扩展了 BaseAdapter。 finally I need to store attendance radio button result and date to TABLE_ATTENDANCE.最后我需要将出勤单选按钮结果和日期存储到 TABLE_ATTENDANCE。

i already got name list from sqlite DB.我已经从 sqlite DB 获得了名单。 then i want to save attendance radio button for each name on listview.然后我想为列表视图上的每个名称保存出勤单选按钮。 i done front part on activity_take_attendance.xml and take_att.xml files.我完成了 activity_take_attendance.xml 和 take_att.xml 文件的前面部分。

this is activity_take_attendance.xml `这是activity_take_attendance.xml`

<ListView
    android:id="@+id/attlv"
    android:layout_width="match_parent"
    android:layout_height="554dp">
</ListView>

<Button
    android:id="@+id/btnstore"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="Store" />`

and take_att.xml is和 take_att.xml 是

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="10dp"
        android:text="Name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="attendance"
        android:layout_marginLeft="20dp"
        android:id="@+id/attendance"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <RadioGroup
        android:id="@+id/radioAttendance"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginLeft="20dp"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:layout_below="@+id/attendance">

        <RadioButton
            android:id="@+id/present"
            android:layout_width="95dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Present"/>

        <RadioButton
            android:id="@+id/absent"
            android:layout_width="95dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Absent" />

        <RadioButton
            android:id="@+id/late"
            android:layout_width="95dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:baselineAligned="false"
            android:text="Late" />
    </RadioGroup>


</LinearLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/colorAccent"/>

this is Take_Attendance.java class code.这是 Take_Attendance.java class 代码。

btnStore.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {

            int selectedId = radioAttendance.getCheckedRadioButtonId();
            RadioButton radioButton = (RadioButton) findViewById(selectedId);
            String attendance = (String) radioButton.getText();

            databaseHelper.addAttendance(attendance, etdate.getText().toString());
            etname.setText("");
            etdate.setText("Date");
            radioAttendance.setOnCheckedChangeListener(null);

            //Toast.makeText(MainActivity.this,  attendance, Toast.LENGTH_SHORT).show();

            Toast.makeText(Take_Attendance.this, "Stored Successfullyyy!", Toast.LENGTH_SHORT).show();
        }
    });

and database helper.java class和数据库助手。java class

public long addAttendance(String date, String attendance) {
    SQLiteDatabase db = this.getWritableDatabase();
    // Creating content values
    ContentValues values = new ContentValues();
    values.put(KEY_DATE, date);
    values.put(KEY_ATTENDANCE, attendance);

    long insert = db.insert(TABLE_ATTENDANCE, null, values);

    return insert;
}

i know above methods can't save array of list data.我知道上述方法无法保存列表数据数组。 but i need function to save all radiobutton result for each name on list when click store button.但是当单击存储按钮时,我需要 function 来保存列表中每个名称的所有单选按钮结果。

try his:试试他的:

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup group, int checkedId)
           {
               radioButton = (RadioButton) findViewById(checkedId);
               String attendance = (String) radioButton.getText().toString();
              databaseHelper.addAttendance(attendance, etdate.getText().toString());
              etname.setText("");
              etdate.setText("Date");

           }
       }
       );
.
.
.

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

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