How set adapter after I have get data for adapter - android

I am a newcomer in Kotlin and synchronous programming.
I have code:
open class MyFragment : DialogFragment() {
private var fragmentBinding: FragmentBinding? = null
private var resultList: List<MyObject> = ArrayList()
private var list = ArrayList<MyObjectItem>()
private lateinit var adapter: MynAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
resultList = getActivity.loadObjects()
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentBinding = FragmentBinding.inflate(inflater, container, false)
lifecycleScope.launch {
getListData()
initAdapter()
}
return fragmentBinding?.root
}
private fun initAdapter() {
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(requireContext())
fragmentBinding?.recyclerView?.layoutManager = layoutManager
adapter = MyAdapter(list, requireContext(), this, this)
fragmentBinding?.recyclerView?.adapter = adapter
}
private fun getListData() {
for (value in resultList) {
list.add(
MyObjectItem(
value.title!!,
value.numbert!!,
)
)
}
}
as result I get empty list in UI. But in debug I see, that from loadObjects() method I get not empty list. I understand , that it works in debug mode only because I stop execution of UI thread on my breakpoint and I should set up the view (namely calling initAdapter() on the UI thread, not in my worker thread. But I don't understand, how can I do this....

Here is the simple example of the adapter. You can use addAll() function of the adapter to add the data into the adapter.
public class SenListAdapter() :
RecyclerView.Adapter<SenListAdapter.MyViewHolder>() {
var data: MutableList<QuizResponse.Datum> = ArrayList()
var itemListener: EventListener? = null
var inflater: LayoutInflater? = null
private var onLoadMoreListener: OnLoadMoreListener? = null
var mData = ""
var context:Context?=null
constructor(context: Context, quizType: String) : this() {
this.quizType = quizType
this.context=context
}
override fun getItemViewType(position: Int): Int {
return position
}
fun addAll(mData: List<QuizResponse.Datum>?) {
data.clear();
data.addAll(mData!!)
notifyDataSetChanged()
}
fun clear() {
data.clear()
notifyDataSetChanged()
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): SenListAdapter.MyViewHolder {
inflater = LayoutInflater.from(parent.context)
val itemHomeSenBinding = DataBindingUtil.inflate<ItemHomeSenBinding>(
inflater!!,
R.layout.item_home_sen, parent, false
)
return MyViewHolder(itemHomeSenBinding)
}
override fun onBindViewHolder(holder: SenListAdapter.MyViewHolder, position: Int) {
populateItemRows(holder, position)
}
private fun populateItemRows(holder: SenListAdapter.MyViewHolder, position: Int) {
var item = data.get(position)
holder.itemHomeSenBinding.tvQuizName.text = item.name
holder.itemHomeSenBinding.tvCost.text = "₹ " + item.entryFee.toString()
holder.itemHomeSenBinding.tvEstimatedTime.text = item.time
}
override fun getItemCount(): Int {
return data.size
}
inner class MyViewHolder(var itemHomeSenBinding: ItemHomeSenBinding) :
RecyclerView.ViewHolder(
itemHomeSenBinding.root
) {
init {
setEventlistener(itemListener)
}
}
interface EventListener {
fun onClick(position: Int, item: QuizResponse.Datum?)
}
fun setEventlistener(onItemClick: EventListener?) {
itemListener = onItemClick
}
}
To set the adapter just use the object of recycler view :
var adapter=SenListAdapter()
adapter.addAll(<Your List>)
recyclerView.setAdapter(adapter)

resultList = getActivity.loadObjects() - I suppose this is your suspend (network) call. This shouldn't be called from Main thread, move this call in coroutine launcher, and when is done you can init your adapter from Main thread.
Try this:
open class MyFragment : DialogFragment() {
private var fragmentBinding: FragmentBinding? = null
private var list = ArrayList<MyObjectItem>()
private lateinit var adapter: MynAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentBinding = FragmentBinding.inflate(inflater, container, false)
viewLifecycleOwner.lifecycleScope.launch(Dispatchers.IO){
val res = getActivity.loadObjects()
withContext(Dispatchers.Main){
getListData(res)
initAdapter()
}
}
return fragmentBinding?.root
}
private fun initAdapter() {
val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(requireContext())
fragmentBinding?.recyclerView?.layoutManager = layoutManager
adapter = MyAdapter(list, requireContext(), this, this)
fragmentBinding?.recyclerView?.adapter = adapter
}
private fun getListData(res:List<MyObject>) {
for (value in res) {
list.add(
MyObjectItem(
value.title!!,
value.numbert!!,
)
)
}
}

Maybe your issue is different, but if you are going to load data asynchronous you need to use at least RecyclerView.Adapter#notifyDataSetChanged() (You can get better results using notifyItemChanged methods but those are more advanced).
Your code should look similar to:
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
fragmentBinding = FragmentBinding.inflate(inflater, container, false)
initAdapter()
lifecycleScope.launch {
adapter.list = getListData()
adapter.notifyDataSetChanged() // Notify changes in the adapter
}
return fragmentBinding?.root
}

