Spiner into RecyclerView item - kotlin - kotlin

i have problem with adding simple Spiner to RecyclerView item. I saw a lot of examples but i cannot add it to my project. All i want is the same Spiner with list of Strings in every item of RecyclerView. I have no problem with implement this without RecycylerView, but with it, i dont know where to pass Adapter for Spiner.
RecyclerViewAdapter:
class MealPlanerAdapter(val mealList: MutableList<String>) : RecyclerView.Adapter<CustomViewHolder>() {
val mealCalories = mutableListOf<Int>()
override fun getItemCount(): Int {
return mealList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellForRow = layoutInflater.inflate(R.layout.meal_row, parent, false)
mealList.forEach {
mealCalories.add(100 / mealList.size)
}
return CustomViewHolder(cellForRow)
}
#SuppressLint("SetTextI18n", "CutPasteId", "ClickableViewAccessibility")
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
holder.view.findViewById<TextView>(R.id.meal_name_text_view).text = "Meal name"
holder.view.findViewById<TextView>(R.id.calosies_slider_name_text_view).text = "Daily calories: "
holder.view.findViewById<TextView>(R.id.calories_value_text_view).text = holder.view.findViewById<SeekBar>(
R.id.calories_seek_bar
).progress.toString()+"%"
holder.view.findViewById<TextView>(R.id.protein_value_text_view).text = holder.view.findViewById<SeekBar>(
R.id.protein_seek_bar
).progress.toString()+"%"
holder.view.findViewById<TextView>(R.id.fat_value_text_view).text = holder.view.findViewById<SeekBar>(
R.id.fat_seek_bar
).progress.toString()+"%"
holder.view.findViewById<TextView>(R.id.carbs_value_text_view).text = holder.view.findViewById<SeekBar>(
R.id.calories_seek_bar
).progress.toString()+"%"
holder.view.findViewById<SeekBar>(R.id.calories_seek_bar).progress = mealCalories[position]
var proteinValue: Int = 20
var fatValue: Int = 25
holder.view.findViewById<SeekBar>(R.id.calories_seek_bar).setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
holder.view.findViewById<TextView>(R.id.calories_value_text_view).text =
"$progress%"
println(position)
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
holder.view.findViewById<SeekBar>(R.id.protein_seek_bar).setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
holder.view.findViewById<TextView>(R.id.protein_value_text_view).text = "$progress%"
proteinValue = progress
holder.view.findViewById<SeekBar>(R.id.carbs_seek_bar).progress =
100 - proteinValue - fatValue
holder.view.findViewById<SeekBar>(R.id.carbs_seek_bar).refreshDrawableState()
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
holder.view.findViewById<SeekBar>(R.id.fat_seek_bar).setOnSeekBarChangeListener(object :
SeekBar.OnSeekBarChangeListener {
#SuppressLint("ResourceType")
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
holder.view.findViewById<TextView>(R.id.fat_value_text_view).text = "$progress%"
fatValue = progress
holder.view.findViewById<SeekBar>(R.id.carbs_seek_bar).progress =
100 - proteinValue - fatValue
holder.view.findViewById<View>(R.id.carbs_seek_bar).refreshDrawableState()
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
holder.view.findViewById<SeekBar>(R.id.carbs_seek_bar).setOnTouchListener { v, event -> true }
}
}
class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {
}
Fragment:
class MealPlanerFragment : Fragment(R.layout.fragment_meal_planer) {
private var param1: String? = null
private var param2: String? = null
private val mealList = mutableListOf<String>("1", "2", "3")
private lateinit var mAdapter: MealPlanerAdapter
private lateinit var mLinearLayoutManager: LinearLayoutManager
private lateinit var mRecyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
mAdapter = MealPlanerAdapter(mealList)
}
#SuppressLint("CutPasteId", "ResourceType")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val inflater = inflater.inflate(R.layout.fragment_meal_planer, container, false)
mLinearLayoutManager = LinearLayoutManager(activity)
mRecyclerView = inflater.findViewById(recycler_view_meal_planer)
mRecyclerView.layoutManager = mLinearLayoutManager
mRecyclerView.adapter = mAdapter
return inflater
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}

Related

How to remove fragment disappearance bug in viewPager on back in FragNav on kotlin

