【问题标题】:Display the total amount from TextView values in a Custom Listview在自定义列表视图中显示来自 TextView 值的总量
【发布时间】:2017-05-11 15:08:41
【问题描述】:

我的应用中有一个购物车,我一直想知道如何从列表视图中的文本视图中获取值的总和,然后将其显示在另一个类中?

那个orderTotal数组就是每行产品的数量

这是我的适配器类:

public class ListCartAdapter extends BaseAdapter {


private Context context;


private ArrayList<String> orderTotal;

public ListCartAdapter(Context context, ArrayList<String> orderTotal){
    this.context = context;
    this.orderTotal = orderTotal;
}

@Override
public int getCount() {
    return orderName.size();
}

@Override
public Object getItem(int position) {
    return orderName.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

  final cartDatabaseHelper db = new cartDatabaseHelper(context);

    final View listView;
    final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.cart_list_item, null);

  TextView total = (TextView) listView.findViewById(R.id.textOrderTotal);

    total.setText(orderTotal.get(position));

    return listView;
}

这是 MainActivity 类:

//CART LISTVIEW
private ArrayList<String> orderid;
private ArrayList<String> orderName;
private ArrayList<String> orderSize;
private ArrayList<String> orderQuantity;
private ArrayList<String> orderTotal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

    //CART LISTVIEW
    orderid = new ArrayList<>();
    orderName = new ArrayList<>();
    orderSize = new ArrayList<>();
    orderQuantity = new ArrayList<>();
    orderTotal = new ArrayList<>();

    TextView textTotal = (TextView) findViewById(R.id.textOrderSumTotal);

    ListView listView = (ListView) findViewById(R.id.listView);

    ListCartAdapter adapter = new ListCartAdapter(cart.this, orderid, orderName, orderSize, orderQuantity, orderTotal);
    listView.setAdapter(adapter);

    Cursor data = db.getListContents();

    if(data.getCount() == 0){
        btnCheckout.setVisibility(View.GONE);
    }
    else
    {
        btnCheckout.setVisibility(View.VISIBLE);

        data.moveToFirst();
        do{
            orderid.add(data.getString(0));
            orderName.add(data.getString(1));
            orderSize.add(data.getString(2));
            orderQuantity.add(data.getString(3));
            orderTotal.add(data.getString(4));
        } while (data.moveToNext());
    }
    data.close();

    listView.setEmptyView(findViewById(R.id.emptyView));

}

textTotal 是我计划显示列表视图中值的总和的地方。

谁能指出我正确的方向?提前致谢!

【问题讨论】:

  • 什么是orderTotal数组列表?
  • 抱歉,我忘记解释了。 orderTotal 是listview中每个产品的数量sir
  • 总计是什么意思?是物品的总数量吗?或没有。产品?
  • 我建议你为订单创建一个 bean 类...这将是一个更好的方法...否则@kaushal28 的答案是正确的
  • 是的,没错

标签: android listview android-adapter


【解决方案1】:

如果您已将所有价格列在名为orderTotal 的数组列表中,这很简单。遍历列表并添加所有值,如下所示:

int total = 0;
for(String s : orderTotal){
   total += Integer.parseInt(s);
}

在任何你想要的地方显示这个total。如果价格是浮动的,则使用Float.parseFloat(YOUR_FLOAT_STRING);

【讨论】:

  • 先生,我有一个问题,我可以将此行添加到我的 MainActivity 类中,然后将其转换回字符串并在所需的文本视图中显示,对吧先生?还是我错了?先生,请多多包涵,我还在学习中哈哈
  • 先生,我已经使用它运行了,它返回零。但是,当我在从 SQLITE 数据库获取数据后添加该行时,它会崩溃先生。这是为什么呢?
  • 哎呀,先生。我忘了我的价格是双倍哈哈..你是对的,谢谢先生的帮助。我真的很感激!
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多