Related

RecyclerView doesn't load data initially

I made an app from which I get data from TMDB API, everything seems to work but when I start the app, it displays only hint text, after scrolling the View get's updated with the data from the API
This is how it looks before scrolling
And this is how it looks after scrolling down a a bit
This is how I implemented it:
HomeFragment.kt
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
private lateinit var popularMovies: RecyclerView
private lateinit var popularMoviesAdapter: MoviesAdapter
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val homeViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
_binding = FragmentHomeBinding.inflate(inflater, container, false)
popularMovies = binding.popularMovies
popularMovies.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
popularMoviesAdapter = MoviesAdapter(listOf())
popularMovies.addItemDecoration(DividerItemDecoration(context,DividerItemDecoration.VERTICAL))
popularMovies.adapter = popularMoviesAdapter
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
MoviesRepository.getPopularMovies(page = 1,onSuccess = ::onPopularMoviesFetched,onError = ::onError)
}
private fun onPopularMoviesFetched(movies: List<Movie>) {
popularMoviesAdapter.updateMovies(movies)
}
private fun onError() {
Toast.makeText(context, getString(R.string.error_fetch_movies), Toast.LENGTH_SHORT).show()
}
MovieAdapter.kt
class MoviesAdapter(
private var movies: List<Movie>
) : RecyclerView.Adapter<MoviesAdapter.MovieViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
val view = LayoutInflater
.from(parent.context)
val binding = MovieItemBinding.inflate(view)
return MovieViewHolder(binding)
}
override fun getItemCount(): Int = movies.size
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
holder.bind(movies[position])
}
fun updateMovies(movies: List<Movie>) {
this.movies = movies
notifyDataSetChanged()
}
inner class MovieViewHolder(private val binding: MovieItemBinding) : RecyclerView.ViewHolder(binding.root) {
private val poster: ImageView = itemView.findViewById(R.id.item_movie_poster)
fun bind(movie: Movie) {
binding.movieTitle.text =movie.title
binding.movieReleaseDate.text = movie.releaseDate
binding.movieOverview.text = movie.overview
binding.movieReleaseDate.text = movie.releaseDate
Glide.with(itemView)
.load("https://image.tmdb.org/t/p/w342${movie.posterPath}")
.transform(CenterCrop())
.into(poster)
}
}
make adapter initialization on onViewCreated instead of onCreateView

ViewModel still displaying old data after dialog dismissed and parent fragment back to view

