0

I want to have an add button after all my exercise Items like this:

Image of the RecyclerView I want

and I tried Implementing it in my CustomAdapter like this:

class CustomAdapterExercise(val exerciseList: ArrayList<Exercise>) : RecyclerView.Adapter<CustomAdapterExercise.ViewHolder>() {

val typeAdd = 0
val typeExercise = 1

//this method is returning the view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapterExercise.ViewHolder {

    if (viewType == typeAdd) {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.exercise_layout, parent, false)
        return ViewHolder(itemView)
    } else if (viewType == typeExercise) {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.add_layout, parent, false)
        return ViewHolder(itemView)
    }
    else{
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.add_layout, parent, false)
        return ViewHolder(itemView)
    }

}

//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) {
    holder.bindItems(exerciseList[position])
}

override fun getItemViewType(position: Int): Int {
    if (position <= exerciseList.size) {
        return typeExercise
    } else if (position == exerciseList.size + 1) {
        return typeAdd
    }
    else{
        return typeAdd
    }
}

//this method is giving the size of the list
override fun getItemCount(): Int {
    return exerciseList.size + 1
}

//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun bindItems(Exercise: Exercise) {

        val exerciseAmount = itemView.findViewById<TextView>(R.id.exerciseAmount)
        val exerciseName  = itemView.findViewById<TextView>(R.id.exerciseName)
        val exerciseWeight = itemView.findViewById<TextView>(R.id.exerciseWeight)
        val exerciseSets  = itemView.findViewById<TextView>(R.id.exerciseSets)
        exerciseAmount.text = Exercise.exAmount
        exerciseName.text = Exercise.exName
        exerciseWeight.text = Exercise.weight
        exerciseSets.text = Exercise.sets
    }
}
}

but It crashes as soon as I go to that view and it gives me this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                      at abyte.fitness.fitnessbyte.CustomAdapterExercise$ViewHolder.bindItems(CustomAdapterExercise.kt:67)

I get that this is because I am assigning text to my add-button but I don't know how to do this differently and make it work.

I have already seen the "duplicate" question when I was searching all day but it is not explained well for a beginner like myself...

Community
  • 1
  • 1
KrisKodira
  • 53
  • 11
  • 1
    "it is not explained well for a beginner" - SO is for enthousiast programmers, not for beginners who are looking for in-depth tutorials on basic stuff. If you have trouble understanding the answers in the linked post, you might want to spend some time on the android developer docs first, to get a basic understanding of android – Tim Nov 20 '17 at 12:18
  • @TimCastelijns well I am a enthousiast programmer I just don't have the skills for android programming yet. Also I will take a look into the docs thank you. I didn't know StackOverflow is only for pros in every area. – KrisKodira Nov 20 '17 at 12:28
  • *I didn't know StackOverflow is only for pros in every area.* - it's not. I didn't say that. It's just not for beginners who want to be spoonfed information – Tim Nov 20 '17 at 12:40
  • @TimCastelijns you're totally right yes, but after trying for around 4 hours and doing half the thing myself and not understanding the last half I think it is completely okay to ask... don't you think so? – KrisKodira Nov 20 '17 at 13:13

1 Answers1

0

You need to switch on viewType in onBindViewHolder.

override fun onBindViewHolder(holder: CustomAdapterExercise.ViewHolder, position: Int) {
    if (holder.itemViewType == typeAdd) {
        // Bind the add button
    else {
        holder.bindItems(exerciseList[position])
    }
}

Sidenote:

You're using a ViewHolder the wrong way, one of the benefits of ViewHolders is not being forced to do findByView every time you update a View.

patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35