java - 如何将数据从 listview 单选按钮保存到 SQLite?
问题描述
我想创建一个用于考勤的应用程序。但我无法将来自 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;
}
我知道上述方法无法保存列表数据数组。但是我需要在单击存储按钮时保存列表中每个名称的所有单选按钮结果的功能。
解决方案
试试他的:
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");
}
}
);
.
.
.
推荐阅读
- python - 需要 Microsoft Visual C++ 14.0。下载python包时出错
- machine-learning - 学习率对神经网络的影响
- php - 试图从数据库中访问某些信息
- java - 如何从 mvnrepository 正确添加 Maven 依赖项
- javascript - Javascript:从两个具有相同标签名称的 XML 节点检索数据的可靠方法是什么?
- c# - 执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组
- scala - SBT 不会运行凿子教程...似乎找不到合适的 scala 版本
- python - 比较两个数据框并根据查找表删除列
- c# - 使用 Linq 在 xml 中添加项目索引
- android - 尝试从另一个活动中的代码更改一个活动中的背景颜色(Kotlin/Android Studio)