I'm still trying to wrap my head around working with viewmodels and a bit confused now that I also have a dialog and recyclerview items but will try to be as clear as I can here if I can get any help please.
I have a dialog with items that when one of them is selected and closed should return data to my calling fragment so the selected item is displayed under that view.
However, once the item is selected and the dialog dismissed, I don't see the new selected item as the one showing on the UI but still the old item instead. (When the fragment is first started it displays the item that is set as selected on my list. The selected value is hardcoded at first but updated when the item is clicked and I can see the update has taken place when I debug the viewmodel observer inside the onDismiss method for the dialog).
I'm a couple of hours on this and have tried a few different things such as calling the viewmodel inside onResume or onDismiss and changing the viewmodel to be initiated by by activityViewModels() as per this post but none of these have worked so far and I think I'm stuck at the moment. Below my most recent version of the code.
class CovidCheckInFragment : Fragment(R.layout.fragment_covid_check_in) {
var navController: NavController? = null
private val model: MainViewModel by activityViewModels()
#RequiresApi(Build.VERSION_CODES.M)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
model.userMutableLiveData.observe(viewLifecycleOwner, Observer<Any?> { list ->
if (list != null)
(list as Iterable<*>).map {
if ((it as ModelDialogOption).selected == true) {
tvHeader.text = it.title
}
}
})
}
}
..
class MyDialogFragment : DialogFragment(), RecyclerDialogOptionsItem.AdapterListener {
private val viewModel: MainViewModel by activityViewModels()
private lateinit var adapter: GroupAdapter<GroupieViewHolder>
var selectedPosition = -1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rvOptions.layoutManager = LinearLayoutManager(activity)
adapter = GroupAdapter()
rvOptions.adapter = adapter
ivClose.setOnClickListener {
this.dismiss()
}
initViewModel()
}
private fun initViewModel() {
viewModel.userMutableLiveData.observe(this, Observer { list ->
for (i in list!!) {
adapter.add(
RecyclerDialogOptionsItem(
this#MyDialogFragment,
i,
this#MyDialogFragment
)
)
}
})
}
override fun onClickItem(position: Int) {
selectedPosition = position
adapter.notifyDataSetChanged()
Log.i("clicked", "position: $position")
}
}
..
class MainViewModel : ViewModel() {
private var list: ArrayList<ModelDialogOption>? = null
val userMutableLiveData: MutableLiveData<ArrayList<ModelDialogOption>?> = MutableLiveData()
init {
populateList()
userMutableLiveData.value = list!!
}
private fun populateList() {
list = ArrayList()
list!!.add(ModelDialogOption("Prefer not to say", false))
list!!.add(ModelDialogOption("16-39", false))
list!!.add(ModelDialogOption("40-59", true))
list!!.add(ModelDialogOption("60+", false))
}
}
..
class RecyclerDialogOptionsItem(
private val fragment: MyDialogFragment,
private val modelDialogOption: ModelDialogOption,
private val adapterListener: AdapterListener
) : Item<GroupieViewHolder>() {
companion object {
var clickListener: AdapterListener? = null
}
override fun bind(viewHolder: GroupieViewHolder, position: Int) {
viewHolder.apply {
with(viewHolder.itemView) {
tvTitle.text = modelDialogOption.title
clickListener = adapterListener
if (fragment.selectedPosition == position) {
ivChecked.visible()
modelDialogOption.selected = true
} else {
ivChecked.invisible()
modelDialogOption.selected = false
}
itemView.setOnClickListener {
clickListener?.onClickItem(adapterPosition)
}
}
}
}
override fun getLayout() = R.layout.rv_options_item_row
interface AdapterListener {
fun onClickItem(position: Int)
}
}
Thank you very much.
Your main view model should be like this
class MainViewModel : ViewModel() {
private var list: ArrayList<ModelDialogOption>? = null
val userMutableLiveData = MutableLiveData<ArrayList<ModelDialogOption>>()
init {
populateList()
userMutableLiveData.value = list!!
}
private fun populateList() {
list = ArrayList()
list!!.add(ModelDialogOption("Prefer not to say", false))
list!!.add(ModelDialogOption("16-39", false))
list!!.add(ModelDialogOption("40-59", true))
list!!.add(ModelDialogOption("60+", false))
}
fun updateItem(position:Int){
val itemToUpdate = list!!.get(position)
itemToUpdate.selected = !itemToUpdate.selected!!
list!![position] = itemToUpdate
}
fun flushItems(){
userMutableLiveData.value = list!!
}
}
Then from MyDialogFragment Should be like this.
class MyDialogFragment : DialogFragment(), RecyclerDialogOptionsItem.AdapterListener {
private val viewModel: MainViewModel by activityViewModels()
private lateinit var adapter: GroupAdapter<GroupieViewHolder>
var selectedPosition = -1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rvOptions.layoutManager = LinearLayoutManager(activity)
adapter = GroupAdapter()
rvOptions.adapter = adapter
ivClose.setOnClickListener {
this.dismiss()
}
initViewModel()
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
viewModel.flushItems()
}
private fun initViewModel() {
viewModel.userMutableLiveData.observe(this, Observer { list ->
for (i in list!!) {
adapter.add(
RecyclerDialogOptionsItem(
this#MyDialogFragment,
i,
this#MyDialogFragment
)
)
}
})
}
override fun onClickItem(position: Int) {
selectedPosition = position
adapter.notifyDataSetChanged()
viewModel.updateItem(position)
Log.i("clicked", "position: $position")
}
}

