1

I'm new android learner, I'm trying to make a RecyclerView contains of List of (Stories Title, and Stories images). When you click on an item in the RecyclerView it should open a new activity called ChildrenStoriesPreview contains of ScrollView which have ImageView to put the Story Image in it and TextView to put the Text of the Story in it. the problem is that I don't know how to set ocItemClickListener to know which item is clicked and how the new activity will contain information depending on this item? Could you please help me?

here is my Main.kt

class MainChildrenStories : AppCompatActivity() {

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

    var childrenStoriesArraylist = ArrayList<ChildrenStoriesRecyclerView>()
    childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story1", R.drawable.pic1))
    childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story2", R.drawable.pic2))
    childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story3", R.drawable.pic3))

    children_stories_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)

    val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist)
    children_stories_recyclerview.adapter = childrenStoriesAdapter

childrenStoriesAdapter.setOnItemClickListener(object : ChildrenStoriesAdapter.ClickListener {
        override fun onClick(pos: Int, aView: View) {

    //The App Crash here
            if (pos == 0){
                my_text_view.text = "Story number 1"
                my_imageview.setImageResource(R.drawable.pic1)
            }else if (pos == 1){
                my_text_view.text = "Story number 2"
                my_imageview.setImageResource(R.drawable.pic2)
            }
            val intent = Intent(this@MainChildrenStories, ChildrenStoryPreview::class.java)
            startActivity(intent)
        }
    })

   }
}

MyRecyclerView Class

data class ChildrenStoriesRecyclerView(var mStoryName: String, var mStoryImage: Int)

My RecyclerView Adapter Class

class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>) :
RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {

lateinit var mClickListener: ClickListener

fun setOnItemClickListener(aClickListener: ClickListener) {
    mClickListener = aClickListener
}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
    val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
    return ViewHolder(v)
}

override fun getItemCount(): Int {
    return myArrayList.size
}

override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
    var infList = myArrayList[p1]
    p0.storyName.text = infList.mStoryName
    p0.storyImage.setImageResource(infList.mStoryImage)
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
    override fun onClick(v: View) {
        mClickListener.onClick(adapterPosition, v)
    }

    val storyName = itemView.findViewById(R.id.txtStoryName) as TextView
    val storyImage = itemView.findViewById(R.id.imageViewChildrenStories) as ImageView

    init {
        itemView.setOnClickListener(this)
    }
}

interface ClickListener {
    fun onClick(pos: Int, aView: View)
  }
}

My new Activity To show Details of the Story

class ChildrenStoryPreview : AppCompatActivity() {

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

   }
}
Mohamed Wessam
  • 167
  • 2
  • 12
  • Possible duplicate of this question [Click Listener](https://stackoverflow.com/questions/54219825/android-kotlin-how-to-add-click-listener-to-recyclerview-adapter/54222687#54222687) – Bhoomin Naik Mar 17 '19 at 16:14
  • @BhoominNaik Thank you for your help. I updated my code above depending on your answer, I still have a problem that when I use `if (pos == 0){ my_text_view.text = "Story number 1" my_imageview.setImageResource(R.drawable.pic1) }` the App crash, but when i remove this part of code it works but the new activity will have the same pic and text for all positions, how can I Change text and picture in **ChildrenStoryPreview** Activity? – Mohamed Wessam Mar 18 '19 at 11:03
  • Finally I solved it by using `intent.putExtra("Key", pos)` to send the position to the other activity and in the other activity I used if statment to change text and it worked. Thank you Bhoomin – Mohamed Wessam Mar 19 '19 at 10:30

1 Answers1

1

Pass the Event Listener to Adapter constructor also to viewholder to catch view holder (items) clicks.

class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>
                                     var clickListener:MyClickListener?) :
        RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {
        ...
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
              val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
              return ViewHolder(v, clickListener)
        }
    ...

    inner class ViewHolder(itemView: View, clickListener:MyClickListener?) : 
      RecyclerView.ViewHolder(itemView) {
    itemView.setOnClickListener { clickListener?.myClickedFun(...) }
    ...

   class ChildrenStoryPreview : AppCompatActivity(), MyClickListener {
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContentView(R.layout.activity_children_story_preview)
         }
         override fun myClickedFun(...) {
         ...
        }
    }

Later init adapter like ..

 val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist, this)
Narek Hayrapetyan
  • 1,731
  • 15
  • 27
  • Thank you for your help. I updated my code above, I still have a problem that when I use `if (pos == 0){ my_text_view.text = "Story number 1" my_imageview.setImageResource(R.drawable.pic1) }` the App crash, but when i remove this part of code it works but the new activity will have the same pic and text for all positions, how can I Change text and picture in **ChildrenStoryPreview** Activity? – Mohamed Wessam Mar 18 '19 at 11:20
  • It didn't give me error the app open with out any error put when I click on item 1 or 2 at RecyclerView which I made (if) for them; the app stoped and closed. when I click at any other item it opens the CheldrenStoryPreview Activity without any problem – Mohamed Wessam Mar 18 '19 at 23:38
  • when I try to debug at line `my_text_view.text = "Story number 2"` This what shows in logcat ... EXCEPTION: main Process: com.mohamedali.coco, PID: 21476 java.lang.IllegalStateException: my_text_view must not be null at com.mohamedali.coco.ChildrenStories.MainChildrenStories$onCreate$1.onClick(MainChildrenStories.kt:97) at com.mohamedali.coco.ChildrenStories.ChildrenStoriesAdapter$ViewHolder.onClick(ChildrenStoriesAdapter.kt:39) at android.view.View.performClick(View.java:4856) at android.view.View$PerformClick.run(View.java:19956) ... – Mohamed Wessam Mar 19 '19 at 00:01
  • Finally I solved it by using `intent.putExtra("Key", pos)` to send the position to the other activity and in the other activity I used if statment and it worked. Thank you Narek – Mohamed Wessam Mar 19 '19 at 10:29