计算总金额

时间:2014-10-31 11:19:53

标签: android listview custom-adapter

enter image description here

当我点击listview行项目的加号或减号按钮时,我想计算总金额,并将更改更新为总数请帮助我摆脱这个问题 提前致谢

3 个答案:

答案 0 :(得分:1)

  

我已经解决了这个你只需要调用父方法   customadapter执行总计算

喜欢这个((ParentClass) context).TotalCalculationfunctionToRun(); 从加号或减号按钮上的自定义适配器分别单击

答案 1 :(得分:0)

我有一个解决方案,可能会帮助你

<TextView
    android:id="@+id/cartTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="RS 0.00"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/cartTotal"
    android:text="Total: "
    android:textAppearance="?android:attr/textAppearanceMedium" />


<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/textView2" />`

适用于指定屏幕的设计布局

 <TextView
    android:id="@+id/tiem_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="TextView"
    android:textSize="20dp" />

<TextView
    android:id="@+id/flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="TextView"
    android:textSize="20dp" />

<TextView

    android:id="@+id/eachPrice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="120.0"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/ext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="10dp"
    android:text="Index Total Price"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="20dp"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editText2"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:hint="Qty" />

    <Button
        android:id="@+id/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/minus"
        android:layout_alignBottom="@+id/minus"
        android:layout_marginEnd="18dp"
        android:layout_marginRight="18dp"
        android:layout_toLeftOf="@+id/minus"
        android:layout_toStartOf="@+id/minus"
        android:text="+" />

    <Button
        android:id="@+id/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText2"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="-" />

</RelativeLayout>

模型项

public class CartItem {

String name;
String flag;
int UserQty;
double ProductPrice;
double ProductSalePrice;
private Double ext = 0.00;

public Double getExt() {
    return ext;
}

public void setExt(Double ext) {
    this.ext = ext;
}

public double getProductSalePrice() {
    return ProductSalePrice;
}

public void setProductSalePrice(double productSalePrice) {
    ProductSalePrice = productSalePrice;
}

public double getProductPrice() {
    return ProductPrice;
}

public void setProductPrice(double productPrice) {
    ProductPrice = productPrice;
}

public int getUserQty() {
    return UserQty;
}

public void setUserQty(int userQty) {
    UserQty = userQty;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getFlag() {
    return flag;
}

public void setFlag(String flag) {
    this.flag = flag;
}

}

**Main Activity**

公共类MainActivity扩展Activity {     String url =&#34; http://139.59.43.252/cnsgoCash/api/ws/controller/?access=true&action=get_category_by_product_list&id=5&#34 ;;     ProgressDialog PD;     按钮付款;

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    payment=(Button)findViewById(R.id.payment);
    ExpandList = (ExpandableListView) findViewById(R.id.expandableListView);
    PD = new ProgressDialog(this);
    PD.setMessage("Loading.....");
    PD.setCancelable(false);
    makejsonobjreq();

/*  Click*/
    payment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Intent in =new Intent(MainActivity.this,AddedItemActivity.class);
                startActivity(in);


        }
    });
    ExpandList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        }
    });

}

private void makejsonobjreq() {
    PD.show();
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println("Json String :" + String.valueOf(response));
            Log.e("RES", String.valueOf(response));
            ArrayList<Group> list = new ArrayList<Group>();
            ArrayList<Child> ch_list;
            try {
                //Iterator<String> key = response.keys();
                JSONArray jsonArray=response.getJSONArray("data");

                for(int i=0;i<jsonArray.length();i++){
                    JSONObject object= jsonArray.getJSONObject(i)  ;
                    String k = object.getString("categoryName");

                    Log.e("Parent","hello"+k);
                    Group gru = new Group();
                    gru.setName(k);
                    ch_list = new ArrayList<Child>();
                    JSONArray ja = object.getJSONArray("productdetails");
                    for (int j = 0; j < ja.length(); j++) {
                        JSONObject jo = ja.getJSONObject(j);
                        Child ch = new Child();
                        //Log.e("Child","hello"+jo.getString("productName"));
                        ch.setName(jo.getString("productCode"));
                        ch.setFlag(jo.getString("productName"));
                        ch.setUserQty(jo.getString("quantity"));
                        ch.setProductPrice(jo.getString("sellPrice"));
                        ch_list.add(ch);
                    } // child for loop end
                    gru.setItems(ch_list);
                    list.add(gru);
                }

                ExpAdapter = new ExpandListAdapter(MainActivity.this, list);
                ExpandList.setAdapter(ExpAdapter);
                PD.dismiss();

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);
}

}

答案 2 :(得分:-1)

尝试这样可以帮到你,

 private int getTotal(){
          private int totalamount = 0;
          for(int i = 1; i < list.getCount(); i++){
                View view = list.getAdapter().getView(i, null, list);
                TextView txt = (TextView) view.findViewById(R.id.yourid);
                int amount = Integer.paserInt(txt.getText().toString())
                totalamount += amount;
            }
            return totalamount;
    }
相关问题