Firebase Recyclerview(Vertical Scroll) always scroll to beginning on click of back button from another activity

Not sure why is this behaviour, but almost tried everything, please let me know in case you are aware of the fix for the issue
When I return from SellerAddNewProductActivity by clicking back button then RecyclerView always scroll to beginning
class SellerHomeFragment : Fragment() {
private var searchInputText:String = ""
private var sellerProductsViewListRecyclerView: RecyclerView? = null
private var layoutManager: RecyclerView.LayoutManager? = null
private var scroll_state: Parcelable? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_seller_home, container, false)
sellerProductsViewListRecyclerView = root.findViewById(R.id.seller_products_view_list)
layoutManager = LinearLayoutManager(this.context)
sellerProductsViewListRecyclerView?.layoutManager = layoutManager
val searchText: EditText = root.findViewById(R.id.seller_search_product_name)
val searchProductsBtn: Button = root.findViewById(R.id.seller_search_products_btn)
val sellerViewByProductState: Spinner = root.findViewById(R.id.seller_view_by_product_state)
sellerViewByProductState.setSelection(0)
searchProductsBtn.setOnClickListener {
searchInputText = searchText.text.toString()
createProductRecyclerView()
}
sellerViewByProductState?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
createProductRecyclerView()
}
}
return root
}
override fun onResume() {
super.onResume()
if(scroll_state!=null){
sellerProductsViewListRecyclerView!!.layoutManager!!.onRestoreInstanceState(scroll_state)
}
}
override fun onPause() {
super.onPause()
scroll_state = sellerProductsViewListRecyclerView!!.layoutManager!!.onSaveInstanceState()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
createProductRecyclerView()
}
private fun createProductRecyclerView() {
var productRef: DatabaseReference = FirebaseDatabase.getInstance().reference.child(
PRODUCTS_DB_NAME
)
var options: FirebaseRecyclerOptions<Products>? = null
val sellerViewByProductState: Spinner =
requireView().findViewById(R.id.seller_view_by_product_state)
val selectedProductState = sellerViewByProductState.selectedItem.toString()
options = FirebaseRecyclerOptions.Builder<Products>().setQuery(
productRef.child(selectedProductState).orderByChild(SID_PROPERTY)
.equalTo(FirebaseAuth.getInstance().currentUser!!.uid), Products::class.java
).setLifecycleOwner(this).build()
val adapter = object : FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
return ProductViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.product_items_layout, parent, false)
)
}
protected override fun onBindViewHolder(
holder: ProductViewHolder,
position: Int,
model: Products
) {
holder.txtProductName.text = model.pname
holder.txtProductDescription.text = model.description
holder.txtProductPrice.text = "Price = ₹ " + model.price.toString()
val context = holder.itemView.context
Picasso.with(this#SellerHomeFragment.context).load(model.image).networkPolicy(
NetworkPolicy.OFFLINE
).tag(context).into(holder.imageView,
object : Callback {
override fun onSuccess() {
}
override fun onError() {
Picasso.with(this#SellerHomeFragment.context).load(model.image)
.into(holder.imageView)
}
})
holder.itemView.setOnClickListener {
val intent: Intent = Intent(
this#SellerHomeFragment.context,
SellerAddNewProductActivity::class.java
)
intent.putExtra("category", model.category)
intent.putExtra("pid", model.pid)
val sellerViewByProductState: Spinner =
requireView().findViewById(R.id.seller_view_by_product_state)
val existingProductState = sellerViewByProductState.selectedItem.toString()
intent.putExtra("existingProductState", existingProductState)
startActivity(intent)
}
}
}
sellerProductsViewListRecyclerView?.setAdapter(adapter)
adapter.startListening()
}
}
I'm not sure I understand your code, but I think if you call function setAdapter(adapter) after added product you reload adapter.
When you add item to adapter use function notifyItemInserted() or notifyItemRangeInserted(position, range) on adapter.

Error in Updating RecyclerView in a Dialog