How to remove fragment disappearance bug in viewPager on back in FragNav on kotlin
`abstract class TabsFragment : BaseFragment() {
val fragment: ArrayList = arrayListOf()
inner class PagerAdapter(fragmentManager: FragmentManager) :
FragmentStatePagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
override fun destroyItem(viewGroup: ViewGroup, i: Int, obj: Any) {
Intrinsics.areEqual(viewGroup, "container")
Intrinsics.areEqual(obj, "object")
}
override fun getCount(): Int {
for(index in 0..this#TabsFragment.count()) {
fragment.add(index, BaseFragment())
}
return this#TabsFragment.count()
}
override fun getPageTitle(i: Int): CharSequence? {
return this#TabsFragment.titleTab(i)
}
override fun getItem(position: Int): BaseFragment {
if(!fragment.contains(this#TabsFragment.tabItems(position))) {
fragment.removeAt(position)
fragment.add(position, this#TabsFragment.tabItems(position))
return this#TabsFragment.tabItems(position)
} else {
return fragment[position]
}
}
}
abstract fun tabItems(i: Int): BaseFragment
abstract fun titleTab(i: Int): String?
abstract fun count(): Int
override fun onActivityResult(i: Int, i2: Int, intent: Intent?) {
super.onActivityResult(i, i2, intent)
if (i == 100 && i2 == -1 && intent != null) {
val stringArrayListExtra =
intent.getStringArrayListExtra("android.speech.extra.RESULTS")
val str = stringArrayListExtra!![0]
//EventBusKt.m30817a(OnSearch(str))
}
}
override fun onCreateView(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup?,
bundle: Bundle?
): View? {
val inflate: View =
layoutInflater.inflate(R.layout.fragment_home, viewGroup, false)
//EventBus.getDefault().register(this)
inflate.search_cardView.setOnClickListener {
mFragmentNavigation.pushFragment(SearchFragment.newInstance(mInt + 1))
}
inflate.settings.setOnClickListener {
mFragmentNavigation.pushFragment(MainFragment.newInstance(mInt + 1), right = true)
}
inflate.view_pager.currentItem = PreferenceManager.getDefaultSharedPreferences(requireContext()).getString("home_default_page", "1")?.toInt() ?: 1
val viewPager = inflate.findViewById<View>(R.id.view_pager) as ViewPager
viewPager.adapter = PagerAdapter(childFragmentManager)
//viewPager.offscreenPageLimit = count() - 1
//viewPager.offscreenPageLimit = 5
(inflate.findViewById<View>(R.id.tabs) as TabLayout).setupWithViewPager(viewPager)
return inflate
}
private var BadgeCount: Int? = null
override fun onSaveInstanceState(savedInstanceState: Bundle) {
super.onSaveInstanceState(savedInstanceState)
BadgeCount?.let { savedInstanceState.putInt("BadgeCount", it) }
}
}`

Recyclerview does not display on the screen in kotlin

