Como puedo abrir el fragment ItvList desde la Snackbar del activity AddItv?
import android.app.DatePickerDialog
import android.content.Intent
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.CalendarContract
import android.support.design.widget.Snackbar
import android.support.v4.app.Fragment
import android.util.Log
import android.view.View
import android.widget.*
import com.ramon.tonig.Global.AppDb
import com.ramon.ttguti.R
import kotlinx.android.synthetic.main.activity_create_fechas.*
import java.text.SimpleDateFormat
import java.util.*
class AddItv : AppCompatActivity() {
internal var rbSelected: String = ""
private var button_date: Button? = null
private var btSalvar: Button? = null
var fechaItv = Calendar.getInstance()
var fecha6 = Calendar.getInstance()
var fecha12 = Calendar.getInstance()
private var itv: Itv? = null
private var listItv: List<Itv>? = null
private var escribirBaseDatosTask: EscribirBaseDatosTask? = null
private var leerBaseDatosTask: LeerBaseDatosTask? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_create_fechas)
configView()
}
private fun configView() {
itv = Itv()
listItv = ArrayList()
listItv = AppDb.getAppDb(applicationContext).itvDAO().findAllItv()
leerBaseDatosTask = LeerBaseDatosTask()
leerBaseDatosTask!!.execute()
button_date = findViewById(R.id.btDate)
btSalvar = findViewById(R.id.btGuardar)
button_date!!.setOnClickListener {
DatePickerDialog(this@AddItv,
dateSetListener,
// set DatePickerDialog to point to today's date when it loads up
fechaItv.get(Calendar.YEAR),
fechaItv.get(Calendar.MONTH),
fechaItv.get(Calendar.DAY_OF_MONTH))
.show()
}
btSalvar!!.setOnClickListener {
//Toast.makeText(applicationContext, rbSelected, Toast.LENGTH_LONG).show() // print the value of selected super star
Snackbar.make(it, "\n" + "Tus datos se han guardado corectamente", Snackbar.LENGTH_LONG)
.setAction("Ok", View.OnClickListener {
fragmentTransaction(ItvList())
finish()
}).show()
val sdf = SimpleDateFormat("EEEE dd 'de 'MMMM 'del ' yyyy")
itv!!.fe_itv_ultima = sdf.format(fechaItv.getTime())
itv!!.notas = et_Notas.text.toString()
escribirBaseDatosTask = EscribirBaseDatosTask()
escribirBaseDatosTask!!.execute(itv)
}
}
private fun fragmentTransaction(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit()
}
val dateSetListener = object : DatePickerDialog.OnDateSetListener {
override fun onDateSet(view: DatePicker, year: Int, monthOfYear: Int,
dayOfMonth: Int) {
fechaItv.set(Calendar.YEAR, year)
fechaItv.set(Calendar.MONTH, monthOfYear)
fechaItv.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val sdf = SimpleDateFormat("EEEE dd 'de 'MMMM yyyy")
tvFechaUltima!!.text = sdf.format(fechaItv.getTime())
fecha6.set(Calendar.MONTH, monthOfYear+6)
fecha12.set(Calendar.YEAR, year+1)
updateDateInView()
}
}
private fun updateDateInView() {
//val myFormat = "EE dd 'de 'MMMM yyyy" // mention the format you need
//val sdf = SimpleDateFormat(myFormat, Locale.US)
val sdf = SimpleDateFormat("EEEE dd 'de 'MMMM yyyy")
//tvFecha!!.text = sdf.format(fechaItv.getTime())
if (rb_uno!!.isChecked() && tvFechaUltima.text != null) {
//rbSelected = rb_uno!!.text.toString()
tvFechaProxima!!.text = sdf.format(fecha6.getTime())
itv!!.fe_itv_proxima = sdf.format(fecha6.getTime())
val intent = Intent(Intent.ACTION_EDIT)
intent.setType("vnd.android.cursor.item/event")
intent.putExtra(CalendarContract.Events.TITLE, "Pasar ITV")
intent.putExtra(CalendarContract.Events.DESCRIPTION, "Debes pasar la I.T.V. de tu coche")
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, fecha6.getTimeInMillis())
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, fecha6.getTimeInMillis())
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
startActivity(intent)
} else if (rb_dos!!.isChecked()) {
//rbSelected = rb_dos!!.getText().toString()
tvFechaProxima!!.text = sdf.format(fecha12.getTime())
itv!!.fe_itv_proxima = sdf.format(fecha12.getTime())
val intent = Intent(Intent.ACTION_EDIT)
intent.setType("vnd.android.cursor.item/event")
intent.putExtra(CalendarContract.Events.TITLE, "Pasar ITV")
intent.putExtra(CalendarContract.Events.DESCRIPTION, "Debes pasar la I.T.V. de tu coche")
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, fecha12.getTimeInMillis())
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, fecha12.getTimeInMillis())
intent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
startActivity(intent)
}
}
private inner class EscribirBaseDatosTask : AsyncTask<Itv, Void, Void>() {
override fun doInBackground(vararg itvs: Itv): Void? {
AppDb.getAppDb(applicationContext).itvDAO().insertItv(itvs[0])
return null
}
}
private inner class LeerBaseDatosTask : AsyncTask<Void, Void, List<Itv>>() {
override fun doInBackground(vararg voids: Void): List<Itv>? {
listItv = AppDb.getAppDb(applicationContext).itvDAO().findAllItv()
return listItv
}
override fun onPostExecute(itvs: List<Itv>) {
showItv(itvs)
}
}
private fun showItv(itvs: List<Itv>) {
for (itv in itvs) {
Log.d("TAG", "ID: " + itv.id + " Nombre: " + itv.fe_itv_proxima + " Email: " + itv.seguro + "\n")
}
}
}
el ItvList Fragment
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.ramon.tonig.MiCocheBD.AddMiCocheAc">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/create_sqlite"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:typeface="sans"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="REVISION I.T.V"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="Selecciona la duracion de la I.T.V"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1">
<RadioButton
android:id="@+id/rb_uno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="true"
android:text="6 Meses"
android:textStyle="bold" />
<RadioButton
android:id="@+id/rb_dos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:checked="false"
android:text="12 Meses"
android:textStyle="bold" />
</RadioGroup>
<Button
android:id="@+id/btDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="@drawable/button"
android:text="@string/fecha"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/radioGroup" />
<TextView
android:id="@+id/tvFechaUltima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="text"
android:text="Fecha Ultima"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btDate" />
<TextView
android:id="@+id/tvFechaProxima"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="text"
android:text="Fecha Proxima"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvFechaUltima" />
<EditText
android:id="@+id/et_Notas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:hint="Notas"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvFechaProxima" />
<Button
android:id="@+id/btGuardar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/button"
android:text="@string/btn_gravar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_Notas" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout
Tranquila por la toolbarqu estw controlqada