androidkotlinandroid-recyclerviewandroid-search

Trouble Using Edit Search Function in Recycler View with Cards


I am trying to search through a recycler view with cards by allowing a user to search. When the user searches, the cards should "reorganize" to show according to the characters entered by the user. I have tried to do this but am having issues doing this. Any assistance is appreciated.

MainActivity.kt

class MainActivity : AppCompatActivity(), BottomSheetRecyclerViewAdapter.ListTappedListener {
    private var customAdapter: CustomAdapter? = null
    private var arrayListModel = ArrayList<Model>()
    private lateinit var bottomSheetBehavior: CustomBottomSheetBehavior<ConstraintLayout>
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val modelList = readFromAsset()
        val adapterList = CustomAdapter(modelList, this)
        customAdapter = CustomAdapter(arrayListModel, this@MainActivity)

        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout) as CustomBottomSheetBehavior

        recyclerView.adapter = adapterList
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)

        et_search.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (!s.isNullOrEmpty()) {
                    val searchList = ArrayList<Model>()
                    for (i in arrayListModel.indices) {
                        if (arrayListModel[i].name.toLowerCase().contains(s)) {
                            searchList.add(arrayListModel[i])
                        }
                    }
                    try {
                        customAdapter?.notifyDataSetChanged()
                        recyclerView.adapter = CustomAdapter(searchList, this@MainActivity)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } else {
                    customAdapter?.notifyDataSetChanged()
                    recyclerView.adapter = customAdapter
                }
            }
        })
    }

    override fun onClickList(text: String) {
    }

    private fun readFromAsset(): List<Model> {

        val modeList = mutableListOf<Model>()
        val bufferReader = application.assets.open("android_version.json").bufferedReader()
        val json_string = bufferReader.use {
            it.readText()
        }

        val jsonArray = JSONArray(json_string);
        for (i in 0..jsonArray.length() - 1) {
            val jsonObject: JSONObject = jsonArray.getJSONObject(i)
            val model = Model(jsonObject.getString("name"), jsonObject.getString("version"))
            modeList.add(model)
        }
        return modeList
    }
}

Solution

  • I might found your problem. Here you getting data val modelList = readFromAsset() but you are never assigning data to arrayListModel that your problem.

    Assign the data to arrayListModel

      val modelList = readFromAsset()
      arrayListModel=modelList