无法单击或选择RecyclerView中的按钮

时间:2019-09-26 12:10:12

标签: android firebase firebase-realtime-database

我在RecyclerView内放置了一个按钮,将其移至下一个活动,但是当我单击该按钮时,它没有执行任何操作,包括不将其移至下一个活动。

我尝试将按钮放在RecyclerView之外,但是当RecyclerView出现时,按钮消失了。而且我尝试修改有关RecyclerView的所有代码,但没有成功。

这是显示它的活动

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);
    }
}

这是按钮的第一个.xml布局

<?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>

这是按钮的第二个.xml布局

<?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>

预期结果是可以单击该按钮并将其移至下一个活动。 这是logcat中的错误消息:

  

E / Android运行时:致命异常:主要       流程:com.example.rsolveapp,PID:13737       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.rsolveapp / com.example.rsolveapp.RsolverMatch}:   java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'   在空对象引用上           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1809)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6692)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        原因:java.lang.NullPointerException:尝试调用虚拟方法'void   android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'   在空对象引用上           在com.example.rsolveapp.RsolverMatch.onCreate(RsolverMatch.java:56)           在android.app.Activity.performCreate(Activity.java:7140)           在android.app.Activity.performCreate(Activity.java:7131)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1809)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6692)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的AdapterRecyclerView活动

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();
}
}

这是我的Rsolvermatch活动,已经激活了onclicklistener,但导致错误

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);
        }
    }

2 个答案:

答案 0 :(得分:2)

您应将 setOnClickListener 用作按钮的适配器的 onBindViewHolder 函数中。并在recyclerview中单击任何项​​目的按钮时,将位置用作 viewHolder.getAdapterPosition(); 。此外,还应将执行 startActivity(); 所需的任何参数传递给适配器的构造函数。

答案 1 :(得分:0)

您做对了。

现在只需在布局item_job.xml中添加一个按钮并执行

   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);
    }
});

onBindViewHolder