单击RecyclerView中的按钮

时间:2017-06-16 10:00:15

标签: android android-recyclerview buttonclick

按钮应该会打开一个新活动,并且应该只显示其按钮被点击的特定卡片的信息

我在RecyclerView中有6张卡,我也从Firebase获取数据

我的RecyclerView代码:

  public class index extends AppCompatActivity  {

  private Toolbar toolbar;
  private RecyclerView recyclerView;
  private DatabaseReference myRef;

  private FirebaseAuth firebaseAuth;

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.index);

    firebaseAuth = FirebaseAuth.getInstance();
    myRef = FirebaseDatabase.getInstance().getReference().child("/Doctor");

    if(firebaseAuth.getCurrentUser() == null)
    {
        startActivity(new Intent(this, MainActivity.class));
    }

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    FirebaseRecyclerAdapter<Listitem,ListItemHolder> adapter = new 
FirebaseRecyclerAdapter<Listitem, ListItemHolder>(
            Listitem.class,
            R.layout.list_item,
            ListItemHolder.class,
            myRef
    ) {
        @Override
        protected void populateViewHolder(ListItemHolder ItemHolder, 
Listitem model, int position) {
            ItemHolder.setDocName(model.getDocName());
            ItemHolder.setSpeciality(model.getSpeciality());
            ItemHolder.setAddress(model.getAddress());
            ItemHolder.setExperience(model.getExperience());
            ItemHolder.setFees(model.getFees());

            ItemHolder.bookButton.setOnClickListener(new 
View.OnClickListener(){
                public void onClick(View v){
                    Intent i = new Intent(index.this, Booking.class);
                    startActivity(i);

                }
            });
        }
    };

    recyclerView.setAdapter(adapter);


  }
    public static class ListItemHolder extends RecyclerView.ViewHolder 
 implements View.OnClickListener  {
        TextView head,desc,desc1,desc2,desc3;
        Button bookButton;

        public ListItemHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            head = (TextView) itemView.findViewById(R.id.textViewHead);
            desc = (TextView) itemView.findViewById(R.id.textViewDesc);
            desc1 = (TextView) itemView.findViewById(R.id.textViewDesc1);
            desc2 = (TextView) itemView.findViewById(R.id.textViewDesc2);
            desc3 = (TextView) itemView.findViewById(R.id.textViewDesc3);

            this.bookButton = (Button) 
itemView.findViewById(R.id.bookButton);
        }

        public void setDocName(String DocName) {
            head.setText(DocName);
        }

        public void setSpeciality(String Speciality) {
            desc.setText(Speciality);
        }

        public void setAddress(String Address) {
            desc1.setText(Address);
        }

        public void setExperience(String Experience) {
            desc2.setText(Experience);
        }

        public void setFees(String Fees) {
            desc3.setText(Fees);
        }

        @Override
        public void onClick(View view) {

        }
    }
}

CardLayout 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="@color/greyshade"
android:fitsSystemWindows="true">

   <android.support.v7.widget.CardView
    android:id="@+id/cardView"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">

     <LinearLayout
        android:padding="@dimen/cardview_default_radius"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        >

        <TextView
            android:id="@+id/textViewHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Heading"
            android:textStyle="bold"
            android:textColor="@color/black"
            android:textSize="@dimen/text_size"
            />
    <TextView

    android:id="@+id/textViewDesc"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="@string/Desc"/>

        <TextView
            android:id="@+id/textViewDesc1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Desc"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
        <TextView
            android:id="@+id/textViewDesc2"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textColor="@color/black"
            android:maxLines="1"
            android:textSize="12sp"
            android:text="@string/Desc"/>

        <TextView
            android:id="@+id/textViewDesc3"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:layout_marginTop="10dp"
            android:layout_marginStart="5dp"
            android:textSize="12sp"
            android:maxLines="1"

            android:text="@string/Desc"/>

            <android.support.v7.widget.AppCompatButton
                android:id="@+id/bookButton"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:text="@string/Book"
                android:textColor="@color/textbutton"
                android:textStyle="bold"
                android:backgroundTint="@color/bookbutton"
                android:layout_marginStart="130dp"
                />


        </LinearLayout>

