【发布时间】:2020-01-21 05:28:43
【问题描述】:
我想创建一个用于考勤的应用。但我无法将来自 listview 单选按钮的数据保存到 SQLite DB。我已经做了正面。我需要 databasehelper.java 类函数的帮助以保存在 DB 表中。
我想在单击保存按钮时保存所有列表视图的单选按钮结果。我使用 .java 类扩展 BaseAdapter 从 SQLite 获得了名单。最后,我需要将出勤单选按钮结果和日期存储到 TABLE_ATTENDANCE。
我已经从 sqlite DB 获得了名单。然后我想为列表视图上的每个名称保存出勤单选按钮。我完成了 activity_take_attendance.xml 和 take_att.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" />`
而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"/>
这是 Take_Attendance.java 类代码。
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();
}
});
和数据库 helper.java 类
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;
}
我知道上述方法无法保存列表数据数组。但是我需要在单击存储按钮时保存列表中每个名称的所有单选按钮结果。
【问题讨论】:
标签: java android sqlite listview