Android Kotlin. Troubles with databinding to recyclerview using fragments
up vote
0
down vote
favorite
I'm trying to learn how to implement databinding in an Android app. I have a small app I'm working with to learn this. And while I have databinding working for part of the app. I have hit a hiccup when trying to implement a recyclerview. I just cannot seem to get it. Been banging away at it for two or three days, and getting frustrated. Thought I'd ask you guys.
The app is super simple at this point.
The part i'm stuck on is accessing my recyclerview from an .xml layout from my MainFragment.kt
At first I was trying to use binding, but got frustrated and went back to just trying to use findViewById, but that is giving me issue too. I am beginning to think, I don't have as firm a grasp on databinding as I thought I did.
This is from the fragment that holds the recyclerView:
fragment_main.xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:id="@+id/job_recyclerView"/>
I have another small layout file that is using Cardview to show each individual item in the recyclerview
A super simple Model:
JobData.kt
data class JobData(val companyName: String, val location: String)
An Adapter:
JobAdapter.kt
class CustomAdapter(val userList: ArrayList<JobData>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_item_layout, parent, false)
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
override fun getItemCount(): Int {
return userList.size
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewName = itemView.findViewById(R.id.tv_company_name) as TextView
val textViewAddress = itemView.findViewById(R.id.tv_Location) as TextView
textViewName.text = job.companyName
textViewAddress.text = job.location
}
}
}
And then the code in my MainFragment to handle it all, which it is not doing. I've tried everything, it was getting ugly. As you can see below. Binding is in place and working for my FloatingActionButton. But I for some reason cannot figure out how to access that recylerview. At the point the code is at below, I thought I'd just accessing using findViewById, but that is not working either.
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentMainBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_main, container, false)
//Setting onClickListener for FAB(floating action button) using Navigation
binding.createNewJobFAB.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
}
//getting recyclerview from xml
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//Arraylist to store jobs using the data class JobData
val jobs = ArrayList<JobData>()
//add dummy data to list
jobs.add(JobData("A Company", "Town A"))
jobs.add(JobData("B Company", "Town B"))
jobs.add(JobData("C Company", "Town C"))
jobs.add(JobData("D Company", "Town D"))
//creating adapter
val adapter = CustomAdapter(jobs)
//add adapter to recyclerView
recyclerView.adapter = adapter
return binding.root
}
}
The above fails to compile for two reasons:
- findViewById shows as an "Unresolved Reference".
- When adding the layoutManager, "this" shows as a "Type Mismatch"
Which I believe is due to the fact that Fragments do not have a context. Or so, I think anyway. But I don't know to resolve that? Maybe override some other method, but I can't seem to figure out which or how?
Oh and MainActivity looks like:
MainActivity.kt
class MainActivity : AppCompatActivity() {
//private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
//Ensures back button works as it should
override fun onSupportNavigateUp() = findNavController(this, R.id.navHostFragment).navigateUp()
}
Which is pointing to Nav_Graph for Android Navigation (part of JetPack). This bit is fine and working.
Adding gradle files to show that my dependencies were set correctly as suggested below.
app/gradle
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
correctErrorTypes = true
}
dependencies {
...
kapt "com.android.databinding:compiler:$gradle_version"
...
}
android android-fragments android-recyclerview kotlin android-databinding
add a comment |
up vote
0
down vote
favorite
I'm trying to learn how to implement databinding in an Android app. I have a small app I'm working with to learn this. And while I have databinding working for part of the app. I have hit a hiccup when trying to implement a recyclerview. I just cannot seem to get it. Been banging away at it for two or three days, and getting frustrated. Thought I'd ask you guys.
The app is super simple at this point.
The part i'm stuck on is accessing my recyclerview from an .xml layout from my MainFragment.kt
At first I was trying to use binding, but got frustrated and went back to just trying to use findViewById, but that is giving me issue too. I am beginning to think, I don't have as firm a grasp on databinding as I thought I did.
This is from the fragment that holds the recyclerView:
fragment_main.xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:id="@+id/job_recyclerView"/>
I have another small layout file that is using Cardview to show each individual item in the recyclerview
A super simple Model:
JobData.kt
data class JobData(val companyName: String, val location: String)
An Adapter:
JobAdapter.kt
class CustomAdapter(val userList: ArrayList<JobData>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_item_layout, parent, false)
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
override fun getItemCount(): Int {
return userList.size
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewName = itemView.findViewById(R.id.tv_company_name) as TextView
val textViewAddress = itemView.findViewById(R.id.tv_Location) as TextView
textViewName.text = job.companyName
textViewAddress.text = job.location
}
}
}
And then the code in my MainFragment to handle it all, which it is not doing. I've tried everything, it was getting ugly. As you can see below. Binding is in place and working for my FloatingActionButton. But I for some reason cannot figure out how to access that recylerview. At the point the code is at below, I thought I'd just accessing using findViewById, but that is not working either.
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentMainBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_main, container, false)
//Setting onClickListener for FAB(floating action button) using Navigation
binding.createNewJobFAB.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
}
//getting recyclerview from xml
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//Arraylist to store jobs using the data class JobData
val jobs = ArrayList<JobData>()
//add dummy data to list
jobs.add(JobData("A Company", "Town A"))
jobs.add(JobData("B Company", "Town B"))
jobs.add(JobData("C Company", "Town C"))
jobs.add(JobData("D Company", "Town D"))
//creating adapter
val adapter = CustomAdapter(jobs)
//add adapter to recyclerView
recyclerView.adapter = adapter
return binding.root
}
}
The above fails to compile for two reasons:
- findViewById shows as an "Unresolved Reference".
- When adding the layoutManager, "this" shows as a "Type Mismatch"
Which I believe is due to the fact that Fragments do not have a context. Or so, I think anyway. But I don't know to resolve that? Maybe override some other method, but I can't seem to figure out which or how?
Oh and MainActivity looks like:
MainActivity.kt
class MainActivity : AppCompatActivity() {
//private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
//Ensures back button works as it should
override fun onSupportNavigateUp() = findNavController(this, R.id.navHostFragment).navigateUp()
}
Which is pointing to Nav_Graph for Android Navigation (part of JetPack). This bit is fine and working.
Adding gradle files to show that my dependencies were set correctly as suggested below.
app/gradle
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
correctErrorTypes = true
}
dependencies {
...
kapt "com.android.databinding:compiler:$gradle_version"
...
}
android android-fragments android-recyclerview kotlin android-databinding
To makefindViewById()
andLinearLayout
constructor work useFragment#getContext()
. To solve problem with databinding, ensure please, that you haveapply plugin: 'kotlin-kapt'
inapp/build.gradle
andkapt "com.android.databinding:compiler:version"
in dependencies section ofapp/build.gradle
– ConstOrVar
Nov 18 at 8:31
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
No still working on it.
– CB Midkiff
Nov 19 at 8:31
What is valuf of$gradle_version
?
– ConstOrVar
Nov 19 at 9:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to learn how to implement databinding in an Android app. I have a small app I'm working with to learn this. And while I have databinding working for part of the app. I have hit a hiccup when trying to implement a recyclerview. I just cannot seem to get it. Been banging away at it for two or three days, and getting frustrated. Thought I'd ask you guys.
The app is super simple at this point.
The part i'm stuck on is accessing my recyclerview from an .xml layout from my MainFragment.kt
At first I was trying to use binding, but got frustrated and went back to just trying to use findViewById, but that is giving me issue too. I am beginning to think, I don't have as firm a grasp on databinding as I thought I did.
This is from the fragment that holds the recyclerView:
fragment_main.xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:id="@+id/job_recyclerView"/>
I have another small layout file that is using Cardview to show each individual item in the recyclerview
A super simple Model:
JobData.kt
data class JobData(val companyName: String, val location: String)
An Adapter:
JobAdapter.kt
class CustomAdapter(val userList: ArrayList<JobData>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_item_layout, parent, false)
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
override fun getItemCount(): Int {
return userList.size
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewName = itemView.findViewById(R.id.tv_company_name) as TextView
val textViewAddress = itemView.findViewById(R.id.tv_Location) as TextView
textViewName.text = job.companyName
textViewAddress.text = job.location
}
}
}
And then the code in my MainFragment to handle it all, which it is not doing. I've tried everything, it was getting ugly. As you can see below. Binding is in place and working for my FloatingActionButton. But I for some reason cannot figure out how to access that recylerview. At the point the code is at below, I thought I'd just accessing using findViewById, but that is not working either.
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentMainBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_main, container, false)
//Setting onClickListener for FAB(floating action button) using Navigation
binding.createNewJobFAB.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
}
//getting recyclerview from xml
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//Arraylist to store jobs using the data class JobData
val jobs = ArrayList<JobData>()
//add dummy data to list
jobs.add(JobData("A Company", "Town A"))
jobs.add(JobData("B Company", "Town B"))
jobs.add(JobData("C Company", "Town C"))
jobs.add(JobData("D Company", "Town D"))
//creating adapter
val adapter = CustomAdapter(jobs)
//add adapter to recyclerView
recyclerView.adapter = adapter
return binding.root
}
}
The above fails to compile for two reasons:
- findViewById shows as an "Unresolved Reference".
- When adding the layoutManager, "this" shows as a "Type Mismatch"
Which I believe is due to the fact that Fragments do not have a context. Or so, I think anyway. But I don't know to resolve that? Maybe override some other method, but I can't seem to figure out which or how?
Oh and MainActivity looks like:
MainActivity.kt
class MainActivity : AppCompatActivity() {
//private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
//Ensures back button works as it should
override fun onSupportNavigateUp() = findNavController(this, R.id.navHostFragment).navigateUp()
}
Which is pointing to Nav_Graph for Android Navigation (part of JetPack). This bit is fine and working.
Adding gradle files to show that my dependencies were set correctly as suggested below.
app/gradle
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
correctErrorTypes = true
}
dependencies {
...
kapt "com.android.databinding:compiler:$gradle_version"
...
}
android android-fragments android-recyclerview kotlin android-databinding
I'm trying to learn how to implement databinding in an Android app. I have a small app I'm working with to learn this. And while I have databinding working for part of the app. I have hit a hiccup when trying to implement a recyclerview. I just cannot seem to get it. Been banging away at it for two or three days, and getting frustrated. Thought I'd ask you guys.
The app is super simple at this point.
The part i'm stuck on is accessing my recyclerview from an .xml layout from my MainFragment.kt
At first I was trying to use binding, but got frustrated and went back to just trying to use findViewById, but that is giving me issue too. I am beginning to think, I don't have as firm a grasp on databinding as I thought I did.
This is from the fragment that holds the recyclerView:
fragment_main.xml
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
android:id="@+id/job_recyclerView"/>
I have another small layout file that is using Cardview to show each individual item in the recyclerview
A super simple Model:
JobData.kt
data class JobData(val companyName: String, val location: String)
An Adapter:
JobAdapter.kt
class CustomAdapter(val userList: ArrayList<JobData>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_item_layout, parent, false)
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
override fun getItemCount(): Int {
return userList.size
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewName = itemView.findViewById(R.id.tv_company_name) as TextView
val textViewAddress = itemView.findViewById(R.id.tv_Location) as TextView
textViewName.text = job.companyName
textViewAddress.text = job.location
}
}
}
And then the code in my MainFragment to handle it all, which it is not doing. I've tried everything, it was getting ugly. As you can see below. Binding is in place and working for my FloatingActionButton. But I for some reason cannot figure out how to access that recylerview. At the point the code is at below, I thought I'd just accessing using findViewById, but that is not working either.
MainFragment.kt
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding: FragmentMainBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_main, container, false)
//Setting onClickListener for FAB(floating action button) using Navigation
binding.createNewJobFAB.setOnClickListener { v: View ->
v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
}
//getting recyclerview from xml
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
//adding a layoutmanager
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
//Arraylist to store jobs using the data class JobData
val jobs = ArrayList<JobData>()
//add dummy data to list
jobs.add(JobData("A Company", "Town A"))
jobs.add(JobData("B Company", "Town B"))
jobs.add(JobData("C Company", "Town C"))
jobs.add(JobData("D Company", "Town D"))
//creating adapter
val adapter = CustomAdapter(jobs)
//add adapter to recyclerView
recyclerView.adapter = adapter
return binding.root
}
}
The above fails to compile for two reasons:
- findViewById shows as an "Unresolved Reference".
- When adding the layoutManager, "this" shows as a "Type Mismatch"
Which I believe is due to the fact that Fragments do not have a context. Or so, I think anyway. But I don't know to resolve that? Maybe override some other method, but I can't seem to figure out which or how?
Oh and MainActivity looks like:
MainActivity.kt
class MainActivity : AppCompatActivity() {
//private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("UNUSED_VARIABLE")
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}
//Ensures back button works as it should
override fun onSupportNavigateUp() = findNavController(this, R.id.navHostFragment).navigateUp()
}
Which is pointing to Nav_Graph for Android Navigation (part of JetPack). This bit is fine and working.
Adding gradle files to show that my dependencies were set correctly as suggested below.
app/gradle
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
...
}
kapt {
generateStubs = true
correctErrorTypes = true
}
dependencies {
...
kapt "com.android.databinding:compiler:$gradle_version"
...
}
android android-fragments android-recyclerview kotlin android-databinding
android android-fragments android-recyclerview kotlin android-databinding
edited Nov 18 at 8:49
asked Nov 18 at 8:15
CB Midkiff
124
124
To makefindViewById()
andLinearLayout
constructor work useFragment#getContext()
. To solve problem with databinding, ensure please, that you haveapply plugin: 'kotlin-kapt'
inapp/build.gradle
andkapt "com.android.databinding:compiler:version"
in dependencies section ofapp/build.gradle
– ConstOrVar
Nov 18 at 8:31
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
No still working on it.
– CB Midkiff
Nov 19 at 8:31
What is valuf of$gradle_version
?
– ConstOrVar
Nov 19 at 9:23
add a comment |
To makefindViewById()
andLinearLayout
constructor work useFragment#getContext()
. To solve problem with databinding, ensure please, that you haveapply plugin: 'kotlin-kapt'
inapp/build.gradle
andkapt "com.android.databinding:compiler:version"
in dependencies section ofapp/build.gradle
– ConstOrVar
Nov 18 at 8:31
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
No still working on it.
– CB Midkiff
Nov 19 at 8:31
What is valuf of$gradle_version
?
– ConstOrVar
Nov 19 at 9:23
To make
findViewById()
and LinearLayout
constructor work use Fragment#getContext()
. To solve problem with databinding, ensure please, that you have apply plugin: 'kotlin-kapt'
in app/build.gradle
and kapt "com.android.databinding:compiler:version"
in dependencies section of app/build.gradle
– ConstOrVar
Nov 18 at 8:31
To make
findViewById()
and LinearLayout
constructor work use Fragment#getContext()
. To solve problem with databinding, ensure please, that you have apply plugin: 'kotlin-kapt'
in app/build.gradle
and kapt "com.android.databinding:compiler:version"
in dependencies section of app/build.gradle
– ConstOrVar
Nov 18 at 8:31
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
No still working on it.
– CB Midkiff
Nov 19 at 8:31
No still working on it.
– CB Midkiff
Nov 19 at 8:31
What is valuf of
$gradle_version
?– ConstOrVar
Nov 19 at 9:23
What is valuf of
$gradle_version
?– ConstOrVar
Nov 19 at 9:23
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Encase your xml in <layout>..<layout/>
private lateinit var binding: FragmentXXXBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentXXXBinding.inflate(inflater)
return binding.root
}
Then you can call recyclerview by binding.jobRecyclerview
try to set all the click listeners etc on onViewCreated
rather than onCreateView
of fragment
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
|
show 2 more comments
up vote
0
down vote
It is wrong way to findViewById from Fragment(it is good technique for Activity):
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
First, fragment's layout have to be return by onCreateView() method.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
I personally like do all fragment's business logic inside onViewCreated()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Now, we can use views by kotlinx
//val recyclerView = job_recyclerView
//Or old-fashioned way
val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}
RecylerView can be accessed from fragment's layout by having root view like: getView()!!.findViewById
or by kotlinx inside onViewCreated(): job_recyclerView
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,onViewCreated()
should handle the business logic, notoncreateView()
, this should just be for inflating the layout
– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
add a comment |
up vote
0
down vote
Ok, so first of all you are getting error on findViewById
bcoz your fragment is unaware about the view that contains recyclerView
What you should do is, take an instance of view that you are inflating for this fragment ( declare view as a global variable, replace your inflater line with this. )
var rootView
// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false)
Now replace, findViewById()
with rootView.findViewById()
And the other error is bcoz the fragment does not have any context of it's own so replace this
with activity!!
By writing activity!!
you are calling getActicity()
method which returns context of parent activity.
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Encase your xml in <layout>..<layout/>
private lateinit var binding: FragmentXXXBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentXXXBinding.inflate(inflater)
return binding.root
}
Then you can call recyclerview by binding.jobRecyclerview
try to set all the click listeners etc on onViewCreated
rather than onCreateView
of fragment
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
|
show 2 more comments
up vote
0
down vote
accepted
Encase your xml in <layout>..<layout/>
private lateinit var binding: FragmentXXXBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentXXXBinding.inflate(inflater)
return binding.root
}
Then you can call recyclerview by binding.jobRecyclerview
try to set all the click listeners etc on onViewCreated
rather than onCreateView
of fragment
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
|
show 2 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Encase your xml in <layout>..<layout/>
private lateinit var binding: FragmentXXXBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentXXXBinding.inflate(inflater)
return binding.root
}
Then you can call recyclerview by binding.jobRecyclerview
try to set all the click listeners etc on onViewCreated
rather than onCreateView
of fragment
Encase your xml in <layout>..<layout/>
private lateinit var binding: FragmentXXXBinding
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = FragmentXXXBinding.inflate(inflater)
return binding.root
}
Then you can call recyclerview by binding.jobRecyclerview
try to set all the click listeners etc on onViewCreated
rather than onCreateView
of fragment
edited Nov 18 at 14:15
answered Nov 18 at 10:02
Blue Jones
1396
1396
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
|
show 2 more comments
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
Your example is how i'd like to do this. And is one of the ways I tried the other day. But for reason I cannot figure out just yet, it does not work. I have my .xml incased in <layout>..<layout/> and have just added the lateinit to FragmentMainBinding. But calling binding.job_recyclerView is where it fails. job_recyclerView remains as unresolved? This is where I am getting confused on binding.
– CB Midkiff
Nov 19 at 8:22
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
binding.jobRecyclerview it should be, does it not auto fill? I'd so a clean/rebuild project if it's not autofilling
– Blue Jones
Nov 19 at 8:43
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Agreed. I have cleaned/rebuilt project and even restarted Android Studio. It does not like it.
– CB Midkiff
Nov 19 at 9:11
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Can you please edit and post all xml, i've got a feeling it'll be something silly like a name wrong aha
– Blue Jones
Nov 19 at 9:27
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
Sorry, sorry. I fat fingered it. It is now seeing binding.jobRecyclerView.
– CB Midkiff
Nov 19 at 9:33
|
show 2 more comments
up vote
0
down vote
It is wrong way to findViewById from Fragment(it is good technique for Activity):
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
First, fragment's layout have to be return by onCreateView() method.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
I personally like do all fragment's business logic inside onViewCreated()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Now, we can use views by kotlinx
//val recyclerView = job_recyclerView
//Or old-fashioned way
val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}
RecylerView can be accessed from fragment's layout by having root view like: getView()!!.findViewById
or by kotlinx inside onViewCreated(): job_recyclerView
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,onViewCreated()
should handle the business logic, notoncreateView()
, this should just be for inflating the layout
– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
add a comment |
up vote
0
down vote
It is wrong way to findViewById from Fragment(it is good technique for Activity):
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
First, fragment's layout have to be return by onCreateView() method.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
I personally like do all fragment's business logic inside onViewCreated()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Now, we can use views by kotlinx
//val recyclerView = job_recyclerView
//Or old-fashioned way
val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}
RecylerView can be accessed from fragment's layout by having root view like: getView()!!.findViewById
or by kotlinx inside onViewCreated(): job_recyclerView
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,onViewCreated()
should handle the business logic, notoncreateView()
, this should just be for inflating the layout
– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
add a comment |
up vote
0
down vote
up vote
0
down vote
It is wrong way to findViewById from Fragment(it is good technique for Activity):
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
First, fragment's layout have to be return by onCreateView() method.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
I personally like do all fragment's business logic inside onViewCreated()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Now, we can use views by kotlinx
//val recyclerView = job_recyclerView
//Or old-fashioned way
val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}
RecylerView can be accessed from fragment's layout by having root view like: getView()!!.findViewById
or by kotlinx inside onViewCreated(): job_recyclerView
It is wrong way to findViewById from Fragment(it is good technique for Activity):
val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView
First, fragment's layout have to be return by onCreateView() method.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_main, container, false)
}
I personally like do all fragment's business logic inside onViewCreated()
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Now, we can use views by kotlinx
//val recyclerView = job_recyclerView
//Or old-fashioned way
val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}
RecylerView can be accessed from fragment's layout by having root view like: getView()!!.findViewById
or by kotlinx inside onViewCreated(): job_recyclerView
answered Nov 18 at 9:10
koliczyna
21619
21619
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,onViewCreated()
should handle the business logic, notoncreateView()
, this should just be for inflating the layout
– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
add a comment |
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,onViewCreated()
should handle the business logic, notoncreateView()
, this should just be for inflating the layout
– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Sorry, your formatting is confusing to me? It seems like your suggesting using both 'onCreateView' and 'onViewCreated', or am I reading this wrong?
– CB Midkiff
Nov 19 at 9:05
Agree with koliczyna,
onViewCreated()
should handle the business logic, not oncreateView()
, this should just be for inflating the layout– Blue Jones
Nov 19 at 9:30
Agree with koliczyna,
onViewCreated()
should handle the business logic, not oncreateView()
, this should just be for inflating the layout– Blue Jones
Nov 19 at 9:30
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
@CB Midkiff You are correct, I suggest to use both of them.
– koliczyna
Nov 20 at 8:53
add a comment |
up vote
0
down vote
Ok, so first of all you are getting error on findViewById
bcoz your fragment is unaware about the view that contains recyclerView
What you should do is, take an instance of view that you are inflating for this fragment ( declare view as a global variable, replace your inflater line with this. )
var rootView
// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false)
Now replace, findViewById()
with rootView.findViewById()
And the other error is bcoz the fragment does not have any context of it's own so replace this
with activity!!
By writing activity!!
you are calling getActicity()
method which returns context of parent activity.
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
add a comment |
up vote
0
down vote
Ok, so first of all you are getting error on findViewById
bcoz your fragment is unaware about the view that contains recyclerView
What you should do is, take an instance of view that you are inflating for this fragment ( declare view as a global variable, replace your inflater line with this. )
var rootView
// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false)
Now replace, findViewById()
with rootView.findViewById()
And the other error is bcoz the fragment does not have any context of it's own so replace this
with activity!!
By writing activity!!
you are calling getActicity()
method which returns context of parent activity.
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
add a comment |
up vote
0
down vote
up vote
0
down vote
Ok, so first of all you are getting error on findViewById
bcoz your fragment is unaware about the view that contains recyclerView
What you should do is, take an instance of view that you are inflating for this fragment ( declare view as a global variable, replace your inflater line with this. )
var rootView
// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false)
Now replace, findViewById()
with rootView.findViewById()
And the other error is bcoz the fragment does not have any context of it's own so replace this
with activity!!
By writing activity!!
you are calling getActicity()
method which returns context of parent activity.
Ok, so first of all you are getting error on findViewById
bcoz your fragment is unaware about the view that contains recyclerView
What you should do is, take an instance of view that you are inflating for this fragment ( declare view as a global variable, replace your inflater line with this. )
var rootView
// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false)
Now replace, findViewById()
with rootView.findViewById()
And the other error is bcoz the fragment does not have any context of it's own so replace this
with activity!!
By writing activity!!
you are calling getActicity()
method which returns context of parent activity.
answered Nov 18 at 9:15
Kinjal Rathod
33125
33125
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
add a comment |
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
I followed your suggestions and it did take care of the compiler errors, and the app now runs. The RecyclerView does not populate as it should. I does not show up at all. I think it is how that inflater is called. But I only had a little bit of time to work on it. I've been at the immigration offices all day dealing with Visa issues. Im going to work on it more this afternoon and tonight.
– CB Midkiff
Nov 19 at 8:06
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359022%2fandroid-kotlin-troubles-with-databinding-to-recyclerview-using-fragments%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
To make
findViewById()
andLinearLayout
constructor work useFragment#getContext()
. To solve problem with databinding, ensure please, that you haveapply plugin: 'kotlin-kapt'
inapp/build.gradle
andkapt "com.android.databinding:compiler:version"
in dependencies section ofapp/build.gradle
– ConstOrVar
Nov 18 at 8:31
Sorry, I forgot to show or mention my dependencies. Those are all set. I will edit main post to show that info. I will looki n into using Fragment#getContext() if I cannot get binding to work.
– CB Midkiff
Nov 18 at 8:41
Have you already solved that problem?
– ConstOrVar
Nov 18 at 12:44
No still working on it.
– CB Midkiff
Nov 19 at 8:31
What is valuf of
$gradle_version
?– ConstOrVar
Nov 19 at 9:23