The idea of my project is to click in one recyclerView in MainActivity and after one dialog with other recyclerView is opened.
The error is that my recyclerView isn't updating in the Dialog
MainActivity:
private fun openDialog(postModel: PostModel){
CommentDialogFragment(postModel).show(supportFragmentManager, "dialog")
}
CommentDialogFragment:
class CommentDialogFragment(postSelected: PostModel): AppCompatDialogFragment() {
private var mCommentsList: MutableList<CommentModel> = arrayListOf()
private val mCommentServiceRequest = CommentServiceRequest()
private lateinit var mContext: Context
private lateinit var mRecyclerView: RecyclerView
private var postModel: PostModel = postSelected
private lateinit var mCommentListAdapter: CommentListAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.DialogStyle)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val viewGroup = inflater.inflate(R.layout.comment, container)
mContext = inflater.context
mCommentListAdapter = CommentListAdapter(mCommentsList)
mRecyclerView = viewGroup.findViewById(R.id.recyclerViewComments)
setupCommentObserver(postModel.id)
loadCommentRecyclerView()
dialog!!.setTitle("${mContext.getString(R.string.comments)} ${postModel.title}")
return viewGroup
}
private fun setupCommentObserver(postId: Int){
mCommentServiceRequest.searchCommentsFromAPI(postId)
.observe(this, Observer { comments ->
if (comments != null){
mCommentsList = comments.toMutableList()
mCommentListAdapter.setData(mCommentsList)
}
})
}
private fun loadCommentRecyclerView() {
mCommentsList.clear()
mRecyclerView.setHasFixedSize(true)
mRecyclerView.adapter = mCommentListAdapter
mRecyclerView.layoutManager = LinearLayoutManager(mContext)
}}
CommentListAdapter:
class CommentListAdapter(private var commentList: MutableList<CommentModel>):
RecyclerView.Adapter<CommentViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommentViewHolder {
val inflate = LayoutInflater.from(parent.context)
val view = inflate.inflate(R.layout.comment_list , parent, false)
return CommentViewHolder(view)
}
override fun getItemCount(): Int {
return commentList.count()
}
override fun onBindViewHolder(holder: CommentViewHolder, position: Int) {
holder.bindTask(commentList[position])
}
fun setData(list: MutableList<CommentModel>){
this.commentList = list
this.notifyDataSetChanged()
}}
I did the same thing in other project without use notifyDataSetChanged() only using callbacks and worked, so I tried here too but didn't work, I think the dialog is the problem

My RecyclerView is not showing the Anything

I am using MVVM with Room persistence and livedata. I am fetching the data from local database and want to show in the form of list in the recyclerView and my recyclerView is not showing anything.
My adapter is like any other regular adapter
RecyclerView Code
class MyInformationAdapter : RecyclerView.Adapter<MyInformationAdapter.ViewHolder>() {
private var myList: List<PersonInformation> = ArrayList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.my_adapter_data, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int = myList.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var myDataClass: PersonInformation = myList[position]
holder.name.text = myDataClass.name
holder.fName.text = myDataClass.fatherName
holder.email.text = myDataClass.email
holder.contact.text = myDataClass.contactNo.toString()
}
fun updateTheState(myList: List<PersonInformation>){
this.myList = myList
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var name: TextView = itemView.findViewById(R.id.yourName)
var fName: TextView = itemView.findViewById(R.id.yourFatherName)
var email: TextView = itemView.findViewById(R.id.yourEmail)
var contact: TextView = itemView.findViewById(R.id.yourContact)
}
}
RecyclerView Activity Code
class FinalActivity : AppCompatActivity() {
private var myDataList : List<PersonInformation> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_final)
setAdapter()
}
private fun setAdapter() {
val adapter = MyInformationAdapter()
val layoutManager = LinearLayoutManager(this)
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
adapter.updateTheState(myDataList)
}
}
*Fragment as a View of MVVM *
class PersonalInformationFragment : Fragment() {
private var viewModel: PersonInformationViewModel? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_personal_information, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
btn.setOnClickListener {
viewModel = ViewModelProviders.of(this)[PersonInformationViewModel::class.java]
viewModel?.getAllData()?.observe(this, Observer {
this.setAllData(it)
})
val intent = Intent(activity, FinalActivity::class.java)
startActivity(intent)
}
}
private fun setAllData(personInformation: List<PersonInformation>) {
val setData = PersonInformation(
name.text.toString(),
fName.text.toString(),
email.text.toString(),
contact.text.toString().toInt()
)
viewModel?.setTheData(setData)
}
}

Categories

Resources