I am building a function in my android app in which user will match two items together and if both items match to each other a correct icon will display to the item where u clicked to match,and if ur match was wrong then it show u the wrong icon. There is condition when the match is correct then the both correct items are removed i try various ways to removed but they are not seems perfect.
The matching are based on the languages its a langauge translation app suppose u try to translate the How are u(engilsh) ->to ->Comment vas-tu(french) in that case if those count values are match that mean they are belong to each other so its right
The value of count I provided from the activity where i adding values to the matchdataholder,By the loop count variable saved the ist lang (english text) at count 1 and 2nd language(french) to count 1 as well but with differnt index
Activity Class
package com.blablacards.activity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.blablacards.R
import com.blablacards.adapter.MatchAdapter
import com.blablacards.app.retrofit.ApiCall
import com.blablacards.model.FavListItem
import com.blablacards.model.MatchDataHolder
import com.blablacards.model.MatchList
import com.blablacards.model.ResponseFavList
import com.blablacards.utils.AppPref
import com.blablacards.utils.Constants
import kotlinx.android.synthetic.main.activity_match.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
class MatchActivity : BaseActivity(), View.OnClickListener {
var matchDataHolder: ArrayList<MatchDataHolder>? = ArrayList<MatchDataHolder>()
var favWordList = ArrayList<FavListItem>()
var matchList = ArrayList<MatchList>()
var categoryId: Int? = null
var categoryName: String? = null
lateinit var matchAdapter: MatchAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_match)
img_back.setOnClickListener(this)
getBundleData()
tv_header.text = categoryName
favWordList.clear()
calFavCategoryListApi("DESC")
// setAdapter()
}
private fun setAdapter(favWordList: ArrayList<MatchDataHolder>) {
Toast.makeText(this@MatchActivity, "Adapter called", Toast.LENGTH_SHORT).show()
matchAdapter = MatchAdapter(
this@MatchActivity,
favWordList
)
println("FAV LIST SIZE ->" + favWordList.size)
rv_match.layoutManager = GridLayoutManager(this@MatchActivity, 2)
rv_match.adapter = matchAdapter
}
private fun getBundleData() {
val extras = intent.extras
if (extras != null) {
categoryId = extras.getInt("CategoryId")
categoryName = extras.getString("CategoryName")
println("!!!categoryId = ${categoryId}")
}
}
private fun calFavCategoryListApi(sortBy: String) {
ApiCall.initApiCall(Constants.BASE_URL).getFavcategoryList(
"Bearer" + AppPref.getValue(AppPref.TOKEN, ""),
AppPref.getValue(AppPref.LOGIN_ID, "")!!, categoryId.toString(), "", "", sortBy
).enqueue(object : Callback<ResponseFavList> {
override fun onResponse(
call: Call<ResponseFavList>,
response: Response<ResponseFavList>
) {
if (response.isSuccessful) {
if (response.body()!!.status!!) {
favWordList.addAll(response.body()!!.data!!)
//
for (i in 0 until favWordList.size) {
matchDataHolder!!.add(MatchDataHolder(favWordList[i].sended_text.toString(),i))
matchDataHolder!!.add(MatchDataHolder(favWordList[i].return_text.toString(),i))
}
for(i in 0 until matchDataHolder!!.size){
println("DATA HOLDER ->"+ matchDataHolder!![i].text)
println("DATA HOLDER Count->"+ matchDataHolder!![i].count)
}
//
setAdapter(matchDataHolder!!)
} else {
showSnackBar(response.message())
}
}
}
override fun onFailure(call: Call<ResponseFavList>, t: Throwable) {
println("!!!t.message = ${t.message}")
}
})
}
override fun onClick(v: View?) {
when (v!!.id) {
R.id.img_back -> {
onBackPressed()
}
}
}
}
Adapter Class
package com.blablacards.adapter
import android.content.Context
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.blablacards.R
import com.blablacards.model.MatchDataHolder
import kotlinx.android.synthetic.main.row_match.view.*
class MatchAdapter(
var context: Context,
var favWordList: ArrayList<MatchDataHolder>,
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var index1 = -1
var index2 = -1
var enabledFlag = false
var oldPos = -1
var newPos = -1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ViewHolder(
LayoutInflater.from(context)
.inflate(R.layout.row_match, parent, false)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as MatchAdapter.ViewHolder).bind(position)
holder.itemView.rl_match.setBackgroundColor(context.resources.getColor(R.color.white))
holder.itemView.tv_match.visibility = View.VISIBLE
holder.itemView.rl_match.setOnClickListener(View.OnClickListener {
if (!enabledFlag) {
println(enabledFlag)
println("IF block")
it.rl_match.setBackgroundColor(context.resources.getColor(R.color.orange_light3))
holder.itemView.tv_match.visibility = View.INVISIBLE
oldPos = favWordList[position].count
println("OLD POS ->" + oldPos)
index1 = position
println("index 1 = " + index1)
enabledFlag = true
} else {
// show the correct and wrong //
println("ELSE BLOCK")
enabledFlag = false
newPos = favWordList[position].count
println("NEW POS ->" + newPos)
index2 = position
println("index 2 = " + index2)
showCorrectOrFalse(holder.itemView, favWordList[position].count)
}
})
}
override fun getItemCount(): Int {
return favWordList.size
}
private fun timeHandler(itemView: View, status: String, position: Int) {
Handler().postDelayed({
if (status == "right") {
println("inside right")
itemView.tv_match.visibility = View.VISIBLE
itemView.rl_match.setBackgroundColor(context.resources.getColor(R.color.white))
itemView.img_right_or_wrong.visibility = View.INVISIBLE
// favWordList.drop(index1)
// favWordList.drop(index2)
/// THE PROBLEM IS HERE CUZ I'm not able to perfectly removed the item//
favWordList.remove(MatchDataHolder(favWordList[index1].text,newPos))
favWordList.remove(MatchDataHolder(favWordList[index2].text,oldPos))
notifyItemRemoved(index1)
notifyItemRemoved(index2)
notifyItemRangeChanged(index1,favWordList.size)
notifyItemRangeChanged(index2,favWordList.size)
notifyDataSetChanged()
}
if (status == "wrong") {
println("Inside wrong")
itemView.tv_match.visibility = View.VISIBLE
itemView.rl_match.setBackgroundColor(context.resources.getColor(R.color.white))
itemView.img_right_or_wrong.visibility = View.INVISIBLE
notifyDataSetChanged()
}
}, 2000)
}
private fun showCorrectOrFalse(itemView: View, position: Int) {
println("SHOW CORRECT CALLED")
//call handler after click //
println("OLD POS " + oldPos + " New POS " + newPos)
if (oldPos == newPos) {
itemView.img_right_or_wrong.setImageDrawable(context.resources.getDrawable(R.drawable.right_game))
Toast.makeText(context, "true", Toast.LENGTH_SHORT).show()
timeHandler(itemView, "right", position)
} else {
itemView.img_right_or_wrong.setImageDrawable(context.resources.getDrawable(R.drawable.wrong_game))
timeHandler(itemView, "wrong", position)
}
}
internal inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(position: Int) {
itemView.findViewById<TextView>(R.id.tv_match).text =
favWordList[position].text
}
}
}
A model class for hold the data of item
class MatchDataHolder(text:String,num:Int) {
var count = num
var text =text
}