Closed Miteshmakwana73 closed 4 years ago
Hi, It seems to use a default adapter. The default adapter can be set only Charsequence
type items.
So If you want to use your custom model as a data list, create a new custom adapter.
can you provide a full custom adapter class so everyone understands better and proper or any example?
This is an example.
The customized adapter is not much different from the RecyclerView.Adapter
because a customized adapter must extend it.
The main difference is implementing the PowerSpinnerInterface
interface.
You can reference this example codes.
thank you it's working now this is my code of the custom adapter so anyone can take reference from it.
bind spinner
binding.spinnerCategory.apply {
setSpinnerAdapter(MySpinnerAdapter(this))
setItems(mList)
setOnSpinnerItemSelectedListener<CategoryListModel.CategoryListData> { _, item ->
binding.spinnerCategory.hint = item.categoryName
Toast.makeText(context, item.categoryName, Toast.LENGTH_SHORT).show()
}
lifecycleOwner = this@SearchFragment
preferenceName = getString(R.string.category)
}
adapter
inner class MySpinnerAdapter(
powerSpinnerView: PowerSpinnerView
) : RecyclerView.Adapter<MySpinnerAdapter.ViewHolder>(),
PowerSpinnerInterface<CategoryListModel.CategoryListData> {
override val spinnerView: PowerSpinnerView = powerSpinnerView
override var onSpinnerItemSelectedListener: OnSpinnerItemSelectedListener<CategoryListModel.CategoryListData>? =
null
private val spinnerItems: MutableList<CategoryListModel.CategoryListData> = arrayListOf()
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val model = spinnerItems[holder.adapterPosition]
holder.itemView.tvItem.setText(model.categoryName)
holder.itemView.setOnClickListener {
notifyItemSelected(position)
}
}
// we must call the spinnerView.notifyItemSelected method to let PowerSpinnerView know about changed information.
override fun notifyItemSelected(index: Int) {
this.spinnerView.notifyItemSelected(index, spinnerItems[index].categoryName)
this.onSpinnerItemSelectedListener?.onItemSelected(index, spinnerItems[index])
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
var mInflater: LayoutInflater = context!!.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val v =
mInflater.inflate(R.layout.raw_category_list, parent, false)
return ViewHolder(
v
)
}
override fun getItemCount(): Int {
return this.spinnerItems.size
}
override fun setItems(itemList: List<CategoryListModel.CategoryListData>) {
this.spinnerItems.clear()
this.spinnerItems.addAll(itemList)
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
@skydoves please correct me if anything is wrong
error
this what i done so far
xml
java
I am setting spinner when I get a response from API
this is my model class
please suggest me why I am getting an error