簡體   English   中英

如何將Recycler View項目傳遞到一個TextView中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM