0

I have put a button inside a RecyclerView to move it to the next activity but when i clicked the button it did nothing including not moving it to the next activity.

I have tried putting the button outside the RecyclerView, but when the RecyclerView comes, the button has gone. And i have tried to modify all codes regarding the RecyclerView but without success.

This is the Activity for showing it

package com.example.rsolveapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class RsolverMatch extends AppCompatActivity {

    private DatabaseReference database;
    private RecyclerView rvView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private ArrayList<Description> daftarBarang;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.rsolvermatch);

        rvView = (RecyclerView) findViewById(R.id.rv_main);
        rvView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        rvView.setLayoutManager(layoutManager);

        database = FirebaseDatabase.getInstance().getReference();

        database.child("description").addValueEventListener(new 
     ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                daftarBarang = new ArrayList<>();
                for (DataSnapshot noteDataSnapshot : 
    dataSnapshot.getChildren()) {

                    Description description = 
    noteDataSnapshot.getValue(Description.class);
                    description.setKey(noteDataSnapshot.getKey());

                    daftarBarang.add(description);
                }


                adapter = new AdapterDescRecyclerView(daftarBarang, 
    RsolverMatch.this);
                rvView.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

                System.out.println(databaseError.getDetails() + " " + 
    databaseError.getMessage());
            }
        });
    }

    public static Intent getActIntent(Activity activity){
        return new Intent(activity, RsolverMatch.class);
    }
}

This is the first .xml layout for the button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip"
    android:background="@drawable/background1">

    <android.support.v7.widget.CardView 
        android:id="@+id/cv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:padding="10dp"
        android:layout_gravity="center_horizontal"
        card_view:cardBackgroundColor="@color/background_material_light"
        card_view:cardCornerRadius="3dp"
        card_view:cardElevation="2.5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_margin="8dp"
                    android:id="@+id/descText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Description : "
                    android:textColor="@color/black"
                    android:textSize="16sp"/>


                <TextView
                    android:layout_margin="8dp"
                    android:id="@+id/tv_namabarang"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_margin="8dp"
                    android:id="@+id/dayText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Working Day :"
                    android:textColor="@color/black"
                    android:textSize="16sp"/>

                <TextView
                    android:layout_margin="8dp"
                    android:id="@+id/tv_day"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/black"
                    android:textSize="16sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_margin="8dp"
                        android:id="@+id/daysHour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Start Working Hour : "
                        android:textColor="@color/black"
                        android:textSize="16sp"/>

                    <TextView
                        android:layout_marginTop="8dp"
                        android:id="@+id/tv_shour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/black"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_marginTop="8dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=":"
                        android:textColor="@color/black"
                        android:textSize="16sp"/>

                    <TextView
                        android:layout_marginTop="8dp"
                        android:id="@+id/tv_smin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/black"
                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_margin="8dp"
                        android:id="@+id/dayeHour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="End Working Hour : "
                        android:textColor="@color/black"
                        android:textSize="16sp"/>

                    <TextView
                        android:layout_marginTop="8dp"
                        android:id="@+id/tv_ehour"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/black"
                        android:textSize="16sp" />

                    <TextView
                        android:layout_marginTop="8dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text=":"
                        android:textColor="@color/black"
                        android:textSize="16sp"/>

                    <TextView
                        android:layout_marginTop="8dp"
                        android:id="@+id/tv_emin"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@android:color/black"
                        android:textSize="16sp" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

    <Button
        android:id="@+id/takeJob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Take Job"
        android:background="@color/green"
        android:layout_marginTop="10dp"/>

</LinearLayout>

This is the second .xml layout for the button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"/>

</LinearLayout>

The expected result is the button can be clicked and it moves to the next activity. Here is the error messages in the logcat :

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.rsolveapp, PID: 13737 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rsolveapp/com.example.rsolveapp.RsolverMatch}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.rsolveapp.RsolverMatch.onCreate(RsolverMatch.java:56) at android.app.Activity.performCreate(Activity.java:7140) at android.app.Activity.performCreate(Activity.java:7131) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6692)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Here is my AdapterRecyclerView Activity

