Home > Back-end >  Can I press a button in Activity A to filter a Recyclerview in Activity B?
Can I press a button in Activity A to filter a Recyclerview in Activity B?

Time:05-23

What I want to reach is that the same RecyclerView shows different data depending on which button the App user pressed before in the MainActivity.kt.

In my MainActivity.kt I have two buttons, which both send the user to the same RecyclerView Activity (RecyclerViewLayout.kt) via Intent.

Example: The RecycerView contains a picture of an apple and a banana. By pressing button A in MainActivity.kt, the RecyclerView in RecyclerViewLayout.kt should only show the apple. By pressing button B it should only show the banana. In my real app there are no fruits... but tutorials, which should be filtered like described.

I gently ask for help here how to do that. Maybe there is also a better way to reach my target to filter the RecyclerView?

Thanks in Advance!

MainActivity.kt

class MainActivity : AppCompatActivity() {
private var binding:ActivityMainBinding? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding?.root)


    val buttonRecyclerView = findViewById<Button>(R.id.btn_recyclerview)
    buttonRecyclerView.setOnClickListener {
        val intent = Intent(this, RecyclerViewLayout::class.java)
        startActivity(intent)
    }
}}

RecyclerViewLayout.kt

class RecyclerViewLayout : AppCompatActivity() {
private lateinit var newRecylerview : RecyclerView
private lateinit var newArrayList : ArrayList<RecyclerViewDataClass>
private lateinit var tempArrayList : ArrayList<RecyclerViewDataClass>
lateinit var imageId : Array<Int>
lateinit var tutorialHeading : Array<String>
lateinit var tutorialText : Array<String>
lateinit var url : Array<String>

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_recycler_view_layout)
    imageId = arrayOf(
        R.drawable.brake,
        R.drawable.brake,
        )

    tutorialHeading = arrayOf(
        getString(R.string.scheibenbremse_lüften_heading),
        getString(R.string.felgenbremse_richten_heading),
        

    )

    tutorialText = arrayOf(

        getString(R.string.scheibenbremse_lüften_text),
        getString(R.string.felgenbremse_richten_text),
    )

    url = arrayOf(

        getString(R.string.url_a),
        getString(R.string.url_b),
    )


    newRecylerview =findViewById(R.id.recyclerView)
    newRecylerview.layoutManager = LinearLayoutManager(this)
    newRecylerview.setHasFixedSize(true)


    newArrayList = arrayListOf<RecyclerViewDataClass>()
    tempArrayList = arrayListOf<RecyclerViewDataClass>()
    getUserdata()

}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

    menuInflater.inflate(R.menu.menu_item,menu)
    val item = menu?.findItem(R.id.search_action)
    val searchView = item?.actionView as SearchView
    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
        override fun onQueryTextSubmit(query: String?): Boolean {
            TODO("Not yet implemented")
        }

        override fun onQueryTextChange(newText: String?): Boolean {

            tempArrayList.clear()
            val searchText = newText!!.toLowerCase(Locale.getDefault())
            if (searchText.isNotEmpty()){

                newArrayList.forEach {

                    if (it.heading.toLowerCase(Locale.getDefault()).contains(searchText)){


                        tempArrayList.add(it)

                    }

                }

                newRecylerview.adapter!!.notifyDataSetChanged()

            }else{

                tempArrayList.clear()
                tempArrayList.addAll(newArrayList)
                newRecylerview.adapter!!.notifyDataSetChanged()

            }


            return false

        }


    })

    return super.onCreateOptionsMenu(menu)
}

private fun getUserdata() {

    for(i in imageId.indices){

        val news = RecyclerViewDataClass(imageId[i],tutorialHeading[i],url[i])
        newArrayList.add(news)

    }

    tempArrayList.addAll(newArrayList)


    val adapter = RecyclerViewAdapter(tempArrayList)

    newRecylerview.adapter = adapter
    adapter.setOnItemClickListener(object : RecyclerViewAdapter.onItemClickListener{
        override fun onItemClick(position: Int) {

            val intent = Intent(this@RecyclerViewLayout,TutorialsActivity::class.java)
            intent.putExtra("tutorialHeading",newArrayList[position].heading)
            intent.putExtra("imageId",newArrayList[position].titleImage)
            intent.putExtra("url",newArrayList[position].url)
            intent.putExtra("tutorialText",tutorialText[position])
            startActivity(intent)

        }


    })

}}

RecyclerViewAdapter.kt

class RecyclerViewAdapter(private val newsList : ArrayList<RecyclerViewDataClass>) : RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>(),
Filterable {

private lateinit var mListener : onItemClickListener

interface onItemClickListener{

    fun onItemClick(position : Int)

}

fun setOnItemClickListener(listener: onItemClickListener){

    mListener = listener

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_item,
        parent,false)

    return MyViewHolder(itemView,mListener)

}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    val currentItem = newsList[position]
    holder.titleImage.setImageResource(currentItem.titleImage)
    holder.tvHeading.text = currentItem.heading

}


override fun getItemCount(): Int {

    return newsList.size
}

class MyViewHolder(itemView : View, listener: onItemClickListener) : RecyclerView.ViewHolder(itemView){

    val titleImage : ShapeableImageView = itemView.findViewById(R.id.title_image)
    val tvHeading : TextView = itemView.findViewById(R.id.tvHeading)

    init {

        itemView.setOnClickListener {

            listener.onItemClick(adapterPosition)

        }


    }

}

override fun getFilter(): Filter {
    TODO("Not yet implemented")
}}

RecyclerViewDataClass.kt

data class RecyclerViewDataClass(var titleImage: Int, var heading: String, val url: String)

Tutorials Activity

class TutorialsActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_tutorials)

    val headingNews : TextView = findViewById(R.id.heading)
    val mainNews : TextView = findViewById(R.id.news)
    val imageNews : ImageView = findViewById(R.id.image_heading)

    val bundle : Bundle?= intent.extras
    val tutorialHeading = bundle!!.getString("tutorialHeading")
    val imageId = bundle.getInt("imageId")
    val tutorialText = bundle.getString("tutorialText")
    val url = bundle.getString("url")

    headingNews.text = tutorialHeading
    mainNews.text = tutorialText
    imageNews.setImageResource(imageId)

    imageNews.setOnClickListener {
        val openURL = Intent(Intent.ACTION_VIEW)
        openURL.data = Uri.parse(url.toString())
        startActivity(openURL)
    }


}}

CodePudding user response:

I believe you can pass data about which button is clicked using intents! Here's a link about that: How to Pass custom object via intent in kotlin

For example, you can pass "A" if button A is clicked and "B" if button B is clicked, and then get that string in RecyclerViewLayout.kt to determine which elements should be shown.

CodePudding user response:

According to me the simplest solution for doing this is you should have a boolean in preferences you can set preferences according to the button clicked and set data in your adapter to by getting the preferences value.

If you want to set data according to the button clicked

Other way is to pass the action onClick while starting a new Activity and getAction() in your second Activity.

This way you can also set data of your recyclerView by passing different data

  • Related