Johannes M
Johannes M

Reputation: 65

RecyclerView is not showing data

I'm currently trying to create a dynamic header with a recyclerView. I have written the ListAdapter aswell as the ViewHolder. The custom list elements are added, and also the numer of the elements within the list is correct, but somehow it's not showing the object data, but only the dummyText that was added at the layoutdesign.

HeaderlistAdapter:

class HeaderListAdapter(val context: Context, val headers: List<CustomHeader>) : RecyclerView.Adapter<HeaderViewHolder>() {


override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): HeaderViewHolder {
    val view = LayoutInflater.from(context).inflate(R.layout.ui_basic_custom_list_element_header, parent, false)
    return HeaderViewHolder(view)
}

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


override fun onBindViewHolder(holder: HeaderViewHolder?, position: Int) {
    holder?.bindHeader(headers[position])
}
fun setFocus(step:UIStep)
{
    for(header in headers)
        header.Active=header.MainContent==step
    notifyDataSetChanged()
  }
}

HeaderViewHolder:

class HeaderViewHolder:RecyclerView.ViewHolder{
@Bind(R.id.ui_adapter_main) var mainText:TextView?=null
@Bind(R.id.ui_adapter_additional) var additionalText:TextView?=null
@Bind(R.id.ui_adapter_layout) var layout:LinearLayout?=null


constructor(itemView: View): super(itemView){
    ButterKnife.bind(this,itemView)
}
fun bindHeader(header:CustomHeader){
    if(header.Active) {
        mainText?.text = header.MainContent.description
        additionalText?.text=header.AdditionalText
        layout?.setBackgroundColor(R.color.colorBackgroundActive.toInt())
    }
    else{
        mainText?.text=header.MainContent.number.toString()
        additionalText?.text=""
        layout?.setBackgroundColor(R.color.colorBackgroundInactive.toInt())
    }
  }
}

Here is, how the listAdapter looks within the view

<android.support.v7.widget.RecyclerView
        android:id="@+id/ui_basic_lv_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@color/colorBackgroundInactive"
        android:layout_weight="1" />

Below here you se the xml of the custom element

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/colorBackgroundInactive"
android:textColor="@color/colorHeaderFont"
android:id="@+id/ui_adapter_layout">
<TextView
    android:id="@+id/ui_adapter_main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="Main Info"
    android:textSize="36sp"/>
<TextView
    android:id="@+id/ui_adapter_additional"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="additional"
    android:gravity="center_vertical"/>

Upvotes: 0

Views: 213

Answers (3)

Johannes M
Johannes M

Reputation: 65

Looks like there was something wrong with the

@Bind

I replaced that by using findViewById within the binHeader function:

fun bindHeader(header:CustomHeader){
    val mainText = itemView.findViewById<TextView>(R.id.ui_adapter_main)
    val additionalText = itemView.findViewById<TextView>(R.id.ui_adapter_additional)
    val layout = itemView.findViewById<LinearLayout>(R.id.ui_adapter_layout)
    if(header.Active) {
        mainText?.text = header.MainContent.description
        additionalText?.text=header.AdditionalText
        layout?.setBackgroundColor(R.color.colorBackgroundActive.toInt())
    }
    else{
        mainText?.text=header.MainContent.number.toString()
        additionalText?.text=""
        layout?.setBackgroundColor(R.color.colorBackgroundInactive.toInt())
    }
}

Upvotes: 1

wilddev
wilddev

Reputation: 1964

You should set layout manager for recyclerview

myRecyclerView.setLayoutManager(linearLayoutManagerVertical);

Upvotes: 0

Melrose
Melrose

Reputation: 81

contentList may be null ,if contentList is Null, the method following '?' will not execute . and after setting the adapter can not call notifyDataSetChanged ;

Upvotes: 0

Related Questions