2014-10-31 114 views
1

enter image description here計算總量

我希望當加減按鈕點擊listview行項目計算總金額和更新的變化總有什麼可以幫我出這個問題的提前

謝謝
+1

請向我們提供您的代碼塊你嘗試過什麼,你面臨 – Pratik 2014-10-31 11:24:25

+0

問題我所做的是什麼,我都要求從定製適配器上的加號按鈕點擊,但它的功能更新時,活動從午餐切換到早餐,我想在點擊@ PRATIK – 2014-10-31 11:27:03

+0

的時間來更新@Pratik請給一些想法,以更新 – 2014-10-31 11:57:34

回答

1

我已經解決了這個您只要致電從 父方法customadapter其執行你的總計算

喜歡這個((ParentClass) context).TotalCalculationfunctionToRun(); 從加號或減號按鈕分別單擊

+0

謝謝你abhishek – 2014-11-01 12:21:28

-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; 
    } 
+1

但它如何幫助更新總在點擊的時候加或減號 – 2014-10-31 11:29:36

+0

點擊該按鈕 – 2014-10-31 11:30:26

+0

該按鈕是在列表視圖內 – 2014-10-31 11:31:04

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延伸活動{ 字符串URL = 「http://139.59.43.252/cnsgoCash/api/ws/controller/?access=true&action=get_category_by_product_list&id=5」; 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); 
} 

}