Проблема в том, что когда данные обновляются, представление не обновляется. Мои выводы -
1. onCreateview вызывается несколько раз, поэтому есть несколько связующих объектов
2. Я проверил список, обновленный список достигает адаптера
3. Данные, вставленные в базу данных, верны ,
4. Я установил все фрагменты кода, связанные с этим
5. Я использовал библиотеку sqaureup для календаря
package com.universal.best.habittracker.view
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import `androidx.databinding.DataBindingUtil`
import androidx.fragment.app.*
import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.universal.best.habittracker.R
import com.universal.best.habittracker.databinding.ActivityMainBinding
import com.universal.best.habittracker.repository.entity.ActivityEntityClass
import com.universal.best.habittracker.view.adapter.ActivityListAdapter
import com.universal.best.habittracker.view.adapter.ActivityStatusAdapter
import com.universal.best.habittracker.viewmodel.MainAcitivityViewModelProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.*
class MainActivity() : AppCompatActivity(),
AddActivityFinishInterface, ActivityAdapterEventrack {
lateinit private var mainActivityDataBinding: ActivityMainBinding
lateinit private var viewModel: MainAcitivityViewModelProvider;
val fragmentManager: FragmentManager = supportFragmentManager;
val tag:String="MainActivity"
companion object {
var newFragment: DialogFragment? = null
internal lateinit var context: Context
internal lateinit var mainActivityContext: Context
fun setContext(con: Context, mainActivityContextt: Context) {
context = con
mainActivityContext = mainActivityContextt
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mainActivityDataBinding = DataBindingUtil.setContentView(
this,
R.layout.activity_main
);
setContext(
applicationContext, this
);
viewModel = ViewModelProvider(this).get(MainAcitivityViewModelProvider::class.java)
var handleCLick =
HandleCLick(
this,
fragmentManager, viewModel,
this
);
mainActivityDataBinding.clickHandler = handleCLick;
Log.d(tag,"Adapter created--")
var listAdapter = ActivityListAdapter(this);
Log.d(tag,"Adapter created with Hashcode--"+listAdapter.hashCode())
mainActivityDataBinding.listAdapter = listAdapter;
mainActivityDataBinding.recyclerView.adapter= listAdapter
mainActivityDataBinding.recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL ,false)
var statusAdapter = ActivityStatusAdapter();
mainActivityDataBinding.statusAdaptor = statusAdapter
mainActivityDataBinding.bottomRecylerView.setHasFixedSize(false);
viewModel.retreiveFromDb()
.observe(this, androidx.lifecycle.Observer { list ->
list?.let {
listAdapter.setAdapterData(list)
listAdapter.notifyDataSetChanged()
statusAdapter.setAdapter(list)
statusAdapter.notifyDataSetChanged()
}
})
viewModel.getSaveres()?.observe(this, androidx.lifecycle.Observer { viewModel.retreiveFromDb()
.observe(this, androidx.lifecycle.Observer { list ->
list?.let {
listAdapter.setAdapterData(list)
listAdapter.ListChangedSoCallNotify()
statusAdapter.clear()
statusAdapter.setAdapter(list)
statusAdapter.notifyDataSetChanged()
}
})
})
}
class HandleCLick(
val context: Context,
val fragmentManager: FragmentManager, val viewmodel: MainAcitivityViewModelProvider,
val addActivityFinishInterface: AddActivityFinishInterface
) {
fun onClick(view: View) {
when (view.id) {
R.id.imageView -> {
newFragment =
AddActivityDialogueFragment(
addActivityFinishInterface
)
newFragment?.show(fragmentManager, "dialog")
};
else -> {
Toast.makeText(context, "Invalid Input", Toast.LENGTH_SHORT).show()
};
}
}
}
@Override
override fun clickHandler(activtyName: String, list: ArrayList<Date>?) {
Toast.makeText(this, activtyName, Toast.LENGTH_SHORT).show()
newFragment?.dismiss()
var len: Int = list?.size ?: 0
when {
len == 1 -> {
Toast.makeText(this, "Valid date range selected ", Toast.LENGTH_SHORT)
.show()
list?.let {
viewModel.saveintoDB(activtyName, list.get(0), list.get(len - 1), "0") }
}
else -> Toast.makeText(this, "Valid date range not selected ", Toast.LENGTH_SHORT)
.show()
}
}
@Override
override fun onDestroy() {
super.onDestroy()
viewModel.close();
}
override fun deleteTriggered(model: ActivityEntityClass) {
viewModel.deleteEntityFromDB(model)
}
override fun viewChartTriggered() {
}
override fun imageCharTriggered() {
}
Above is my mainActivity
myAdapterClass
package com.universal.best.habittracker.view.adapter
import android.app.AlertDialog
import android.content.DialogInterface
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.universal.best.habittracker.R
import com.universal.best.habittracker.databinding.TopActivitiesItemLayoutBinding
import com.universal.best.habittracker.repository.entity.ActivityEntityClass
import com.universal.best.habittracker.view.ActivityAdapterEventrack
import com.universal.best.habittracker.view.MainActivity
import com.universal.best.habittracker.view.MainActivity.Companion.context
import com.universal.best.habittracker.view.MainActivity.Companion.mainActivityContext
class ActivityListAdapter(var adaptercallBackInterface: ActivityAdapterEventrack) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var list: List<ActivityEntityClass>? = null
val tag: String = "ActivityListAdapter"
lateinit var myViewHolder: MyViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val inflater =LayoutInflater.from(parent.context)
val databinder = TopActivitiesItemLayoutBinding.inflate(inflater);
Log.d(tag, "onCreate dataBinder--" + databinder.hashCode());
myViewHolder = MyViewHolder(databinder);
Log.d(tag, "onCreate viewholder--" + myViewHolder.hashCode());
var Handler = MyClickHandler(adaptercallBackInterface)
databinder.myclickHanlder = Handler
return myViewHolder;
}
override fun getItemCount(): Int {
return list?.size ?: 0
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
myViewHolder.bindData(list, position);
}
override fun getItemViewType(position: Int): Int {
return position
}
fun setAdapterData(list: List<ActivityEntityClass>?) {
this.list = list
}
class MyViewHolder( databinder: TopActivitiesItemLayoutBinding) :
RecyclerView.ViewHolder(databinder.root) {
val databinderr: TopActivitiesItemLayoutBinding = databinder
fun bindData(
list: List<ActivityEntityClass>?,
i: Int
) {
databinderr.model = list?.get(i)
databinderr.executePendingBindings()
}
}
class MyClickHandler(var eventrack: ActivityAdapterEventrack) {
fun onclick(view: View, entityClass: ActivityEntityClass) {
when (view.id) {
R.id.imageView2 -> {
//open dialogbox and on yes delete it from db..
val dialogClickListener =
DialogInterface.OnClickListener { dialog, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
Toast.makeText(
MainActivity.mainActivityContext,
"Activity name is ${entityClass.activityName}",
Toast.LENGTH_SHORT
).show()
eventrack.deleteTriggered(entityClass)
}
DialogInterface.BUTTON_NEGATIVE -> {
}
}
}
var builder: AlertDialog.Builder = AlertDialog.Builder(mainActivityContext)
builder.setMessage("Are you sure to delete this activity?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show()
}
R.id.imageView4 -> {
//open new actiivty with intent
}
}
}
}
fun ListChangedSoCallNotify() {
notifyDataSetChanged()
}
}
mainActivty layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="clickHandler"
type="com.universal.best.habittracker.view.MainActivity.HandleCLick" />
<variable
name="listAdapter"
type="com.universal.best.habittracker.view.adapter.ActivityListAdapter" />
<variable
name="statusAdaptor"
type="com.universal.best.habittracker.view.adapter.ActivityStatusAdapter" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/homepage_bg"
tools:context=".view.Activity.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="48dp"
android:fontFamily="sans-serif-condensed"
android:text="Add New Habit to Track"
android:textColor="#FFFF"
android:textSize="@dimen/_18sdp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="@dimen/_50sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginTop="28dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/ic_add"
android:onClick="@{(view)->clickHandler.onClick(view)}"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.945"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.738" />
<TextView
android:id="@+id/notext"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:text="You are not tracking any habbit" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="72dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
tools:listitem="@layout/top_activities_item_layout" />
<TextView
android:id="@+id/textView3"
android:layout_width="149dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="40dp"
android:textColor="#FFFFFF"
android:textSize="@dimen/_18sdp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recyclerView"
android:text="Habbit Status" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/bottomRecylerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:adapter="@{statusAdaptor}"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="1.0"
tools:listitem="@layout/habit_staus_item" />
<TextView
android:textSize="@dimen/_14sdp"
android:textColor="#FFFF"
android:fontFamily="sans-serif-condensed"
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Other date"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.588"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
adapter layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="model"
type="com.universal.best.habittracker.repository.entity.ActivityEntityClass" />
<variable
name="myclickHanlder"
type="com.universal.best.habittracker.view.adapter.ActivityListAdapter.MyClickHandler" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/itemview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent">
<TextView
android:id="@+id/Yoga"
android:layout_width="123dp"
android:layout_height="174dp"
android:background="@drawable/activity_item_background_color"
android:fontFamily="monospace"
android:gravity="center"
android:text="@{model.activityName}"
android:textColor="#FFFFFF"
android:textSize="@dimen/_30sdp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Yoga" />
<ImageView
android:onClick="@{(view)->myclickHanlder.onclick(view,model)}"
android:id="@+id/imageView2"
android:layout_width="@dimen/_70sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginTop="12dp"
android:layout_marginEnd="144dp"
android:layout_marginRight="144dp"
app:layout_constraintEnd_toEndOf="@+id/Yoga"
app:layout_constraintHorizontal_bias="0.294"
app:layout_constraintStart_toStartOf="@+id/Yoga"
app:layout_constraintTop_toBottomOf="@+id/Yoga"
app:srcCompat="@drawable/deleete" />
<ImageView
android:onClick="@{(view)->myclickHanlder.onclick(view,model)}"
android:id="@+id/imageView4"
android:layout_width="@dimen/_45sdp"
android:layout_height="@dimen/_45sdp"
android:layout_marginTop="@dimen/_10sdp"
app:layout_constraintEnd_toEndOf="@+id/Yoga"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/Yoga"
app:layout_constraintTop_toBottomOf="@+id/Yoga"
app:srcCompat="@drawable/chart" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>