public class AdapterDescRecyclerView extends 
RecyclerView.Adapter<AdapterDescRecyclerView.ViewHolder> {

private ArrayList<Description> daftarBarang;
private Context context;

public AdapterDescRecyclerView(ArrayList<Description> barangs, Context ctx){

    daftarBarang = barangs;
    context = ctx;
}

class ViewHolder extends RecyclerView.ViewHolder {


    TextView tvTitle;
    TextView tvday;
    TextView tvshour;
    TextView tvsmin;
    TextView tvehour;
    TextView tvemin;
    CardView cvMain;

    ViewHolder(View v) {
        super(v);
        tvTitle = (TextView) v.findViewById(R.id.tv_namabarang);
        tvday = (TextView) v.findViewById(R.id.tv_day);
        tvshour = (TextView) v.findViewById(R.id.tv_shour);
        tvsmin = (TextView) v.findViewById(R.id.tv_smin);
        tvehour = (TextView) v.findViewById(R.id.tv_ehour);
        tvemin = (TextView) v.findViewById(R.id.tv_emin);
        cvMain = (CardView) v.findViewById(R.id.cv_main);
    }
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = 
  LayoutInflater.from(parent.getContext()).inflate(R.layout.item_job, 
  parent, false);
    // mengeset ukuran view, margin, padding, dan parameter layout lainnya
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    final String name = daftarBarang.get(position).getDescription();
    final String day = daftarBarang.get(position).getDay();
    final String shour = daftarBarang.get(position).getsHour();
    final String smin = daftarBarang.get(position).getsMin();
    final String ehour = daftarBarang.get(position).geteHour();
    final String emin = daftarBarang.get(position).geteMin();


    holder.tvTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


        }
    });
    holder.tvTitle.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {

            return true;
        }
    });
    holder.tvTitle.setText(name);
    holder.tvday.setText(day);
    holder.tvshour.setText(shour);
    holder.tvsmin.setText(smin);
    holder.tvehour.setText(ehour);
    holder.tvemin.setText(emin);


}



@Override
public int getItemCount() {

    return daftarBarang.size();
}
}

This is my Rsolvermatch Activity that already impelements the onclicklistener but resulting in an error

package com.example.rsolveapp;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;




public class RsolverMatch extends AppCompatActivity implements 
View.OnClickListener {


private DatabaseReference database;
private RecyclerView rvView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Description> daftarBarang;
Button takeJob;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.rsolvermatch);


    rvView = (RecyclerView) findViewById(R.id.rv_main);
    rvView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    rvView.setLayoutManager(layoutManager);
    takeJob.setOnClickListener(this);
    takeJob = findViewById(R.id.takeJob);

    database = FirebaseDatabase.getInstance().getReference();



    database.child("description").addValueEventListener(new 
    ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


            daftarBarang = new ArrayList<>();
            for (DataSnapshot noteDataSnapshot : dataSnapshot.getChildren()) 
                {

                Description description = 
                noteDataSnapshot.getValue(Description.class);
                description.setKey(noteDataSnapshot.getKey());


                daftarBarang.add(description);
            }

            adapter = new AdapterDescRecyclerView(daftarBarang, 
            RsolverMatch.this);
            rvView.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            System.out.println(databaseError.getDetails() + " " + 
            databaseError.getMessage());
        }
    });
}


@Override
public void onClick(View v) {
    if (v == takeJob) {
        Intent intent = new Intent(RsolverMatch.this, Chatting.class);
        startActivity(intent);
        }
    }

        public static Intent getActIntent (Activity activity){
            return new Intent(activity, RsolverMatch.class);
        }
    }
Bryan Ratin
  • 11
  • 1
  • 4

2 Answers2

2

You should put setOnClickListener for your button inside the Adapter's onBindViewHolder function. And use the position as viewHolder.getAdapterPosition(); when you click on button of any item in recyclerview.Also pass whatever arguments you need for doing startActivity(); in the adapter's Constructor.

  • I was trying to reference a button from a different layout but i don't know how to do it, i already tried putting onClickListener from my rsolvermatch activity referencing to the itemjob.xml. So after i got the error from that i removed the onclick listener. – Bryan Ratin Sep 27 '19 at 02:44
  • i added the Rsolvermatch activity which included the onclicklistener that resulting an error i added before – Bryan Ratin Sep 27 '19 at 03:41
  • @BryanRatin You can set click listener inside you view holder class also. If you want to set click listener for full item than set it on view comes to your viewHolder class constructor. – Bhaumik Ghodasara Oct 01 '19 at 09:51
0

you are doing it right.

Now just add a button in layout item_job.xml and do

   holder.myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    Intent intent = new Intent (context, NextActivity.class);
    intent.putExtra("argumentKey", argumentValue);       
    context.startActivity(intent);
    }
});

in onBindViewHolder

Makarand
  • 983
  • 9
  • 27