I want an activity/class to run on a Button
click. The Button
is inside my RecyclerView
. I first had the code in my MainActivity
with:
btnComplete.setOnClickListener {
startActivity(Intent(this@MainActivity, DelComplete::class.java))}
This code was in my onCreate
of my MainActivity
. How ever on running the code I found the error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
My first thought was that it could not reach the Button
since it was in the RecyclerView
. So I tried to put the code in my RecyclerView
. But I read that it is not a good idea to put it in you RecyclerView
and I dont know what to do with the Context
as it keeps throwing errors.
RecyclerView
:
txtbutton1.setOnClickListener {
confirmdel()
modal.tvdone = "Delivered"
Log.e("Clicked", "Successful delivery")
//this is where I add code to export data through api
modal.state = DataState.Success
Status = 1
notifyDataSetChanged()}
private fun confirmdel() {
startActivity(Intent(this, DelComplete::class.java))}
It might be as simple as filling in the Context
but I am unsure what the Context
must be to test that theory.
Recyclerview- table_list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@ id/txtWOrder"
android:layout_width="160dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:text="@string/worder"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<TextView
android:id="@ id/txtDElNote"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:text="@string/delnote"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<TextView
android:id="@ id/txtCompany"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:text="@string/company"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
<Button
android:id="@ id/btnComplete"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_header_cell_bg"
android:drawableTint="#70D5C8"
android:text="@string/button1"
android:textSize="18sp" />
<Button
android:id="@ id/btnException"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:text="@string/button2"
android:textSize="18sp" />
<TextView
android:id="@ id/txttvdone"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@drawable/table_content_cell_bg"
android:foregroundGravity="center_horizontal"
android:text=""
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="@ id/txtWeight"
android:layout_width="190dp"
android:layout_height="match_parent"
android:layout_gravity="top"
android:background="@drawable/table_content_cell_bg"
android:text="@string/weight"
android:textSize="18sp"
tools:ignore="TextContrastCheck" />
</LinearLayout>
The reason why I have a Button
in my RecyclerView
is on every entry a driver should be able to give feedback on a delivery. Every entry is a different order. Did I go the wrong way of doing this?
My Button does the following:
txtbutton1.setOnClickListener {
confirmdel()
modal.tvdone = "Delivered"
Log.e("Clicked", "Successful delivery")
//this is where I add code to export data through api
modal.state = DataState.Success
Status = 1
notifyDataSetChanged()
}
It does that with no problem. Now however I want to add an AlertDialog
and update data in my DB
with the setOnClickListener
. Will this be possible from within my adapter?
CodePudding user response:
You shouldn't put a button inside of your RecyclerView, unless you are referring to a button as the item itself. RecyclerViews are used to render as much as items as you have in your data source, instead of hardcoding them, so I think you want to handle the click on a single item and navigate to another Activity with the data of that item.
You can do that by following these steps. This is a codelab made by Google and explains how to handle the item click.
In addition, you should do those too, in order to better understanding.RecyclerViews
CodePudding user response:
You can use the context like this :
class Adapter(private val context: Context, private val myList: List<String>) :
RecyclerView.Adapter<Adapter.ViewHolder>()
And pass the context where required like this
private fun confirmdel() {
startActivity(Intent(context, DelComplete::class.java))}