So I tried to create multi views recyclerview in kotlin, but sadly it did not work.The recyclerview does not display on the screen. It worth to mention that I tried to set the orientation as vertical in my layout files, also added all the dependencies and things needed. Can anyone please help me with that?
My MainActivity.class
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
lateinit var adapter: NumbersAdapter
lateinit var recyclerView: RecyclerView
var list: List<Nums> = listOf(Nums(1,false))
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
updateAll()
}
fun updateAll(){
binding.recyclerview.apply {
val layoutManager = LinearLayoutManager(this#MainActivity)
adapter = NumbersAdapter(list)
adapter = adapter
}}}
My Adapter
class NumbersAdapter(
var list: List<Nums>,
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
class RedViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val name = itemView.findViewById<TextView>(R.id.red_number)
fun bindRed(number: Nums) {
name.text = number.nums.toString()
}
}
class OrangeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val name = itemView.findViewById<TextView>(R.id.orange_number)
fun bindOrange(number: Nums) {
name.text = number.nums.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (viewType == 0) {
val redvView =
LayoutInflater.from(parent.context).inflate(R.layout.red_item, parent, false)
return RedViewHolder(redvView)
}
else {
val orangeView =
LayoutInflater.from(parent.context).inflate(R.layout.orange_item, parent, false)
return OrangeViewHolder(orangeView)
}
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if(getItemViewType(position)==0){
(holder as RedViewHolder).bindRed(list[position])
}
else{
(holder as OrangeViewHolder).bindOrange(list[position])
}
}
override fun getItemViewType(position: Int): Int {
checkItem()
if(list[position].flag)
return 0
return 1
}
fun checkItem(){
for (i in list.indices) {
for (k in i + 1 until list.size) {
if (list[i].nums + list[k].nums == 0) {
list[i].flag = true
list[k].flag = true
}}}}}
in MainActivity.class change code in updateAll() function
fun updateAll(){
binding.recyclerView.apply {
adapter = NumbersAdapter(list)
layoutManager = LinearLayoutManager(this#MainActivity)
}
}

i want to add next page (every page is 10) to my recyclerview but it delet my last page

my recyclerview fragment:
this is the fragment that I set my recycler view , I want when touch the continue button (btn_continue) add the rest of the list, which is actually the next page, to my list when I click the button
But it deletes the previous list and displays the new list
and i use the kotlin coruotine, viewmodel and recyclerview
class AllUnpublishedAdsFragment : Fragment() {
lateinit var unpublishedAdsAdapter: UnpublishedAdsAdapter
var items = ArrayList<M_Ads>()
lateinit var loadMoreItems: ArrayList<M_Ads>
lateinit var scrollListener: RecyclerViewLoadMoreScroll
lateinit var mLayoutManager: RecyclerView.LayoutManager
val page = "1"
var newpage = page.toInt()
var navController: NavController? = null
lateinit var btn_continue: Button
private lateinit var viewModel: MainViewmodels
lateinit var token: String
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_all_unpublished_ads, container,
false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btn_continue = view.findViewById<Button>(R.id.btn_continueAds)
token = arguments?.getString(TOKEN_KEY).toString()
Log.i("TOKEN", token)
setupViewModel(token, page)
continueAds()
unpublishedAdsAdapter.itemClickListener = { item, position ->
val id = item.id
val bundle_id = Bundle()
bundle_id.putString(ID_KEY, id.toString())
navController = Navigation.findNavController(view)
//view.findNavController().navigate(R.id.action_UnpublishedAds_to_Detail,
bundle_id)
navController!!.navigate(
R.id.action_allUnpublishedAdsFragment_to_adsDetailFragment,
bundle_id
)
}
}
private fun setupViewModel(token: String, page: String) {
LoadingDialog.show(requireContext(), { ld ->
recy_unpublished_ads.layoutManager = LinearLayoutManager(activity)
val decoration = DividerItemDecoration(activity, DividerItemDecoration.VERTICAL)
recy_unpublished_ads.addItemDecoration(decoration)
viewModel = ViewModelProvider(this).get(MainViewmodels::class.java)
unpublishedAdsAdapter = UnpublishedAdsAdapter()
recy_unpublished_ads.adapter = unpublishedAdsAdapter
viewModel.adsListObserve().observe({ lifecycle }, { model ->
items.clear()
items.addAll(model.adsList!!)
unpublishedAdsAdapter.setUpdateData(items)
ld.dismiss()
btn_continue.visibility = View.VISIBLE
})
viewModel.getAdsList(token, page)
})
}
fun continueAds() {
var isTouched=false
btn_continue.setOnClickListener {
isTouched=true
newpage = newpage + 1
Log.i("CONTINUE", newpage.toString())
setupViewModel(token, newpage.toString())
unpublishedAdsAdapter.notifyDataSetChanged()
isTouched=false
}
}
RetrofitInstance class
class RetrofitInstance {
companion object{
fun getRetrofitInstance (): Retrofit{
// val interceptor = HttpLoggingInterceptor()
// interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
// val client: OkHttpClient = OkHttpClient.Builder().addInterceptor(interceptor).build()
val okHttpClient: OkHttpClient = OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build()
return Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
}
}
}
my adapter:
class UnpublishedAdsAdapter : RecyclerView.Adapter<UnpublishedAdsAdapter.MyViewHolder>() {
var items = ArrayList<M_Ads>()
var itemClickListener: ((item: M_Ads, position: Int) -> Unit)? = null
lateinit var mcontext: Context
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class LoadingViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
fun setUpdateData(items: ArrayList<M_Ads>) {
this.items = items
notifyDataSetChanged()
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val img_ads = view.findViewById<ImageView>(R.id.img_recy_ads)
val txt_title = view.findViewById<TextView>(R.id.txt_recy_ads_title)
val txt_accountType = view.findViewById<TextView>(R.id.txt_recy_ads_acctype)
fun bind(data: M_Ads) {
txt_title.setText(data.title)
txt_accountType.setText(data.accountType)
if (!data.image1.equals("")) {
Picasso.with(img_ads.context).load(data.image1).into(img_ads)
} else {
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
mcontext = parent.context
return if (viewType == Constant.VIEW_TYPE_ITEM) {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.row_recy_unpublished_ads, parent, false)
MyViewHolder(view)
} else {
val view =
LayoutInflater.from(mcontext).inflate(R.layout.progress_loading, parent, false)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
view.progressbar.indeterminateDrawable.colorFilter =
BlendModeColorFilter(Color.WHITE, BlendMode.SRC_ATOP)
} else {
view.progressbar.indeterminateDrawable.setColorFilter(
Color.WHITE,
PorterDuff.Mode.MULTIPLY
)
}
MyViewHolder(view)
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(items.get(position))
handleClick(holder)
}
fun handleClick(holder: UnpublishedAdsAdapter.MyViewHolder) {
holder.itemView.setOnClickListener {
val position = holder.adapterPosition
itemClickListener?.invoke(items.get(position), position)
}
}
override fun getItemCount(): Int {
return items.size
}
my Viewmodel:
class MainViewmodels:ViewModel() {
lateinit var getAds:MutableLiveData<Main_ModelAds>
init {
getAds= MutableLiveData<Main_ModelAds>()
}
fun adsListObserve():MutableLiveData<Main_ModelAds>{
return getAds
}
fun getAdsList(token:String,page:String){
viewModelScope.launch(Dispatchers.IO) {
try {
Log.i("ERRORRES",token + "")
val retrofitInstance=RetrofitInstance.getRetrofitInstance().create(RetrofitService::class.java)
val response=retrofitInstance.unPublishedAdsList(token,page)
getAds.postValue(response)
}catch (ex:Exception){
Log.i("ERRORRES", ex.message.toString() + "")
}
}
}
What you want here is to update the list that you are showing when the user clicks the button.
In this case you should use ListAdapter.
You can refer to this article for implementation:
https://developer.android.com/reference/androidx/recyclerview/widget/ListAdapter

How to call a method of a fragment from adapter class?

I have seen many questions like this but they are not the same as my situation and I couldn't use those solutions.
I have a fragment (OrdersByStatusFragment.kt) and the method I want to call from the adapter is getOrderStatusList()
class OrdersByStatusFragment : BaseFragment() {
private lateinit var binding: OrderStatusLayoutBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_orders_by_status, container, false)
}
override fun onResume() {
super.onResume()
getOrderStatusList()
}
fun getOrderStatusList() {
showProgressDialog(resources.getString(R.string.please_wait))
FirestoreClass().getOrderStatusList(this#OrdersByStatusFragment)
}
fun successOrderStatusList(orderStatusList: ArrayList<OrderStatus>) {
hideProgressDialog()
if (orderStatusList.size > 0) {
rv_order_by_status.visibility = View.VISIBLE
tv_no_orders_by_status_found.visibility = View.GONE
rv_order_by_status.layoutManager = LinearLayoutManager(activity)
rv_order_by_status.setHasFixedSize(true)
val orderStatusListAdapter =
OrderStatusListAdapter(requireActivity(), orderStatusList)
rv_order_by_status.adapter = orderStatusListAdapter
} else {
rv_order_by_status.visibility = View.GONE
tv_no_orders_by_status_found.visibility = View.VISIBLE
}
}
fun successNewOrderStatus() {
Toast.makeText(requireContext(), "Success", Toast.LENGTH_SHORT).show()
}
}
Adapter OrderStatusListAdapter.kt
open class OrderStatusListAdapter(
private val context: Context,
private var list: ArrayList<OrderStatus>,
) : RecyclerView.Adapter<OrderStatusListAdapter.MyViewHolder>() {
class MyViewHolder(var binding: OrderStatusLayoutBinding) :
RecyclerView.ViewHolder(binding.root)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
OrderStatusLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val model = list[position]
if (true) {
GlideLoader(context).loadProductPicture(
model.image,
holder.itemView.iv_order_status_item_image
)
val dateFormat = "dd MMM yyyy HH:mm"
val formatter = SimpleDateFormat(dateFormat, Locale.getDefault())
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = model.order_datetime
val orderDateTime = formatter.format(calendar.time)
holder.itemView.tv_order_status_order_date.text = orderDateTime
holder.itemView.tv_order_status_item_name.text = model.items[0].title
holder.itemView.tv_order_status_item_price.text = "$${model.total_amount}"
holder.itemView.tv_order_status.text = model.order_status
holder.itemView.tv_order_status_order_id.text = model.id
holder.binding.spnOrderChangeStatus.onItemSelectedListener =
object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
holder.binding.tvSpinnerValue.text =
parent?.getItemAtPosition(position).toString()
}
override fun onNothingSelected(parent: AdapterView<*>?) {
}
}
holder.binding.btnOrderStatusChangeStatus.setOnClickListener {
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show()
FirestoreClass().updateOrderStatus(
model.id,
holder.binding.tvSpinnerValue.text.toString()
)
Toast.makeText(context, "DB Updated", Toast.LENGTH_SHORT).show()
}
holder.itemView.ib_order_status_delete_product.visibility = View.GONE
holder.itemView.setOnClickListener {
val intent = Intent(context, SoldProductDetailsActivity::class.java)
intent.putExtra(Constants.EXTRA_SOLD_PRODUCT_DETAILS, model)
context.startActivity(intent)
}
}
}
}
As you can see the fun getOrderStatusList() in the OrdersByStatusFragment.kt is actually calling a method in the FirestoreClass.kt. Following is the getOrderStatusList() in the FirestoreClass.kt
fun getOrderStatusList(fragment: OrdersByStatusFragment) {
mFireStore.collection(Constants.ORDERS)
.whereIn("order_status", listOf("Pending", "In process"))
.get()
.addOnSuccessListener { document ->
val list: ArrayList<OrderStatus> = ArrayList()
for (i in document.documents) {
val orderStatus = i.toObject(OrderStatus::class.java)!!
orderStatus.id = i.id
list.add(orderStatus)
}
fragment.successOrderStatusList(list)
}
.addOnFailureListener {
fragment.hideProgressDialog()
}
}
The way I solved something like this is to pass a reference to the method into the adapter's constructor, then you can call the method in the adapter.
See kotlin-how-to-pass-a-function-as-parameter-to-another
In general, the link shows a class containing a method called buz.
Let's say your adapter has a constructor like:
class MyAdapter (private val context: Context, val buzz: () -> Unit) : RecyclerView.Adapter<RecyclerView.ViewHolder>()
then you can create the adapter like
MyAdapter(applicationContext, ::buz)
In the adapter the method can be called normally:
buzz()

