[英]How to pass Recycler View Items into one TextView
我正在尝试在RecyclerView中获取TextView值项。 我的RecyclerView布局文件有两个TextView,一个是“产品名称”,另一个是“数量”,您可以在此处看到
===这是我的cart_items_layout_item ===
<TextView android:id="@+id/cart_list_product_name" android:layout_width="160dp" android:layout_height="wrap_content" android:text="Product Name" android:textColor="@color/colorPrimary" android:textSize="16sp" android:layout_marginLeft="5dp" android:textStyle="bold"/> <TextView android:id="@+id/cart_list_product_quantity" android:layout_width="160dp" android:layout_height="wrap_content" android:text="Product Quantity" android:textColor="@color/colorPrimary" android:textSize="16sp" android:layout_marginRight="5dp" android:layout_alignParentRight="true" android:gravity="end" android:textStyle="bold"/>
这是我的activity_cart xml
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/cart_list" android:layout_width="match_parent" android:layout_height="542dp" android:layout_above="@+id/next" android:layout_below="@id/header_color" android:layout_marginBottom="113dp"> </androidx.recyclerview.widget.RecyclerView> <Button android:id="@+id/next" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/button" android:text="CONFIRM ORDER/S" android:textColor="@color/colorPrimaryDark" android:layout_margin="10dp" /> <Button android:id="@+id/sendSMSButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginStart="0dp" android:layout_marginLeft="0dp" android:layout_marginTop="10dp" android:layout_marginEnd="0dp" android:layout_marginRight="0dp" android:layout_marginBottom="69dp" android:background="@drawable/button" android:text="Send" android:textColor="@color/colorPrimaryDark" /> <TextView android:id="@+id/smsphoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/cart_list" android:text="0918"/>
这是我的RecyclerView适配器类
@Override
protected void onStart() {
super.onStart();
final DatabaseReference cartListRef =
FirebaseDatabase.getInstance().getReference().child("Cart List");
FirebaseRecyclerOptions<Cart> options =
new FirebaseRecyclerOptions.Builder<Cart>()
.setQuery(cartListRef.child("User View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products"), Cart.class)
.build();
FirebaseRecyclerAdapter<Cart, CartViewHolder> adapter =
new FirebaseRecyclerAdapter<Cart, CartViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull CartViewHolder
holder, int i, @NonNull final Cart model) {
holder.txtProductName.setText(model.getProductName());
holder.txtProductQuantity.setText("Quantity = " +
model.getQuantity());
holder.itemView.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view)
{
CharSequence options[] = new CharSequence[]
{
"Edit",
"Removed"
};
AlertDialog.Builder builder = new
AlertDialog.Builder(CartActivity.this);
builder.setTitle("Cart Options");
builder.setItems(options, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface
dialogInterface, int i)
{
if (i == 0)
{
Intent intent = new
Intent(CartActivity.this, ProductDetailsActivity.class);
intent.putExtra("pid",model.getPid());
startActivity(intent);
}
if(i == 1)
{
cartListRef.child("User View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products")
.child(model.getPid())
.removeValue()
.addOnCompleteListener(new
OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull
Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CartActivity.this, "Item removed successfully",
Toast.LENGTH_SHORT).show();
// Intent intent = new
Intent(CartActivity.this, Home.class);
//
startActivity(intent);
}
}
});
}
if(i == 1)
{
cartListRef.child("Admin View")
.child(Prevalent.CurrentOnlineUsers.getPhone())
.child("Products")
.child(model.getPid())
.removeValue()
.addOnCompleteListener(new
OnCompleteListener<Void>() {
@Override
public void
onComplete(@NonNull Task<Void> task)
{
if (task.isSuccessful())
{
Toast.makeText(CartActivity.this, "Item removed successfully",
Toast.LENGTH_SHORT).show();
// Intent intent =
new Intent(CartActivity.this, Home.class);
//
startActivity(intent);
}
}
});
}
}
});
builder.show();
}
});
}
@NonNull
@Override
public CartViewHolder onCreateViewHolder(@NonNull ViewGroup
parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout,
parent, false);
CartViewHolder holder = new CartViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
===这是我的购物车类别/型号===
public class Cart
{
private String
date,discount,pid,productDesc,productName,productSupplier,quantity,time;
public Cart()
{
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDiscount() {
return discount;
}
public void setDiscount(String discount) {
this.discount = discount;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductSupplier() {
return productSupplier;
}
public void setProductSupplier(String productSupplier) {
this.productSupplier = productSupplier;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Cart(String date, String discount, String pid, String productDesc,
String productName, String productSupplier, String quantity, String time)
{
this.date = date;
this.discount = discount;
this.pid = pid;
this.productDesc = productDesc;
this.productName = productName;
this.productSupplier = productSupplier;
this.quantity = quantity;
this.time = time;
}
=== CartViewHolder ===
public class CartViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
{
public TextView txtProductName, txtProductQuantity;
private ItemClickListener itemClickListener;
public CartViewHolder(@NonNull View itemView) {
super(itemView);
txtProductName = itemView.findViewById(R.id.cart_list_product_name);
txtProductQuantity =
itemView.findViewById(R.id.cart_list_product_quantity);
}
@Override
public void onClick(View view)
{
itemClickListener.onClick(view, getAdapterPosition(), false);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
}
如您在该图中所看到的,我使用购物车类/模型将数据传递给他们,然后创建了另一个名为CartViewHolder的类,我使用该类将数据设置到Recycler View中。 我想获取此值并将其放在消息中,特别是在SMS中。 我不知道如何通过它尝试了几个代码。
private void sendSmsBySIntent ()
{
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
//Want to Intent the Items in Here the Product Name and the Quantity
smsSIntent.putExtra("PUT THE ITEMS HERE");
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
you can use your List<Cart> cart model list item to send product and quantity to
sms.
private void sendSmsBySIntent (Cart model)
{
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsSIntent.putExtra("pName",model.getProductName());
smsSIntent.putExtra("pQty",model.getQuantity());
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
您需要发送到适配器的列表。 将其作为参数发送到sendSmsBySIntent()
函数,并在创建String的地方进行for循环。
即:
private void sendSmsBySIntent (List<Cart> yourList){
StringBuilder extra = new StringBuilder();
for (int i = 0; i < yourList.size(); i++) {
//Build the string the way you want
extra.append(yourList.get(i).getName()).append(" ").append(yourList.get(i).getQuantity);
}
//Want to set specific Phone Number here
Uri uri = Uri.parse("SET DEFAULT VALUE FOR THIS");
Intent smsSIntent = new Intent(Intent.ACTION_SENDTO, uri);
//Want to Intent the Items in Here the Product Name and the Quantity
smsSIntent.putExtra(extra.toString());
try{
startActivity(smsSIntent);
} catch (Exception ex) {
Toast.makeText(CartActivity.this, "Your sms has failed...",
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
编辑:
我改变了()
里面的函数,检查一下。 现在,当您调用函数时,必须传递正在使用的列表。 像这样: sendSmsBySIntent(placeHereYourCartList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.