0

First of all, I apologize for my poor English skills.

I'm a beginner in android studio. I'm implementing an app with search function added to recyclerview using searchview. However, despite using getFilter(), the search results are not displayed in the recyclerview. I don't know what my mistake is. It would be of great help if you could let me know what I'm missing.

My xml code

<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    app:umanoDragView="@id/slide_layout"
    app:umanoPanelHeight="16dp"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/first_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/inside_layout"
            android:layout_width="match_parent"
            android:layout_height="260dp"
            android:background="#8BC34A"
            app:layout_constraintBottom_toTopOf="@id/search_view"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/app_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="co-investor"
                android:textSize="30dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/str_total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="총 게시물 개수"
                android:textSize="40dp"
                app:layout_constraintBottom_toTopOf="@id/total"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/app_name" />

            <TextView
                android:id="@+id/total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="300개"
                android:textSize="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/str_total"

                />


        </androidx.constraintlayout.widget.ConstraintLayout>


        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@id/recyclerview"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/inside_layout" />


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="430dp"
            android:paddingBottom="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/search_view" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/slide_layout"
        android:layout_width="match_parent"
        android:layout_height="430dp"
        android:background="#ffffff">
    </androidx.fragment.app.FragmentContainerView>


</com.sothree.slidinguppanel.SlidingUpPanelLayout>

My MainActivity code

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val coinList = ArrayList<CoinList>()

        for (i in 0 until 50) {
            coinList.add(CoinList("a" + i, "count : " + i))
        }

        
        val adapter = RecyclerViewAdapter(coinList, LayoutInflater.from(this@MainActivity))
        val recycler_view: RecyclerView = findViewById(R.id.recyclerview)
        recycler_view.adapter = adapter
        recycler_view.layoutManager = LinearLayoutManager(this)       

    }

    class CoinList(val name: String, val count: String) {

    }


    //RecyclerViewAdapter
    class RecyclerViewAdapter(
        val itemList: ArrayList<CoinList>,
        val inflater: LayoutInflater
    ) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>(), Filterable {
        private var searchList: ArrayList<CoinList>? = null

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val name: TextView
            val count: TextView

            init {
                name = itemView.findViewById(R.id.coin_name)
                count = itemView.findViewById(R.id.coin_count)
            }
        }

        init {
            this.searchList = itemList
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = inflater.inflate(R.layout.item_view, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.name.setText(itemList.get(position).name)
            holder.count.setText(itemList.get(position).count)
            holder.itemView.setOnClickListener {
            }
        }
        
        override fun getItemCount(): Int {
            return itemList.size
        }

        //Filter
        override fun getFilter(): Filter {
            return object : Filter() {
                override fun performFiltering(charSequence: CharSequence): FilterResults {
                    val charString = charSequence.toString()
                    if (charString.isEmpty()) {
                        searchList = itemList
                    } else {
                        val filteredList = ArrayList<CoinList>()
                        for (coin in itemList) {
                            if (coin.name.contains(charString) || coin.count.contains(charString)) {
                                filteredList.add(coin)
                            }
                        }
                        searchList = filteredList
                    }
                    val filterResults = FilterResults()
                    filterResults.values = searchList
                    return filterResults
                }

                override fun publishResults(
                    charSequence: CharSequence,
                    filterResults: FilterResults
                ) {
                    searchList = filterResults.values as ArrayList<CoinList>
                    notifyDataSetChanged()
                }
            }
        }
    }


If anyone knows what I'm doing wrong, please let me know. Thank you!! :)

Ken Y-N
  • 13,857
  • 21
  • 69
  • 105
hoOHho
  • 3
  • 1

0 Answers0