I am new to Kotlin…I have no problem with the creation of a normal recyclerview with defined data, I just have a problem to inform my recycler with the data from back4App.
I don’t know if I built it correctly, but I got a “No value passed for parameter 'menuList” error. I don’t quite understand what is wrong. your help would be welcome.
my subclass
@ParseClassName(“MenuGeneral”)
open class PFmenuGeneral: ParseObject() {
var nom:String = "nom"
var menuGeneralList: String?
get()=getString("nom")
set(value) {
if(value!=null) {
put(nom,value)
}
}
fun getNom(block: (nom: List<PFmenuGeneral>?,exception: Exception?) -> Unit) {
val query=ParseQuery.getQuery(PFmenuGeneral::class.java)
query.orderByAscending("order")
query.include("pointerPageVente")
query.findInBackground { objects,error ->
if(error==null) {
for (nom in objects) {
Log.d("DEBUG", nom.getString ("nom"))
}
} else {
Log.d("item","Error: " + error!!.message)
}
block(objects, error)
}
}
}
My Adapter
class MenuGeneralAdapter(private val menuList: List<PFmenuGeneral> ):
RecyclerView.Adapter<MenuGeneralAdapter.MenuGeneralViewHolder>() {
fun onMenuGeneralClick(index:Int, context:Context){
val nomMenu: PFmenuGeneral = menuList[index]
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MenuGeneralViewHolder {
// 1 - chargement de la vue
val rootView = LayoutInflater.from(parent.context).inflate(R.layout.cell_menugeneral,parent,
false)
// 2 - création du view holder pour le contrôle de la vue
val holder = MenuGeneralViewHolder(rootView)
// 3 - retourner le view holder
return holder
}
override fun onBindViewHolder(holder: MenuGeneralViewHolder,position: Int) {
// 1 - obtenir la valeur(holder)
val menuGeneral = menuList[position]
// 2 - envoi des infos dans la cellule(position)
holder.fillWithPFmenuGeneral(menuGeneral = menuGeneral)
}
override fun getItemCount(): Int {
return menuList.size
}
inner class MenuGeneralViewHolder(rootView: View) : RecyclerView.ViewHolder(rootView),
View.OnClickListener {
private val ui_nom = rootView.ui_nom
init {
rootView.setOnClickListener(this)
}
fun fillWithPFmenuGeneral(menuGeneral: PFmenuGeneral){
ui_nom.text = menuGeneral.nom
}
override fun onClick(v: View?) {
if (v != null) {
onMenuGeneralClick(adapterPosition, v.context)
}
}
}
}
My activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation)
bottomNavigationView.selectedItemId = R.id.ic_home
bottomNavigationView.setOnNavigationItemSelectedListener(BottomNavigationView.OnNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.ic_home -> return@OnNavigationItemSelectedListener true
R.id.ic_convertisseur -> {
startActivity(Intent(applicationContext, ConvertisseurActivity::class.java))
overridePendingTransition(0, 0)
return@OnNavigationItemSelectedListener true
}
R.id.ic_recherche -> {
startActivity(Intent(applicationContext, SearchActivity::class.java))
overridePendingTransition(0, 0)
return@OnNavigationItemSelectedListener true
}
R.id.ic_favoris -> {
startActivity(Intent(applicationContext,FavoritesActivity::class.java))
overridePendingTransition(0, 0)
return@OnNavigationItemSelectedListener true
}
R.id.ic_plus -> {
startActivity(Intent(applicationContext, MoreActivity::class.java))
overridePendingTransition(0, 0)
return@OnNavigationItemSelectedListener true
}
}
false
})
ui_menugenerallist.adapter = MenuGeneralAdapter()
ui_menugenerallist.layoutManager = LinearLayoutManager(this)
}
fun openDecouverteButtonTouched(button: View){
val intent = Intent(this, DecouverteActivity::class.java)
startActivity(intent)
}
fun openSommaireButtonTouched(button: View){
val intent = Intent(this, SommaireActivity::class.java)
startActivity(intent)
}
}