I have a an Activity called ShoppingListActivity. The only thing it has in its layout is a fragment called ShoppingListFragment. That fragment get a listId as a parameter and should list all items within the list with id = listId in a RecyclerView and the name of that list. When the ShoppingListFragment gets called the name is displayed correctly but the recyclerview is never displayed.
The code for the ShoppingListActivity is:
class ShoppingListActivity : AppCompatActivity() {
private var _binding: ActivityShoppingListBinding? = null
private val binding get() = _binding!!
private lateinit var shoppingListViewModel: ShoppingListViewModel
private lateinit var shoppingListNameTextView: TextView
private lateinit var recyclerView: RecyclerView
private lateinit var shoppingList: ShoppingList
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_shopping_list)
var listId = intent.getIntExtra("ShoppingListId",0)
if(savedInstanceState == null)
{
supportFragmentManager.commit {
add(R.id.shopping_list_fragment, ShoppingListFragment(listId))
setReorderingAllowed(true)
}
}
}
}
The .xml file for the ShoppingListActivity:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_shopping_list"
tools:context=".Activities.ShoppingListActivity">
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shopping_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code for ShoppingListFragment:
class ShoppingListFragment(private val listId:Int) : Fragment() {
private var _binding: FragmentShoppingListBinding? = null
private val binding get() = _binding!!
private lateinit var shoppingListViewModel: ShoppingListViewModel
private lateinit var shoppingListNameTextView: TextView
private lateinit var recyclerView: RecyclerView
private lateinit var shoppingList: ShoppingList
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_shopping_list, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//DVATA VIEWMODELS, DA SE ZEMI LISTATA SO ITEMS I DA SE PUSTI NAPRED
_binding = FragmentShoppingListBinding.bind(view)
recyclerView = binding.shoppingListRecyclerView
var context = requireContext()
val shoppingListViewModelFactory = ShoppingListViewModelFactory(context)
shoppingListViewModel = ViewModelProvider(this,shoppingListViewModelFactory)[ShoppingListViewModel::class.java]
shoppingListNameTextView = binding.shoppingListName
shoppingList = shoppingListViewModel.getListById(listId)
shoppingListNameTextView.text = shoppingList.Name
var itemsInList = shoppingListViewModel.getItemsInShoppingList(shoppingList)
var adapter = SingleShoppingListAdapter(itemsInList)
recyclerView.adapter = adapter
}
}
The code for the .xml file for the ShoppingListFragment:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:id="@+id/shopping_list_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UI.Fragments.ShoppingListFragment">
<TextView
android:id="@+id/shopping_list_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="52dp"
android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="50dp"
android:id="@+id/shopping_list_recycler_view"
android:layout_height="150dp"
android:layout_marginTop="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shopping_list_name"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_item_to_shopping_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:layout_marginBottom="76dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_menu_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
The code for recycler_view_shopping_list_item_element.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="10dp"
android:layout_height="match_parent">
<TextView
android:id="@+id/shopping_list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="-16dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteY="1dp" />
<TextView
android:id="@+id/shopping_list_item_quantity"
android:layout_width="10dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/shopping_list_item_name"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
The code for the SingleShoppingListAdapter:
class SingleShoppingListAdapter(private val listItems: ArrayList<ShoppingListItem>) : RecyclerView.Adapter<SingleShoppingListAdapter.SingleShoppingListViewHolder>() {
inner class SingleShoppingListViewHolder(view: View) : RecyclerView.ViewHolder(view) {
private var name: TextView = view.findViewById(R.id.shopping_list_item_name)
private var quantity: TextView = view.findViewById(R.id.shopping_list_item_quantity)
fun bind(item: ShoppingListItem)
{
name.text = item.Name
quantity.text = item.Quantity.toString()
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): SingleShoppingListViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_shopping_list_item_element,parent,false)
return SingleShoppingListViewHolder(view)
}
override fun getItemCount(): Int {
return listItems.size
}
override fun onBindViewHolder(holder: SingleShoppingListViewHolder, position: Int) {
holder.bind(listItems[position])
}
}
When i tried debugging it the listItems passed to the adapter is never empty and the adapter gets created but the onCreateViewHolder, getItemCount, onBindViewHolder methods are never called.