I want to know if there any getter options to get time,date etc associated with Date() built-in class in kotlin..? If yes then which are the options and how to use them..?
I have searched a lot but found nothing..I have provided my code below..
Inbox Activity
private fun refreshSmsInbox() {
try {
val smsList = ArrayList<SmsData>()
val cursor = contentResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null)
cursor?.let{
if(it!!.moveToFirst()){
val nameID = it.getColumnIndex("address")
val messageID = it.getColumnIndex("body")
//val dateID = it.getColumnIndex("date")
val timestamp = it.getColumnIndexOrThrow("date")
do{
val dateString = it.getString(timestamp)
val date : Date = Date(dateString.toLong())
val formatter = SimpleDateFormat("hh:mm a")
val displayTime = formatter .format(date)
val sms = SmsData(getContactName(this,it.getString(nameID!!.toInt()).toString()),it.getString(messageID),displayTime)
smsList.add(sms)
}while (it.moveToNext())
}
it.close()
}
val adapter = ListAdapter(this, smsList)
sms_list_view.adapter = adapter
} catch (ex: Exception) {
if (this != null) {
Toast.makeText(this, ex.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
List Adapter
class ListAdapter (val context: Context, val list : ArrayList<SmsData>): BaseAdapter(){
@SuppressLint("ViewHolder", "SimpleDateFormat")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view = LayoutInflater.from(context).inflate(R.layout.rowlayout,parent,false)
/*view.clickable.setOnClickListener {
val intent = Intent(this, MainActivity1::class.java)
startActivity(intent)
}*/
list[position].senderName?.let{
view.sender.text = it.substring(0,1).toUpperCase()
}
view.sms_sender.text = list[position].senderName
view.sms_message.text = list[position].message
view.sms_date.text = list[position].date.toString()
view.clickable.setOnClickListener { View ->
val intent = Intent(context,MainActivity1::class.java)
intent.putExtra("address",list[position].senderName)
context.startActivity(intent)
}
return view
}
Model class
data class SmsData(val senderName: String?, val message: String, val date: Date){
}
Expected:
Time 11:45 am/pm Actual:
Mon 12 jul 2019 GMT +05:00