</LinearLayout>

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

</LinearLayout>

更新

<?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="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewHead"
android:text="@string/Heading"
android:layout_marginTop="30dp"
android:textStyle="bold"
android:textColor="@color/black"/>

   <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textViewDesc1"
    android:text="@string/Desc"
    android:layout_marginTop="20dp"
    android:textStyle="bold" />

  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="bottom">
  <android.support.v7.widget.AppCompatButton
    android:id="@+id/logout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Logout"
    android:layout_marginBottom="45dp"/>
    </RelativeLayout>
  </LinearLayout>

5 个答案:

答案 0 :(得分:1)

我建议您查看我的项目存储库:Android Firebase Clickable Cards

它完全符合您的描述。

但是,如果您在将对象从一个活动发送到另一个活动时遇到问题,那么最好的方法是使用Parcelables。

首先让你的对象成为可能的:

public class Listitem implements Parcelable {

    private String docName, address, ...;

    public Listitem() {
    }

    public Listitem(String name, String address) {
        this.docName = name;
        this.address = address;
        ...
    }

    public String getDocName() {
        return docName;
    }

    public void setDocName(String name) {
        this.docName = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    ...

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.docName);
        dest.writeString(this.address);
        ...
    }

    protected Listitem(Parcel in) {
        this.docName = in.readString();
        this.address = in.readString();
        ...
    }

    public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() {
        @Override
        public Listitem createFromParcel(Parcel source) {
            return new Listitem(source);
        }

        @Override
        public Listitem[] newArray(int size) {
            return new Listitem[size];
        }
    };
}

现在发送对象Listitem,只需将其添加到intent:

intent.putExtra("listitem", listitem);

从Booking类中检索对象:

Listitem listitem = (Listitem) getIntent().getParcelable("listitem");

答案 1 :(得分:1)

您需要对代码进行一些更改才能获得所需的结果。如果您还想要视图的位置而不是需要 getAdapterPosition()来获取您选择的当前 CardView 的位置。

所以 ViewHolder 类应该像 -

try{
   //Some code to tryout
}
catch(Exception e){
   //Catch if there is an exception
}
//SNIPPET1 that is always executed

用于接收其他活动的数据。你需要让@Abhishek Singh回答你的模特课 Serializable

答案 2 :(得分:0)

ItemHolder.bookButton.setTag(model);

在onClcik内部

Listitem model= (Listitem) v.getTag();

然后从模型传递所需的细节并放入Bundle,将其添加到您正在开始的意图

答案 3 :(得分:0)

您必须传递intent您要在新活动中展示的内容。所以你的代码应该是这样的。

`Intent i = new Intent(index.this, Booking.class);
 i.putExtra("docName", model.getDocName());
 i.putExtra("spaecialty", model.getSpeciality());
 i.putExtra("address", model.getAddress());
 i.putExtra("experience", model.getExperience());
 i.putExtra("fees", model.getFees());
 startActivity(i);`

并在您的新活动中执行类似此操作

String docName = getIntent().getStringExtra("docName")等。请注意&#34;额外类型&#34;应该与您发送的额外类型相匹配。

答案 4 :(得分:0)

首先,您需要使您的模型类可序列化......

public class Listitem implements Serializable { ..... }

像这样改变你的意图..

Intent i = new Intent(index.this, Booking.class);
i.putExtra("data",model);
startActivity(i);

开启Booking.class Actvity onCreate()方法

Listitem model;

Intent extra = getIntent();
if (extra != null) {
     model = (Listitem) extra.getSerializableExtra("data");
}
textview.setText(model.getDocName);
............
............