Kotlin: Show AlertDialog when RecycleView item clicked

Now I'm studying to show AlertDialog when RecyclerView item clicked.
However, I don't know how to set the adapter for click listener.
Could you give me a tip?
Firstly, I tried to put the AlertDialog in this Main Activity.
Is this right position?
MainActivity
class MainActivity : AppCompatActivity() {
private val foodList = listOf(
FoodModel("Noodle", 2),
FoodModel("Cake", 3),
FoodModel("Pizza", 4),
FoodModel("Stake", 5),
FoodModel("Chicken", 4)
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val adapter = FoodDataAdapter(foodList)
adapter.notifyDataSetChanged()
foodListView.adapter = adapter
foodListView.layoutManager = LinearLayoutManager(this)
fun onItemClick(item: FoodModel, position: Int) {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Item deletion")
dialog.setMessage("Do you want to delete this item?")
dialog.setPositiveButton("Yes", DialogInterface.OnClickListener { _, _ ->
})
dialog.setNegativeButton("No", DialogInterface.OnClickListener { _, _ ->
})
dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener { _, _ ->
})
dialog.show()
}
}
}
This is the Adapter.
class FoodDataAdapter(val list: List<FoodModel>):RecyclerView.Adapter<FoodDataViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food,parent,false)
return FoodDataViewHolder(view)
}
override fun getItemCount(): Int {
return list.count()
}
override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) {
holder.containerView.nameText.text=list[position].name
holder.containerView.priceText.text="${list[position].price}dollar"
}
}
View holder
class FoodDataViewHolder(override val containerView: View) : RecyclerView.ViewHolder(containerView),LayoutContainer
I edited your code. MainActivity:
class MainActivity : AppCompatActivity(), FoodDataAdapter.OnItemClickListener {
var foodList = ArrayList<FoodModel>()
lateinit var adapter : FoodDataAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
foodlist.add(FoodModel("Noodle", 2))
foodlist.add(FoodModel("Cake", 3))
foodlist.add(FoodModel("Pizza", 4))
foodlist.add(FoodModel("Stake", 5))
foodlist.add(FoodModel("Chicken", 4))
adapter = FoodDataAdapter(foodList, this)
foodListView.adapter = adapter
foodListView.layoutManager = LinearLayoutManager(this)
}
override fun alertDialog(position: Int) {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Item deletion")
dialog.setMessage("Do you want to delete this item?")
dialog.setPositiveButton("Yes", DialogInterface.OnClickListener { _, _ ->
adapter.removeItem(position)
})
dialog.setNegativeButton("No", DialogInterface.OnClickListener { _, _ ->
})
dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener { _, _ ->
})
dialog.show()
}
}
FoodDataAdapter:
class FoodDataAdapter(var list: ArrayList<FoodModel>, var listener : FoodDataAdapter.OnItemClickListener):RecyclerView.Adapter<FoodDataAdapter.FoodDataViewHolder>(){
interface OnItemClickListener {
fun alertDialog(position : Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food,parent,false)
return FoodDataViewHolder(view)
}
override fun getItemCount(): Int {
return list.count()
}
fun removeItem(position: Int) {
list.removeAt(position)
notifyItemRemoved(position)
}
override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) {
holder.nameText.text=list[position].name
holder.priceText.text="${list[position].price}dollar"
}
inner class FoodDataViewHolder(containerView: View) : RecyclerView.ViewHolder(containerView),
View.OnClickListener {
var nameText: TextView = containerView.findViewById(R.id.nameText)
var priceText: TextView = containerView.findViewById(R.id.priceText)
init {
containerView.setOnClickListener(this)
}
override fun onClick(v: View?) {
listener.alertDialog(adapterPosition)
}
}
}
I just put a declare code for 'removeAt' at the end of the Adapter like this,
class FoodDataAdapter(val list: List<FoodModel>,var itemClicklistener : OnItemClickListener):RecyclerView.Adapter<FoodDataAdapter.FoodDataViewHolder>() {
interface OnItemClickListener {
fun onItemClick(position: Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FoodDataViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_food, parent, false)
return FoodDataViewHolder(view)
}
override fun getItemCount(): Int {
return list.count()
}
fun removeItem(position: Int) {
list.removeAt(position)
notifyItemRemoved(position)
}
override fun onBindViewHolder(holder: FoodDataViewHolder, position: Int) {
holder.nameText.text = list[position].name
holder.priceText.text = "${list[position].price} dollar"
}
inner class FoodDataViewHolder(containerView: View) : RecyclerView.ViewHolder(containerView),
View.OnClickListener {
var nameText: TextView = containerView.findViewById(R.id.nameText)
var priceText: TextView = containerView.findViewById(R.id.priceText)
init {
containerView.setOnClickListener(this)
}
override fun onClick(v: View?) {
itemClicklistener.onItemClick(adapterPosition)
}
}
}
private fun Any.removeAt(position: Int) {
}
and also changed the Main activity like this.
class MainActivity : AppCompatActivity(), FoodDataAdapter.OnItemClickListener {
private val foodList = arrayListOf(
FoodModel("Noodle", 3),
FoodModel("Cake", 5),
FoodModel("Pizza", 7),
FoodModel("Stake", 8),
FoodModel("Chicken", 8)
)
var adapter: FoodDataAdapter? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = FoodDataAdapter(foodList, this)
foodListView.adapter = adapter
foodListView.layoutManager = LinearLayoutManager(this)
}
override fun onItemClick(position: Int) {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Item deletion")
dialog.setMessage("Do you want to delete this item?")
dialog.setPositiveButton("Yes", DialogInterface.OnClickListener { _, _ ->
adapter?.removeItem(position)
})
dialog.setNegativeButton("No", DialogInterface.OnClickListener { _, _ ->
})
dialog.setNeutralButton("Cancel", DialogInterface.OnClickListener { _, _ ->
})
dialog.show()
}
}
And it works! but...one problem is..
after deletion of the list, the last list keep comes up.
the total number of list was not decreased..
lol
another 'Chicken' comes up..do you know how to fix it?