hope you guys are all well.
Newbie to Android development. I am trying out RecyclerViews but it is not getting rendered at all. Is there a certain step I am missing?
Here is my mainActivity kotlin file :
package com.example.alphablog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.alphablog.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rv = findViewById<RecyclerView>(R.id.recycler_view_main)
rv.layoutManager = LinearLayoutManager(this)
rv.adapter = PostAdapter()
}
}
Here is my main activity xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:i`enter code here`temCount="30"
tools:listitem="#layout/recycler_view_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my recycler view xml :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_baseline_add_business_24"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title of Blog Post"
android:textSize="20sp"
android:textColor="#android:color/black"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="285dp"
android:layout_height="20dp"
android:text="Body of first blog post"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.050" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit and Delete action"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
And finally, here is my adapter class boiler plate:
package com.example.alphablog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
class PostViewHolder(postView: View) : RecyclerView.ViewHolder(postView){
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder { // creates view to display
val postView = LayoutInflater.from(parent.context).inflate(R.layout.post, parent,false)
return PostViewHolder(postView)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
}
override fun getItemCount(): Int { // amount of items you will be working with in your recycler view
return 30
}
}
Related
I am new to programming and want to ask for help because I am stuck on this task.
Clicking on the RecyclerView gets this error.
Below I will attach my code and hope for your help and solutions to the problem. Thank you
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.skreep.worko, PID: 24044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skreep.worko/com.skreep.worko.DetailActivity}: java.lang.NullPointerException: workout_title must not be null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3308)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.NullPointerException: workout_title must not be null
at com.skreep.worko.DetailActivity.onCreate(DetailActivity.kt:32)
at android.app.Activity.performCreate(Activity.java:7894)
at android.app.Activity.performCreate(Activity.java:7881)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3283)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3457)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7560)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Workout Adapter
package com.skreep.worko.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.skreep.worko.DetailActivity
import com.skreep.worko.R
import com.skreep.worko.databinding.ItemWorkoutBinding
import com.skreep.worko.model.WorkoutData
class WorkoutAdapter(var c:Context,var workoutList:ArrayList<WorkoutData>
):RecyclerView.Adapter<WorkoutAdapter.WorkoutViewHolder>()
{
inner class WorkoutViewHolder(var v:ItemWorkoutBinding): RecyclerView.ViewHolder(v.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WorkoutViewHolder {
val inflter = LayoutInflater.from(parent.context)
val v = DataBindingUtil.inflate<ItemWorkoutBinding>(
inflter, R.layout.item_workout,parent,
false)
return WorkoutViewHolder(v)
}
override fun onBindViewHolder(holder: WorkoutViewHolder, position: Int) {
val newList = workoutList[position]
holder.v.isWorkouts = workoutList[position]
holder.v.root.setOnClickListener {
val name = newList.name
val description = newList.description
val fullTime = newList.fullTime
val workoutDesc = newList.workoutDesc
val workoutName = newList.workoutName
val workoutTime = newList.workoutTime
val mIntent = Intent(c,DetailActivity::class.java)
mIntent.putExtra("description",description)
mIntent.putExtra("name",name)
mIntent.putExtra("fullTime",fullTime)
mIntent.putExtra("workoutDesc",workoutDesc)
mIntent.putExtra("workoutName",workoutName)
mIntent.putExtra("workoutTime",workoutTime)
c.startActivity(mIntent)
}
}
override fun getItemCount(): Int {
return workoutList.size
}
}
Detail Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.DatabaseReference
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.item_workout.*
import kotlinx.android.synthetic.main.item_workout_detail.*
class DetailActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
val workoutIntent = intent
val name = workoutIntent.getStringExtra("name")
val description = workoutIntent.getStringExtra("description")
val workoutName = workoutIntent.getStringExtra("workoutName")
detail_name.text = name
detail_desc.text = description
workout_title.text = workoutName
}
}
Home Activity
package com.skreep.worko
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.*
import com.skreep.worko.adapter.WorkoutAdapter
import com.skreep.worko.model.WorkoutData
import kotlinx.android.synthetic.main.activity_detail.*
import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : BaseActivity() {
lateinit var mDataBase:DatabaseReference
private lateinit var workoutList:ArrayList<WorkoutData>
private lateinit var mAdapter:WorkoutAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
workoutList = ArrayList()
mAdapter = WorkoutAdapter(this, workoutList)
recyclerworkoutList.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerworkoutList.setHasFixedSize(true)
recyclerworkoutList.adapter = mAdapter
getWorkoutData()
}
private fun getWorkoutData() {
mDataBase = FirebaseDatabase.getInstance().getReference("Workout")
mDataBase.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (userSnapshot in snapshot.children) {
val workout = userSnapshot.getValue(WorkoutData::class.java)
workoutList.add(workout!!)
}
recyclerworkoutList.adapter = mAdapter
}
}
override fun onCancelled(error: DatabaseError) {
Toast.makeText(this#HomeActivity,
error.message, Toast.LENGTH_SHORT).show()
}
})
}
}
XML Activity Detail
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isData"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray"
tools:context=".DetailActivity">
<ImageView
android:id="#+id/back_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:padding="20dp"
android:src="#drawable/ic_back">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:padding="20dp"
android:text="#string/app_name"
android:textSize="20sp"></TextView>
<TextView
android:id="#+id/detail_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="120dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isData.name}"
android:textColor="#color/darkblue"
android:textSize="26sp"></TextView>
<TextView
android:id="#+id/detail_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/detail_name"
android:layout_alignLeft="#+id/detail_name"
android:fontFamily="#font/poppins"
android:text="#{isData.workoutDesc}"
android:textColor="#color/lightgray">
</TextView>
<TextView
android:id="#+id/tv_workout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="310dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:text="Упражнения"
android:textColor="#color/darkblue"
android:textSize="20sp"></TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/workoutListDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_workout"
android:orientation="vertical"
android:padding="10dp"
tools:itemCount="1"
/>
</RelativeLayout>
</layout>
Xml activity Home
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/darkblue"
android:orientation="vertical"
tools:context=".HomeActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_name"
android:textColor="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="30sp"
tools:ignore="MissingConstraints" />
<TextView
android:id="#+id/welcome_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="#string/welcome_description"
android:textColor="#color/desc_color"
android:textSize="14sp"
app:layout_constraintTop_toBottomOf="#id/welcome" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerworkoutList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal"
android:paddingTop="40dp"
tools:itemCount="1"
tools:listitem="#layout/item_workout" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</layout>
item_workout xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isWorkouts"
type="com.skreep.worko.model.WorkoutData" />
</data>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:layout_width="250dp"
android:layout_height="350dp"
android:layout_margin="10dp"
android:backgroundTint="#color/gray"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/watch_ic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="#drawable/ic_watch"
app:tint="#color/darkblue"></ImageView>
<TextView
android:id="#+id/tvfulltime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="50dp"
android:layout_marginTop="26dp"
android:fontFamily="#font/poppins"
android:text="#{isWorkouts.fullTime}"
android:textSize="10sp"></TextView>
<TextView
android:id="#+id/tvtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="27dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkouts.name}"
android:textColor="#color/darkblue"
android:textSize="24sp">
</TextView>
<TextView
android:id="#+id/tvdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtitle"
android:layout_marginStart="10dp"
android:padding="20dp"
android:text="#{isWorkouts.description}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
android:src="#drawable/ic_next"
>
</ImageView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
item workout detail xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="isWorkout"
type="com.skreep.worko.model.WorkoutData" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".SplashActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:backgroundTint="#color/gray">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/workout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:fontFamily="#font/poppins_bold"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/darkblue"
android:textSize="24sp"></TextView>
<TextView
android:id="#+id/workout_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/workout_title"
android:layout_alignLeft="#+id/workout_title"
android:fontFamily="#font/poppins"
android:text="#{isWorkout.workoutName}"
android:textColor="#color/lightgray"
android:textSize="12sp"></TextView>
<TextView
android:id="#+id/workout_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/workout_title"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:text="#{isWorkout.workoutTime}"
android:textColor="#color/darkblue"
android:textSize="12sp">
</TextView>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
</layout>
Check your activity_detail XML file is missing a Textview called workout_title
You have it in a different XML layout file workout_detail xml
I just created a simple RecyclerView project which will show 30 basic things . But when i run the file it is not showing anything. In fact my whole source code is just fine.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="12"
tools:listitem="#layout/recycler_view_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.saddyahmed.recyclerview
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rv_main.layoutManager = LinearLayoutManager(this)
rv_main.adapter = PostAdapter()
}
}
PostAdapter.kt
package com.saddyahmed.recyclerview
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
class PostAdapter : RecyclerView.Adapter<PostAdapter.PostViewHolder>() {
class PostViewHolder(postView: View) : RecyclerView.ViewHolder(postView){
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
val postView = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_item , parent ,false)
return PostViewHolder(postView)
}
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
}
override fun getItemCount(): Int {
return 30
}
}
post.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginBottom="5dp"
>
<ImageView
android:id="#+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
tools:src="#drawable/sunny"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Today is Sunny"
android:textSize="20sp"
android:textColor="#android:color/black"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/textView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="285dp"
android:layout_height="20dp"
tools:text="Today is sunny day lets go there"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"
app:layout_constraintHorizontal_bias="0.050" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Actions"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
First, add a constructor to your PostAdapter that accepts data and get your data into your PostAdapter, for example:
myDataList = ...; // Get data from somewhere
rv_main = PostAdapter(myDataList);
Then bind it in your onBindViewHolder:
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
holder.bindData(myDataList.get(position));
}
Also getItemCount() should return myDataList.size()
UPDATE: I added my whole code after I fixed it as I stated in my answer
hello guys I am having a weird bug. I have a calendar view that's initially set to GONE and I try to change it to VISIBLE and GONE when the user clicks on an icon.
it doesn't show.
but, when I set it initially to VISIBLE it works as expected.
I tried debugging and it enters both branches of the when statement, but it doesn't change the visibility
any idea what I am doing wrong here?
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
else -> { calendarView.visibility = View.GONE
}
}
}
XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/per_day_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/brighter_white">
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:cardBackgroundColor="#color/brighter_white"
app:cardCornerRadius="12dp"
app:cardElevation="12dp"
app:contentPadding="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/per_day_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="4"
android:background="#android:color/transparent"
android:hint="#string/search"
android:textColorHint="#color/silver"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageView11"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.6" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_azure_bg"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_search_line" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="#drawable/rounded_corner_lavendar_bg"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/calendarView"
app:layout_goneMarginTop="16dp">
<TextView
android:id="#+id/per_day_current_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="4"
android:fontFamily="#font/montserrat_medium"
android:text="TextView"
android:textColor="#color/charcoal"
android:textSize="15sp" />
<ImageView
android:id="#+id/calendar_icon"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_calendar" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:id="#+id/report_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/periwinkle"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/user_name"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_attend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/platinum"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/attend_time"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_leave"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/misty_rose"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/leave_time"
android:textColor="#color/charcoal"
android:textStyle="bold" />
<TextView
android:id="#+id/report_item_lost_points"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#color/magnolia"
android:ellipsize="end"
android:fontFamily="#font/montserrat_medium"
android:gravity="center"
android:maxLines="2"
android:paddingStart="8dp"
android:paddingLeft="8dp"
android:paddingTop="16dp"
android:paddingEnd="8dp"
android:paddingRight="8dp"
android:paddingBottom="16dp"
android:text="#string/lostPoints"
android:textColor="#color/charcoal"
android:textStyle="bold" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/report_rv"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout5" />
<CalendarView
android:id="#+id/calendarView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment code
package iti.intake40.mawgood_admin.reports.per_day
import android.app.DatePickerDialog
import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.snackbar.Snackbar
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.core.DateUtilities
import iti.intake40.mawgood_admin.models.Report
import kotlinx.android.synthetic.main.fragment_reports.*
class ReportsFragment : Fragment(), ReportPerDayContract.IView {
private val TAG = ReportsFragment::class.java.simpleName
private lateinit var presenter: ReportPerDayPresenter
private lateinit var adapter: ReportPerDayAdapter
private val reportList = ArrayList<Report>()
private var rootView: View? = null
private var mDatePickerDialog: DatePickerDialog? = null
private var per_day_current_date: TextView? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_reports, container, false)
per_day_current_date = root.findViewById(R.id.per_day_current_date)
presenter = ReportPerDayPresenter(context!!, this)
return root
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
rootView = view.findViewById(R.id.per_day_root)
presenter.getOrganizationCreationDate()
calendarView.maxDate = System.currentTimeMillis()
calendarView.visibility = View.GONE
calendar_icon.setOnClickListener {
when (calendarView.visibility) {
View.GONE -> {
calendarView.visibility = View.VISIBLE
}
View.VISIBLE -> {
calendarView.visibility = View.GONE
}
}
}
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
calendarView.visibility = View.GONE
val date = "$dayOfMonth-${month + 1}-$year"
Log.d(TAG, date)
val dayInUnix = DateUtilities.convertDateToTimestamp(date)
presenter.listenForChanges(dayInUnix.toString())
}
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onStart() {
super.onStart()
activity!!.title = "Reports"
search()
per_day_current_date?.text = DateUtilities.getTodayInDate()
val today = DateUtilities.getTodayInUnix().toString()
presenter.listenForChanges(today)
}
private fun search() {
per_day_search.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
adapter.filter.filter(s)
}
})
}
override fun onStop() {
super.onStop()
presenter.detachListeners()
}
override fun displayNoData() {
// per_day_empty_iv.visibility = View.VISIBLE
// per_day_empty_tv.visibility = View.VISIBLE
report_rv.visibility = View.INVISIBLE
}
override fun hideNoData() {
// per_day_empty_iv.visibility = View.INVISIBLE
// per_day_empty_tv.visibility = View.INVISIBLE
report_rv.visibility = View.VISIBLE
}
override fun updateRecyclerView(reports: List<Report>) {
reportList.clear()
reportList.addAll(reports)
adapter = ReportPerDayAdapter(reportList)
report_rv.setHasFixedSize(true)
report_rv.layoutManager = LinearLayoutManager(context)
report_rv.adapter = adapter
adapter.notifyDataSetChanged()
}
override fun updateDayView(date: String) {
per_day_current_date?.text = date
}
override fun showConnectionError() {
Snackbar.make(rootView!!, R.string.no_connection_show_cache, Snackbar.LENGTH_LONG)
.show()
}
override fun setCalendarStartDate(creationDateUnix: Long) {
calendarView.minDate = creationDateUnix
Log.d(TAG, creationDateUnix.toString())
}
}
it seems like a bug. I was able to fix this by removing the visibility attribute from XML.
and set the visibility programmatically in onViewCreated like this: calendarView.visibility = View.GONE
I have two controls that i can increase or decrease an integer. I click on either the first or second set of plus and minus buttons,despite being separate functions, it'll display the Integers on both outputs. For example if I click on the plus and minus button for the integer_number_1, it'll display the numbers on both the integer_number_1 and integer_number_2 outputs/TextView.
How do I fix this??? I've so many different ways and none of them worked.
Here's my xml section:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:layout_marginBottom="549dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/decrease_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<TextView
android:id="#+id/integer_number_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="16dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold" />
<Button
android:id="#+id/increase_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
</LinearLayout>
<Button
android:id="#+id/decrease_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/integer_number_2"
app:layout_constraintHorizontal_bias="0.513"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.317" />
<Button
android:id="#+id/increase_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.579"
app:layout_constraintStart_toEndOf="#+id/integer_number_2"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.313" />
<TextView
android:id="#+id/integer_number_2"
android:layout_width="46dp"
android:layout_height="99dp"
android:layout_marginStart="46dp"
android:layout_marginLeft="46dp"
android:inputType="number"
android:text="0"
android:textSize="70sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.426"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
app:layout_constraintVertical_bias="0.297" />
</androidx.constraintlayout.widget.ConstraintLayout>
And here's my kt file:
package com.example.plus_and_minus_input
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
increase_1.setOnClickListener { increaseInteger1() }
decrease_1.setOnClickListener { decreaseInteger1() }
increase_2.setOnClickListener { increaseInteger2() }
decrease_2.setOnClickListener { decreaseInteger2() }
}
fun increaseInteger1() {
display(integer_number_1.text.toString().toInt() + 1)
}
fun decreaseInteger1() {
display(integer_number_1.text.toString().toInt() - 1)
}
fun increaseInteger2() {
display(integer_number_2.text.toString().toInt() + 1)
}
fun decreaseInteger2() {
display(integer_number_2.text.toString().toInt() - 1)
}
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
}
I think you need to separate this following display methods.
private fun display(number: Int) {
integer_number_1.setText("$number")
integer_number_2.setText("$number")
}
to
private fun display_number_1(number: Int) {
integer_number_1.setText("$number")
}
and
private fun display_number_2(number: Int) {
integer_number_2.setText("$number")
}
Playing around with Android and following this simple introduction but I can't get the line resultsTextView.text = rand.toString() to trigger. Not sure what I'm missing.
The main Kotlin code looks like this (MainActivity.kt):
package io.github.ovid.randomizer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import kotlin.random.Random
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.resultsTextView)
val seekBar = findViewById<SeekBar>(R.id.seekBar)
rollButton.setOnContextClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
true
}
}
}
And my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="#+id/rollButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginLeft="96dp"
android:layout_marginEnd="96dp"
android:layout_marginRight="96dp"
android:layout_marginBottom="24dp"
android:text="Roll"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<SeekBar
android:id="#+id/seekBar"
style="#style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="24dp"
android:max="10"
android:progress="3"
app:layout_constraintBottom_toTopOf="#+id/rollButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginBottom="16dp"
android:text="How Many"
app:layout_constraintBottom_toTopOf="#+id/seekBar"
app:layout_constraintStart_toStartOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="409dp"
android:layout_height="1dp"
android:layout_marginBottom="16dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toTopOf="#+id/textView"
tools:layout_editor_absoluteX="1dp" />
<TextView
android:id="#+id/resultsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="144sp"
app:layout_constraintBottom_toTopOf="#+id/divider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Everything on the layout in the emulator looks correct, but pressing the "Roll" button shows I clicked the button, but no text shows up in the resultsTextView.
D'oh! It was setOnClickListener, not setOnContextClickListener. Made the change and removed the true at the end and it worked fine. Sorry for the noise.
Just replace the event method from setOnContextClickListener to setOnClickListener, an it will work fine. Just like this:
rollButton.setOnClickListener {
val rand = Random.nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
}