Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android listview simpleAdapter获取每行的总和';s最后一列文本视图_Android_Listview_Simpleadapter - Fatal编程技术网

Android listview simpleAdapter获取每行的总和';s最后一列文本视图

Android listview simpleAdapter获取每行的总和';s最后一列文本视图,android,listview,simpleadapter,Android,Listview,Simpleadapter,我有3个名称、数量和价格数组。 我使用Listview通过SimpleAdapter显示它们。最重要的是,我在每行有2个按钮,用于控制数量-加和减数量 单击加号或减号按钮时,数量将发生变化,该行的小计也将更新。 如果数量=1,单击减号按钮时,数量将保持为1 到目前为止,所有功能都正常工作 Java int cal_quantity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sa

我有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);

}
int-calu数量;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
列表列表=新的ArrayList();
最后一个字符串名[]={“苹果”、“橙色”、“梨”};
最终字符串数量[]={“1”、“2”、“3”};
最终字符串价格[]={“5”、“10”、“2”};

对于(int i=0;i无论何时加或减,都应将数量保存到数据源中。
下面是将数量保存到数据源并计算总数的方法。当减数时,您应该对加数执行相同的操作

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();

        ...
    }
});
减号.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
cal_quantity=Integer.parseInt(tv_quantity.getText().toString());
如果(校准数量!=1){
校准数量=校准数量-1;
}
//将数量保存到数据源(列表)
列表.get(position).put(“数量”、“+calu数量”);
//计算总数
int-total=0;
Log.d(“标签”,“开始总计=+总计”);
对于(int i=0;i
您现在的总计已经反映了listview中每行项目的小计

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

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

在每个按钮的侦听器的最后一行调用此方法。 不过,我建议您以面向对象的方式进行操作,您的hashmap列表可以只是一个简单的对象列表。这有助于提高清晰度

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);
    }**
int-calu数量;
私人整数合计;
**私人文本视图grandTotal_TV**
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
**grandTotal_TV=findViewById(R.id.TextView3)**
列表列表=新的ArrayList();
最后一个字符串名[]={“苹果”、“橙色”、“梨”};
最终字符串数量[]={“1”、“2”、“3”};
最终字符串价格[]={“5”、“10”、“2”};

对于(int i=0;ihey!thx表示您的答案。但是,数学计算不正确。它仅在单击第一个按钮时正确。当单击第二次或更多次时,数学计算是错误的。知道有什么问题吗?@gosulove我已经在我的答案中添加了2个logcat,请帮助我将其输入到您的代码中,然后再次运行您的应用程序,并向我显示logcat的错误谢谢。请检查我编辑的问题。我在那里发布供您参考。我已经添加了一个
Log.d(“TAG”,“start total=“+total”);
。请将其添加到您的代码中,然后再次调试。确保不要将total变量放在onClickListener之外,将其像我的代码一样放在里面。我将稍后再试一次,看看它是否有效。顺便说一句,您以前试过吗?
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);
    }**