Java 如何从列表视图中从sqlite检索数据单击并在不同的活动android studio上查看
我是android studio的新手,我正在尝试制作一个考勤手机应用程序,其中: -我可以添加学生并查看添加的学生列表 -添加事件并查看添加的事件列表 -在有微调器、自动完成文本视图和按钮的位置添加考勤。在微调器上,事件名称正在从SQLite数据库表中检索。在autocomplete文本视图中,学生的姓氏也从SQLite DB中的表中检索 我的问题是我不知道如何查看出席该活动的学生名单。我希望当我点击事件列表视图时,新的活动将打开并显示所有在场的学生 我的student表包含StudentID(主键,自动递增)、姓、名、年份。 事件表具有EventId(主键、自动增量)、事件名称、日期和时间。 考勤表:attendanceID(主键,自动递增),eventname,student last name 我非常感谢你们的帮助。这是我的密码 ViewEvents.javaJava 如何从列表视图中从sqlite检索数据单击并在不同的活动android studio上查看,java,android,database,sqlite,listview,Java,Android,Database,Sqlite,Listview,我是android studio的新手,我正在尝试制作一个考勤手机应用程序,其中: -我可以添加学生并查看添加的学生列表 -添加事件并查看添加的事件列表 -在有微调器、自动完成文本视图和按钮的位置添加考勤。在微调器上,事件名称正在从SQLite数据库表中检索。在autocomplete文本视图中,学生的姓氏也从SQLite DB中的表中检索 我的问题是我不知道如何查看出席该活动的学生名单。我希望当我点击事件列表视图时,新的活动将打开并显示所有在场的学生 我的student表包含StudentID
package com.example.acer.finals;
导入android.content.Intent;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.support.annotation.Nullable;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类ViewEvents扩展了AppCompatActivity{
数据库助手myDB;
私有列表视图mylistView;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u viewevents);
mylistView=(ListView)findViewById(R.id.listView3);
myDB=新数据库助手(此);
populateEventView();
}
私有void populateEventView()
{
ArrayList theList=新的ArrayList();
游标数据=myDB.getEventsList();
int numRows=data.getCount();
如果(numRows==0)
{
Toast.makeText(ViewEvents.this,“事件列表为空!”,Toast.LENGTH_LONG.show();
}
否则{
while(data.moveToNext()){
添加(“事件名称:”+data.getString(1)+“\n日期:”+data.getString(2)+“\n时间:”+data.getString(3));
}
}
ListAdapter ListAdapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,列表);
mylistView.setAdapter(listAdapter);
//将侦听器设置为ListView
mylistView.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
字符串ename=adapterView.getItemAtPosition(i).toString();
游标数据=myDB.getEventName(ename);
意向意向=新意向(ViewEvents.this,Load_attention.class);
星触觉(意向);
}
});
}
}
addAttention.java
package com.example.acer.finals;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.Button;
导入android.widget.Spinner;
导入android.widget.Toast;
导入java.util.ArrayList;
公共类AddAttention扩展AppCompative活动{
纺纱机;
按钮btnSave;
自动完成文本视图自动电视;
数据库助手myDB;
ArrayList name=新的ArrayList();
ArrayList all_Lname=新的ArrayList();
阵列适配器;
ArrayAdapter AllNames_适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u考勤);
spnEvent=(微调器)findViewById(R.id.spnEvent);
btnSave=(按钮)findviewbyd(R.id.btnSave);
autoCtv=(AutoCompleteTextView)findviewbyd(R.id.autoCtv);
//适配器
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,名称);
myDB=新数据库助手(此);
最终游标事件=myDB.getAllEvents();
while(event.moveToNext())
{
字符串名称=event.getString(1);
名称。添加(名称);
}
设置适配器(适配器);
//================================================
all_Lname=myDB.getAll_Lname();
AllNames\u adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,all\u Lname);
autoCtv.setAdapter(AllNames_adapter);
//================================================
btnSave.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串evname=SPnEvent.getSelectedItem().toString();
字符串lastname=autoCtv.getText().toString();
if(evname.length()!=0&&lastname.length()!=0){
createAttention(evname,lastname);
SPnEvent.setSelection(0);
autoCtv.setText(“”);
}否则{
Toast.makeText(addAttention.this,“必须在文本字段中放入内容!”,Toast.LENGTH\u LONG.show();
}
}
});
}
public void createAttention(字符串evname,字符串lastname){
布尔insertData=myDB.createAttendanceTB(evname,lastname);
if(insertData==true){
Toast.makeText(addAttention.this,“成功输入数据!”,Toast.LENGTH_LONG.show();
}否则{
Toast.makeText(addAttention.this,“出了点问题:(.”,Toast.LENGTH\u LONG.show())
package com.example.acer.finals;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class ViewEvents extends AppCompatActivity{
DatabaseHelper myDB;
private ListView mylistView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewevents);
mylistView = (ListView) findViewById(R.id.listView3);
myDB = new DatabaseHelper(this);
populateEventView();
}
private void populateEventView()
{
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getEventsList();
int numRows = data.getCount();
if(numRows == 0)
{
Toast.makeText(ViewEvents.this, "Event List Empty!", Toast.LENGTH_LONG).show();
}
else {
while (data.moveToNext()) {
theList.add("Event Name: " + data.getString(1) + "\nDate: " + data.getString(2) + "\nTime: " + data.getString(3));
}
}
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
mylistView.setAdapter(listAdapter);
//set an onItemClickListener to the ListView
mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String ename = adapterView.getItemAtPosition(i).toString();
Cursor data = myDB.getEventName(ename);
Intent intent = new Intent(ViewEvents.this, Load_Attendance.class);
startActivity(intent);
}
});
}
}
package com.example.acer.finals;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class AddAttendance extends AppCompatActivity {
Spinner spnrEvent;
Button btnSave;
AutoCompleteTextView autoCtv;
DatabaseHelper myDB;
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> all_Lname = new ArrayList<String>();
ArrayAdapter<String> adapter;
ArrayAdapter<String> AllNames_adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
spnrEvent = (Spinner) findViewById(R.id.spnrEvent);
btnSave = (Button) findViewById(R.id.btnSave);
autoCtv = (AutoCompleteTextView) findViewById(R.id.autoCtv);
//ADAPTER
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
myDB = new DatabaseHelper(this);
final Cursor event = myDB.getAllEvents();
while(event.moveToNext())
{
String name = event.getString(1);
names.add(name);
}
spnrEvent.setAdapter(adapter);
//================================================
all_Lname = myDB.getAll_Lname();
AllNames_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, all_Lname);
autoCtv.setAdapter(AllNames_adapter);
//================================================
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String evname = spnrEvent.getSelectedItem().toString();
String lastname = autoCtv.getText().toString();
if(evname.length() != 0 && lastname.length() != 0){
createAttendance(evname,lastname);
spnrEvent.setSelection(0);
autoCtv.setText("");
}else{
Toast.makeText(AddAttendance.this,"You must put something in the text field!",Toast.LENGTH_LONG).show();
}
}
});
}
public void createAttendance(String evname, String lastname){
boolean insertData = myDB.createAttendanceTB(evname,lastname);
if(insertData==true){
Toast.makeText(AddAttendance.this,"Successfully Entered Data!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(AddAttendance.this,"Something went wrong :(.",Toast.LENGTH_LONG).show();
}
}
}
package com.example.acer.finals;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Load_Attendance extends AppCompatActivity {
DatabaseHelper myDB;
private ListView attendance_list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loadattendance);
attendance_list = (ListView) findViewById(R.id.attendance_list);
myDB = new DatabaseHelper(this);
populateListViewAttendance();
}
private void populateListViewAttendance()
{
String name= "";
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getEventAttendance(name);
int numRows = data.getCount();
if(numRows == 0)
{
Toast.makeText(Load_Attendance.this, "Attendance List Empty!", Toast.LENGTH_LONG).show();
}
else {
while(data.moveToNext())
{
theList.add(data.getString(1));
}
}
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
attendance_list.setAdapter(listAdapter);
}
}
package com.example.acer.finals;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
/**
* Created by acer on 12/3/2017.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Finals";
// Table Names
private static final String StudentInfo = "StudentInfo";
private static final String EventTB= "EventTB";
private static final String AttendanceTB = "AttendanceTB";
// StudentInfo Table - column names
private static final String StudentID = "StudentID";
private static final String LastName = "LastName";
private static final String FirstName = "FirstName";
private static final String Year = "Year";
// TAGS Table - column names
private static final String EventID = "EventID";
private static final String EventName = "EventName";
private static final String EDate = "EDate";
private static final String Time = "Time";
// NOTE_TAGS Table - column names
private static final String AttendanceID = "AttendanceID";
private static final String Event_Name = "EventName";
private static final String Student_Lname = "Student_Lname";
public DatabaseHelper(Context context){
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
// Table Create Statements
// createStudentInfo table create statement
private static final String createStudentInfo = "CREATE TABLE " + StudentInfo + "("
+ StudentID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ LastName + " TEXT,"
+ FirstName + " TEXT,"
+ Year + " TEXT " + ")";
// createEventTB table create statement
private static final String createEventTB = "CREATE TABLE " + EventTB + "("
+ EventID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ EventName + " TEXT,"
+ EDate + " TEXT,"
+ Time + " TEXT " + ")";
// AttendanceTB table create statement
private static final String createAttendanceTB = "CREATE TABLE " + AttendanceTB + "("
+ AttendanceID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Event_Name + " TEXT,"
+ Student_Lname + " TEXT "+ ")";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createStudentInfo);
db.execSQL(createEventTB);
db.execSQL(createAttendanceTB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + createStudentInfo);
db.execSQL("DROP TABLE IF EXISTS " + createEventTB);
db.execSQL("DROP TABLE IF EXISTS " + createAttendanceTB);
// create new tables
onCreate(db);
}
public boolean createStudentInfo(String lname, String fname, String year)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LastName,lname);
contentValues.put(FirstName,fname);
contentValues.put(Year, year);
long result = db.insert(StudentInfo, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public boolean createEventTB(String ename, String edate, String time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(EventName,ename);
contentValues.put(EDate,edate);
contentValues.put(Time,time);
long result = db.insert(EventTB, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public boolean createAttendanceTB(String evname, String lastname)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Event_Name,evname);
contentValues.put(Student_Lname,lastname);
long result = db.insert(AttendanceTB, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
//returns all data from StudentInfo db
public Cursor getStudentsList() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + StudentInfo;
Cursor data = db.rawQuery(query, null);
return data;
}
//returns all events list
public Cursor getEventsList() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + EventTB;
Cursor data = db.rawQuery(query, null);
return data;
}
//Returns only the ID that matches the name passed in
public Cursor getEventName(String ename){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + AttendanceTB +
" WHERE " + EventName + " = '" + ename + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
//Returns only the ID that matches the name passed in
public Cursor getEventAttendance(String name){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * " + Student_Lname + " FROM " + AttendanceTB +
" WHERE " + EventName + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
//GET ALL Events
public Cursor getAllEvents()
{
SQLiteDatabase db = this.getWritableDatabase();
String[] columns={EventID,EventName};
return db.query(EventTB, columns, null, null, null, null, null);
}
//spinner
public ArrayList<String> getAll_Lname()
{
ArrayList<String> all_Lname = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT DISTINCT " + LastName + " FROM " + StudentInfo;
Cursor data = db.rawQuery(query, null);
if (data.getCount() > 0)
{
data.moveToFirst();
}
while(!data.isAfterLast())
{
all_Lname.add(data.getString(0));
data.moveToNext();
}
return all_Lname;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@+id/listView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="20dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Choose Event :"
android:id="@+id/textView15"
android:textSize="22sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spnrEvent"
android:layout_gravity="right"
android:clickable="false"
android:layout_below="@+id/textView15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:id="@+id/btnSave"
android:textSize="25sp"
android:layout_marginTop="45dp"
android:layout_below="@+id/autoCtv"
android:layout_alignLeft="@+id/autoCtv"
android:layout_alignStart="@+id/autoCtv"
android:layout_alignRight="@+id/autoCtv"
android:layout_alignEnd="@+id/autoCtv"
android:background="@color/material_deep_teal_500"
android:textColor="@color/primary_text_default_material_dark" />
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/autoCtv"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:visibility="visible"
android:layout_below="@+id/spnrEvent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Last Name"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/attendance_list" />
</LinearLayout>
Intent intent = new Intent(ViewEvents.this, Load_Attendance.class);
intent.putExtra("EVENT_NAME",ename); //<<<<<<<< ADD this Line
startActivity(intent);
public class Load_Attendance extends AppCompatActivity {
DatabaseHelper myDB;
private ListView attendance_list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loadattendance);
attendance_list = (ListView) findViewById(R.id.attendance_list);
myDB = new DatabaseHelper(this);
populateListViewAttendance(getIntent().getStringExtra("EVENT_NAME")); //<<<<<<<< invoke with retrieved event name
}
private void populateListViewAttendance(String name) //<<<<<<<< added parameter
{
ArrayList<String> theList = new ArrayList<>();
Cursor data = myDB.getEventAttendance(name);
int numRows = data.getCount();
if(numRows == 0)
{
Toast.makeText(Load_Attendance.this, "Attendance List Empty!", Toast.LENGTH_LONG).show();
}
else {
while(data.moveToNext())
{
theList.add(data.getString(1));
}
}
ListAdapter listAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, theList);
attendance_list.setAdapter(listAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="acer.com.finals.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<ListView
android:id="@+id/EventsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Finals";
// Table Names
private static final String StudentInfo = "StudentInfo";
private static final String EventTB= "EventTB";
private static final String AttendanceTB = "AttendanceTB";
// StudentInfo Table - column names
private static final String StudentID = "StudentID";
private static final String LastName = "LastName";
private static final String FirstName = "FirstName";
private static final String Year = "Year";
// TAGS Table - column names
private static final String EventID = "EventID";
private static final String EventName = "EventName";
private static final String EDate = "EDate";
private static final String Time = "Time";
// NOTE_TAGS Table - column names
private static final String AttendanceID = "AttendanceID";
private static final String Event_Name = "EventName";
private static final String Student_Lname = "Student_Lname";
public DatabaseHelper(Context context){
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
// Table Create Statements
// createStudentInfo table create statement
private static final String createStudentInfo = "CREATE TABLE " + StudentInfo + "("
+ StudentID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ LastName + " TEXT,"
+ FirstName + " TEXT,"
+ Year + " TEXT " + ")";
// createEventTB table create statement
private static final String createEventTB = "CREATE TABLE " + EventTB + "("
+ EventID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ EventName + " TEXT,"
+ EDate + " TEXT,"
+ Time + " TEXT " + ")";
// AttendanceTB table create statement
private static final String createAttendanceTB = "CREATE TABLE " + AttendanceTB + "("
+ AttendanceID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Event_Name + " TEXT,"
+ Student_Lname + " TEXT "+ ")";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createStudentInfo);
db.execSQL(createEventTB);
db.execSQL(createAttendanceTB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + StudentInfo);
db.execSQL("DROP TABLE IF EXISTS " + EventTB);
db.execSQL("DROP TABLE IF EXISTS " + AttendanceTB);
// create new tables
onCreate(db);
}
public boolean createStudentInfo(String lname, String fname, String year)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(LastName,lname);
contentValues.put(FirstName,fname);
contentValues.put(Year, year);
long result = db.insert(StudentInfo, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public boolean createEventTB(String ename, String edate, String time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(EventName,ename);
contentValues.put(EDate,edate);
contentValues.put(Time,time);
long result = db.insert(EventTB, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public boolean createAttendanceTB(String evname, String lastname)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Event_Name,evname);
contentValues.put(Student_Lname,lastname);
long result = db.insert(AttendanceTB, null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
//returns all data from StudentInfo db
public Cursor getStudentsList() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + StudentInfo;
Cursor data = db.rawQuery(query, null);
return data;
}
//returns all events list
public Cursor getEventsList() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + EventTB;
Cursor data = db.rawQuery(query, null);
return data;
}
//Returns only the ID that matches the name passed in
public Cursor getEventName(String ename){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + AttendanceTB +
" WHERE " + EventName + " = '" + ename + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
//Returns only the ID that matches the name passed in
public Cursor getEventAttendance(String name){
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT * FROM " + AttendanceTB +
" WHERE " + EventName + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
//GET ALL Events
public Cursor getAllEvents()
{
SQLiteDatabase db = this.getWritableDatabase();
String[] columns={EventID,EventName};
return db.query(EventTB, columns, null, null, null, null, null);
}
//spinner
public ArrayList<String> getAll_Lname()
{
ArrayList<String> all_Lname = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT DISTINCT " + LastName + " FROM " + StudentInfo;
Cursor data = db.rawQuery(query, null);
if (data.getCount() > 0)
{
data.moveToFirst();
}
while(!data.isAfterLast())
{
all_Lname.add(data.getString(0));
data.moveToNext();
}
return all_Lname;
}
}
DatabaseHelper dbhelper;
ListAdapter mEventListAdapter;
ArrayList<String> mEventList;
ListView mEventsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEventsListView = this.findViewById(R.id.EventsList);
dbhelper = new DatabaseHelper(this);
dbhelper.getWritableDatabase().delete("EventTB",null,null);
dbhelper.getWritableDatabase().delete("StudentInfo", null,null);
dbhelper.getWritableDatabase().delete("AttendanceTB", null,null);
dbhelper.createEventTB("The Main Event","31/12/2017","10:30");
dbhelper.createEventTB("The Normal Event","31/12/2017","10:30");
dbhelper.createEventTB("The Minor Event","31/12/2017","10:30");
dbhelper.createStudentInfo("Bloggs","Fred","2017");
dbhelper.createStudentInfo("Smith","Tom", "2016");
dbhelper.createStudentInfo("Thomas","John","2015");
dbhelper.createAttendanceTB("The Main Event","Bloggs");
dbhelper.createAttendanceTB("The Main Event", "Smith");
dbhelper.createAttendanceTB("The Normal Event","Thomas");
dbhelper.createAttendanceTB("The Minor Event","Thomas");
dbhelper.createAttendanceTB("The Minor Event","Smith");
Cursor csr = dbhelper.getEventsList();
mEventList = new ArrayList<>();
while (csr.moveToNext()) {
mEventList.add(csr.getString(1));
}
mEventListAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,mEventList);
mEventsListView.setAdapter(mEventListAdapter);
mEventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String eventname = parent.getItemAtPosition(position).toString();
Log.d("EVLIST_ITEMCLCK","You clicked Item at Position " + position + " and extracted Event name as " + eventname);
logEventAttendance(eventname);
}
});
}
private void logEventAttendance(String eventname) {
Cursor csr = dbhelper.getEventAttendance(eventname);
Log.d("EVATTEND","Number of attendance rows extracted is " + csr.getCount());
while (csr.moveToNext()) {
Log.d("EVATTEND", "Attendee is " + csr.getString(2) + " at Event " + csr.getString(1));
}
}
12-12 04:58:53.014 2860-2860/acer.com.finals D/EVLIST_ITEMCLCK: You clicked Item at Position 0 and extracted Event name as The Main Event
12-12 04:58:53.014 2860-2860/acer.com.finals D/EVATTEND: Number of attendance rows extracted is 2
12-12 04:58:53.014 2860-2860/acer.com.finals D/EVATTEND: Attendee is Bloggs at Event The Main Event
12-12 04:58:53.014 2860-2860/acer.com.finals D/EVATTEND: Attendee is Smith at Event The Main Event
12-12 04:58:54.192 2860-2860/acer.com.finals D/EVLIST_ITEMCLCK: You clicked Item at Position 1 and extracted Event name as The Normal Event
12-12 04:58:54.193 2860-2860/acer.com.finals D/EVATTEND: Number of attendance rows extracted is 1
12-12 04:58:54.193 2860-2860/acer.com.finals D/EVATTEND: Attendee is Thomas at Event The Normal Event
12-12 04:58:55.477 2860-2860/acer.com.finals D/EVLIST_ITEMCLCK: You clicked Item at Position 2 and extracted Event name as The Minor Event
12-12 04:58:55.478 2860-2860/acer.com.finals D/EVATTEND: Number of attendance rows extracted is 2
12-12 04:58:55.478 2860-2860/acer.com.finals D/EVATTEND: Attendee is Thomas at Event The Minor Event
12-12 04:58:55.478 2860-2860/acer.com.finals D/EVATTEND: Attendee is Smith at Event The Minor Event