在 RecyclerView 中单击按钮
Button click in RecyclerView
该按钮应打开一个新的 activity,并且应仅显示其按钮已被单击的特定卡片的信息
我在 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>
ItemHolder.bookButton.setTag(model);
并在 onClick 内部
Listitem model= (Listitem) v.getTag();
然后从模型中传递所需的细节并放入 Bundle 中,将其添加到您正在启动的互联网中
我建议你看看我的项目库:Android Firebase Clickable Cards
它完全符合您的描述。
但是,如果您只是在将对象从一个 activity 发送到另一个时遇到问题,那么最好的方法是使用 Parceables。
首先使您的对象可打包:
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.putExtra("listitem", listitem);
从预订中检索对象 class:
Listitem listitem = (Listitem) getIntent().getParcelable("listitem");
您必须在 intent
中传递要在新 activity 中显示的内容。所以你的代码应该是这样的。
`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);`
然后在你的新 activity 中做这样的事情
String docName = getIntent().getStringExtra("docName")
等。注意 "extra type" 应该与您发送的额外类型相匹配。
首先你需要让你的模型class可序列化...
public class Listitem implements Serializable { ..... }
像这样改变你的意图..
Intent i = new Intent(index.this, Booking.class);
i.putExtra("data",model);
startActivity(i);
在Booking.class
活动onCreate()
方法
Listitem model;
Intent extra = getIntent();
if (extra != null) {
model = (Listitem) extra.getSerializableExtra("data");
}
textview.setText(model.getDocName);
............
............
您只需对代码进行少量更改即可获得所需的结果。如果您还想要视图的位置,那么您需要 getAdapterPosition() 来获取您选择的当前 CardView 的位置。
所以你的 ViewHolder class 应该像-
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);
bookButton.setOnClickListener(this); // change number 1
}
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) {
int position = getAdapterPosition(); // change number 2 if you need position for further work.
switch(view.getId()){
case R.id.bookButton:
// do your other code here
break:
}
}
}
}
并用于在其他 Activity 上接收数据。你必须让你的模型 class Serializable 如@Abhishek Singh 所回答。
该按钮应打开一个新的 activity,并且应仅显示其按钮已被单击的特定卡片的信息
我在 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>
ItemHolder.bookButton.setTag(model);
并在 onClick 内部
Listitem model= (Listitem) v.getTag();
然后从模型中传递所需的细节并放入 Bundle 中,将其添加到您正在启动的互联网中
我建议你看看我的项目库:Android Firebase Clickable Cards
它完全符合您的描述。
但是,如果您只是在将对象从一个 activity 发送到另一个时遇到问题,那么最好的方法是使用 Parceables。
首先使您的对象可打包:
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.putExtra("listitem", listitem);
从预订中检索对象 class:
Listitem listitem = (Listitem) getIntent().getParcelable("listitem");
您必须在 intent
中传递要在新 activity 中显示的内容。所以你的代码应该是这样的。
`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);`
然后在你的新 activity 中做这样的事情
String docName = getIntent().getStringExtra("docName")
等。注意 "extra type" 应该与您发送的额外类型相匹配。
首先你需要让你的模型class可序列化...
public class Listitem implements Serializable { ..... }
像这样改变你的意图..
Intent i = new Intent(index.this, Booking.class);
i.putExtra("data",model);
startActivity(i);
在Booking.class
活动onCreate()
方法
Listitem model;
Intent extra = getIntent();
if (extra != null) {
model = (Listitem) extra.getSerializableExtra("data");
}
textview.setText(model.getDocName);
............
............
您只需对代码进行少量更改即可获得所需的结果。如果您还想要视图的位置,那么您需要 getAdapterPosition() 来获取您选择的当前 CardView 的位置。
所以你的 ViewHolder class 应该像-
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);
bookButton.setOnClickListener(this); // change number 1
}
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) {
int position = getAdapterPosition(); // change number 2 if you need position for further work.
switch(view.getId()){
case R.id.bookButton:
// do your other code here
break:
}
}
}
}
并用于在其他 Activity 上接收数据。你必须让你的模型 class Serializable 如@Abhishek Singh 所回答。