This is my first time posting. I have made a simple counting app. I can connect and add data to the database, but i just cannot figure out how to view the data in the COLUMN_COUNT field. I have created a retrieveCount function, but cannot figure out how to call it on the HistoryAdapter page without making errors.
I have pasted the 4 files with code below.
MainActivity.kt
package com.example.thesupremecounter
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val addButton = findViewById<LinearLayout>(R.id.addButton)
val reduceButton = findViewById<LinearLayout>(R.id.reduceButton)
val resetButton = findViewById<LinearLayout>(R.id.resetButton)
val myTextView = findViewById<TextView>(R.id.textView)
val saveButton = findViewById<Button>(R.id.saveButton)
val historyButton = findViewById<Button>(R.id.historyButton)
var timeClicked = 0
addButton.setOnClickListener {
timeClicked += 1
myTextView.text = timeClicked.toString()
// Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
reduceButton.setOnClickListener {
timeClicked -= 1
if (timeClicked < 0) {
timeClicked = 0
} else {
myTextView.text = timeClicked.toString()
// Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
}
resetButton.setOnClickListener {
timeClicked = 0
myTextView.text = timeClicked.toString()
// Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
}
historyButton.setOnClickListener {
val intent = Intent(this, HistoryActivity::class.java)
startActivity(intent)
}
saveButton.setOnClickListener {
val count = timeClicked.toString()
val c = Calendar.getInstance() // Calender Current Instance
val dateTime = c.time // Current Date and Time of the system.
val sdf = SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.getDefault()) // Date Formatter
val date = sdf.format(dateTime) // dateTime is formatted in the given format.
val dbHandler = SqliteOpenHelper(this, null)
dbHandler.addDate(count, date) // Add date function is called.
Toast.makeText(this@MainActivity, count + date, Toast.LENGTH_SHORT).show()
}
}
}
HistoryActivity.kt
package com.example.thesupremecounter
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
class HistoryActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_history)
val toolbarPageTwo = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar_history_activity)
setSupportActionBar(toolbarPageTwo)
supportActionBar?.setDisplayHomeAsUpEnabled(true) //set back button
supportActionBar?.title = "HISTORY" // Setting an title in the action bar.
toolbarPageTwo.setNavigationOnClickListener {
onBackPressed()
}
getAllCompletedDates()
}
private fun getAllCompletedDates() {
val dbHandler = SqliteOpenHelper(this, null)
val allCompletedDatesList = dbHandler.getAllCompletedDatesList()
val tvHistory = findViewById<TextView>(R.id.tvHistory)
val rvHistory = findViewById<androidx.recyclerview.widget.RecyclerView>(R.id.rvHistory)
val tvNoDataAvailable = findViewById<TextView>(R.id.tvNoDataAvailable)
if (allCompletedDatesList.size > 0) {
tvHistory.visibility = View.VISIBLE
rvHistory.visibility = View.VISIBLE
tvNoDataAvailable.visibility = View.GONE
rvHistory.layoutManager = LinearLayoutManager(this)
val historyAdapter = HistoryAdapter(this, allCompletedDatesList)
rvHistory.adapter = historyAdapter
} else {
tvHistory.visibility = View.GONE
rvHistory.visibility = View.GONE
tvNoDataAvailable.visibility = View.VISIBLE
}
}
}
SqliteOpenHelper.kt
package com.example.thesupremecounter
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class SqliteOpenHelper(
context: Context,
factory: SQLiteDatabase.CursorFactory?
) :
SQLiteOpenHelper(
context, DATABASE_NAME,
factory, DATABASE_VERSION
) {
override fun onCreate(db: SQLiteDatabase) {
val CREATE_HISTORY_TABLE = ("CREATE TABLE " + TABLE_HISTORY +
"(" + COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_COUNT + " TEXT,"
+ COLUMN_COMPLETED_DATE + " TEXT" + ")")
db.execSQL(CREATE_HISTORY_TABLE)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY) // It drops the existing history table
onCreate(db) // Calls the onCreate function so all the updated table will be created.
}
fun addDate(count: String, date: String ) {
val values = "INSERT INTO history " +
"( 'count', 'completed_date')" +
" VALUES " +
"( '$count', '$date')"
val db = this.writableDatabase
db.execSQL(values)
db.close()
}
fun retrieveCount(): ArrayList<String> {
val countList = ArrayList<String>() // ArrayList is initialized
val db = this.readableDatabase
val cursor = db.rawQuery("SELECT * FROM $TABLE_HISTORY", null)
while (cursor.moveToNext())
{countList.add(cursor.getString(cursor.getColumnIndex(COLUMN_COUNT)))
}
cursor.close()
return countList
}
fun getAllCompletedDatesList(): ArrayList<String> {
val list = ArrayList<String>() // ArrayList is initialized
val db = this.readableDatabase
val cursor = db.rawQuery("SELECT * FROM $TABLE_HISTORY", null)
while (cursor.moveToNext())
{list.add(cursor.getString(cursor.getColumnIndex(COLUMN_COMPLETED_DATE)))
}
cursor.close()
return list
}
companion object {
const val DATABASE_VERSION = 1 // This DATABASE Version
const val DATABASE_NAME = "TheCountDatabase9.db" // Name of the DATABASE
const val TABLE_HISTORY = "history" // Table Name
const val COLUMN_ID = "_id" // Column Id
const val COLUMN_COUNT = "count" // Count
const val COLUMN_COMPLETED_DATE = "completed_date" // Column for Completed Date
}
}
HistoryAdapter.kt
package com.example.thesupremecounter
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_history_row.view.*
class HistoryAdapter(val context: Context, val items: ArrayList<String>) :
RecyclerView.Adapter<HistoryAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_history_row,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val date: String = items[position]
holder.tvPosition.text = (position + 1).toString()
holder.tvCount.text = "unsure what code to write here to display count values from COLUMN_COUNT + also not sure how to call the retrieve count function"
holder.tvItem.text = date
if (position % 2 == 0) {
holder.llHistoryItemMain.setBackgroundColor(
Color.parseColor("#EBEBEB")
)
} else {
holder.llHistoryItemMain.setBackgroundColor(
Color.parseColor("#FFFFFF")
)
}
}
override fun getItemCount(): Int {
return items.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val llHistoryItemMain = view.ll_history_item_main!!
val tvItem = view.tvItem!!
val tvPosition = view.tvPosition!!
val tvCount = view.tvCount!!
}
}
Any help will be greatly appreciated.
Thanks