Android listview simpleAdapter 获取每行最后一列的总和 Textview

Android listview simpleAdapter get the sum of each Row's last column Textview

我有 3 个名称、数量和价格数组。

我使用 Listview 通过 SimpleAdapter 显示它们。最重要的是,我每行有 2 个按钮,用于控制数量 - 加号和减号。

单击“加号”或“减号”按钮时,数量会发生变化,该行的小计也会更新。

如果数量=1,点击减号按钮时,会保持1;

到目前为止所有功能都正常工作。

Java

int cal_quantity;

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

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

final String name[]={"apple","orange","pear"};
final String quantity[]={"1","2","3"};
final String price[]={"5","10","2"};

for(int i=0;i<name.length;i++){
    HashMap<String, String> map = new HashMap<>();
    map.put("name",name[i]);
    map.put("quantity",quantity[i]);
    map.put("price",price[i]);
    aList.add(map);
}

String[] from = {"name","quantity","price"};
int[] to = {R.id.name,R.id.quantity,R.id.price};

SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.main7, from, to){
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = super.getView(position, convertView, parent);
        final TextView tv_quantity=(TextView)v.findViewById(R.id.quantity);
        final TextView tv_price=(TextView)v.findViewById(R.id.price);
        final TextView tv_total=(TextView)v.findViewById(R.id.total);

        final int get_quantity = Integer.parseInt(tv_quantity.getText().toString());
        final double get_price= Double.parseDouble(tv_price.getText().toString());
        final double get_total=get_quantity*get_price;
        tv_total.setText(get_total+"");

        Button minus=(Button)v.findViewById(R.id.minus);
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cal_quantity=Integer.parseInt(tv_quantity.getText().toString());
                if(cal_quantity!=1){
                    cal_quantity=cal_quantity-1;
                }
                tv_quantity.setText(cal_quantity+"");
                double get_total=cal_quantity*get_price;
                tv_total.setText(get_total+"");
            }
        });

        Button plus=(Button)v.findViewById(R.id.plus);
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cal_quantity=Integer.parseInt(tv_quantity.getText().toString());
                cal_quantity=cal_quantity+1;
                tv_quantity.setText(cal_quantity+"");
                double get_total=cal_quantity*get_price;
                tv_total.setText(get_total+"");
            }
        });
        return v;
    }
};

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.yu.singleton.Main8Activity"
android:orientation="vertical">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_weight="0.3"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@android:color/holo_blue_dark"
    android:layout_weight="0.7"
    android:layout_height="match_parent">

    <TextView
        android:text="Total"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:textAlignment="center"
        android:textSize="36sp" />
</LinearLayout>
</LinearLayout>

我的问题是如何添加每一行的小计,然后将其显示在Listview下面的Textview(Total)中

无论何时加上或减去,您都应该将数量保存到数据源中。
这是将数量保存到数据源并在减去时计算总数的方法,你应该对加

做同样的事情
minus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        cal_quantity = Integer.parseInt(tv_quantity.getText().toString());
        if (cal_quantity != 1) {
            cal_quantity = cal_quantity - 1;
        }

        // Save the quantity to your datasource (aList)
        aList.get(position).put("quantity", "" + cal_quantity);

        // Calculate total
        int total = 0;
        Log.d("TAG", "start total = " +total);
        for (int i = 0; i < aList.size(); i++) {
            Log.d("TAG", "at "+i+ " quantity = " +aList.get(i).get("quantity"));
            total += Integer.parseInt(aList.get(i).get("quantity")) * Integer.parseInt(aList.get(i).get("price"));
            Log.d("TAG", "at "+i+ " total = " +total);
        }
        // Display total, currently I use Toast, you can display it into your TextView
        Toast.makeText(getApplicationContext(), "Total " + total, Toast.LENGTH_SHORT).show();

        ...
    }
});

您现在的总数已经反映了列表视图中每个行项目的小计。

我建议将您的 hashmap 列表移出作为 activity 的实例变量,以便它具有更广泛的范围,同时声明另一个变量来存储您的总数。

编写一个方法来迭代您的 hashmap 列表,并在每次迭代中获取数量、价格并将它们相乘。在每次迭代中将此值添加到您的 total 变量中,并且在迭代器完成后,将 total textview 的值设置为等于您的 total 变量。在此方法开始时将总变量设置为等于 0.0。

在每个按钮的侦听器的最后一行调用此方法。 但是,我建议您以面向对象的方式执行此操作,在这种方式中,您的哈希图列表可以只是一个简单的对象列表。有助于清晰。

int cal_quantity;
private int total;
**private TextView grandTotal_TV;**

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
**grandTotal_TV = findViewById(R.id.TextView3);**

List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();

final String name[]={"apple","orange","pear"};
final String quantity[]={"1","2","3"};
final String price[]={"5","10","2"};

for(int i=0;i<name.length;i++){
    HashMap<String, String> map = new HashMap<>();
    map.put("name",name[i]);
    map.put("quantity",quantity[i]);
    map.put("price",price[i]);
    aList.add(map);
}

String[] from = {"name","quantity","price"};
int[] to = {R.id.name,R.id.quantity,R.id.price};

SimpleAdapter adapter = new SimpleAdapter(this, aList, R.layout.main7, from, to){
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View v = super.getView(position, convertView, parent);
        final TextView tv_quantity=(TextView)v.findViewById(R.id.quantity);
        final TextView tv_price=(TextView)v.findViewById(R.id.price);
        final TextView tv_total=(TextView)v.findViewById(R.id.total);

        final int get_quantity = Integer.parseInt(tv_quantity.getText().toString());
        final double get_price= Double.parseDouble(tv_price.getText().toString());
        final double get_total=get_quantity*get_price;
        tv_total.setText(get_total+"");

        Button minus=(Button)v.findViewById(R.id.minus);
        minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cal_quantity=Integer.parseInt(tv_quantity.getText().toString());
                if(cal_quantity!=1){
                    cal_quantity=cal_quantity-1;
                }
                tv_quantity.setText(cal_quantity+"");
                double get_total=cal_quantity*get_price;
                tv_total.setText(get_total+"");
                **calculateTotal();**
            }
        });

        Button plus=(Button)v.findViewById(R.id.plus);
        plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cal_quantity=Integer.parseInt(tv_quantity.getText().toString());
                cal_quantity=cal_quantity+1;
                tv_quantity.setText(cal_quantity+"");
                double get_total=cal_quantity*get_price;
                tv_total.setText(get_total+"");
                **calculateTotal();**
            }
        });
        return v;
    }
};

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

}
**private void calculateTotal()
    {
        total=0.0;
        for(int i = 0; i<aList.size(); i++)
        {
            HashMap<String, String> map =  aList.get(i);
            int qty = Integer.parseInt(map.get("quantity"));
            double price = Double.parseDouble(map.get("price"));
            total+=qty*price;

        }
        grandTotal_TV.setText(total);
    }**