RecyclerView获取所有TextView字符串并进行总和并在字符串中设置总和

时间:2018-07-18 16:34:49

标签: java android firebase android-studio

public class CartActivityAdapter extends RecyclerView.Adapter<CartActivityAdapter.CartViewHolder> {

private List<Cart> cartList;
private OnClickItemFeed onClickItemFeed;

public CartActivityAdapter(List<Cart> cartList, OnClickItemFeed onClickItemFeed) {

    this.cartList = cartList;
    this.onClickItemFeed = onClickItemFeed;

}

@NonNull
@Override
public CartActivityAdapter.CartViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {

    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_cart_adapter, viewGroup, false);
    return new CartActivityAdapter.CartViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull CartActivityAdapter.CartViewHolder categoryViewHolder, int position) {

    Cart cart = cartList.get(position);
    categoryViewHolder.setImageShapeSquare(cart.getCartImage());
    categoryViewHolder.setTextView1(cart.getCartName());
    categoryViewHolder.setTextView2(cart.getCartModel());
    categoryViewHolder.setTextView3(cart.getCartPrice());

}

@Override
public int getItemCount() {

    return cartList == null ? 0 : cartList.size();

}

public class CartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private ImageShapeSquare imageShapeSquare;
    private TextView textView1, textView2, textView3;
    private Button button;

    public CartViewHolder(View view) {

        super(view);
        imageShapeSquare = view.findViewById(R.id.cart_adapter_image_square_square);
        textView1 = view.findViewById(R.id.cart_adapter_text_view_1);
        textView2 = view.findViewById(R.id.cart_adapter_text_view_2);
        textView3 = view.findViewById(R.id.cart_adapter_text_view_3);
        button = view.findViewById(R.id.cart_adapter_button);
        button.setOnClickListener(this);

    }

    public void setImageShapeSquare(String string) {

        if (imageShapeSquare == null) {

            return;

        }

        new ImageLoader(imageShapeSquare.getContext()).fromNetwork(string, SaveLocation.Cache).to(imageShapeSquare);

    }

    public void setTextView1(String string) {

        if (textView1 == null) {

            return;

        }

        textView1.setText(string);

    }

    public void setTextView2(String string) {

        if (textView2 == null) {

            return;

        }

        textView2.setText(string);

    }

    public void setTextView3(String string) {

        if (textView3 == null) {

            return;

        }

        textView3.setText("₹ " + string);

    }

    @Override
    public void onClick(View view) {

        if (onClickItemFeed != null) {

            onClickItemFeed.onClickItemFeed(getAdapterPosition(), view);

        }

    }

}

public interface OnClickItemFeed {

    void onClickItemFeed(int position, View view);

}

}

这是我的视图持有者,我想从TextView 3中获取所有文本并添加值并以字符串形式获取它们

这是活动文件

public class CartActivity extends AppCompatActivity implements CartActivityAdapter.OnClickItemFeed {

    private RecyclerView cart_recycler_view;
    private List<Cart> cartList;

    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference();
        cart_recycler_view = findViewById(R.id.cart_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CartActivity.this);
        cart_recycler_view.setLayoutManager(linearLayoutManager);
        cart_recycler_view.setHasFixedSize(true);
        TextView cart_text_view = findViewById(R.id.cart_text_view);
        Button cart_button = findViewById(R.id.cart_button);

    }

    private void cartRecyclerView(List<Cart> cartList) {

        CartActivityAdapter cartActivityAdapter = new CartActivityAdapter(cartList, CartActivity.this);
        cart_recycler_view.setAdapter(cartActivityAdapter);

    }

    @Override
    protected void onResume() {

        super.onResume();
        cartList = new ArrayList<>();

        databaseReference.child("Cart").child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).getRef().addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                cartList.clear();

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    Cart cart = snapshot.getValue(Cart.class);
                    cartList.add(cart);

                }

                cartRecyclerView(cartList);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }

        });

    }


    @Override
    public void onClickItemFeed(int position, View view) {

        Cart cart = cartList.get(position);

        switch (view.getId()) {

            case R.id.cart_adapter_button:
                String stringSKU;
                stringSKU = cart.getCartSKU();
                databaseReference.child("Cart").child(Objects.requireNonNull(FirebaseAuth.getInstance().getUid())).child(stringSKU).removeValue();
                break;

        }

    }

}

1 个答案:

答案 0 :(得分:0)

您需要的所有值都存储在适配器中,如我所见,该适配器为List<Cart>。总结一下:

double sum = 0.0;
double value = 0.0;
for(int i = 0; i < cartActivityAdapter.size(); i++)
    try {
        value = Double.parseDouble(cartActivityAdapter.get(i).getCartPrice());
    } catch (NumberFormatException e) {
        value = 0.0
    }
    sum += value;
}

sum现在拥有总数

如果要将总和放入TextView中:

textView.setText("" + sum);