Apr 26, 2011

ImageView TextView Edittext in List View using getview method And Calculating Grand Total in android

In this programing i am explaing code For To display Image and Text and EditText in Single Colum in ListView

files :
-------
cart.xml
cartitems.xml
cartAdapter.java
CartDetails.java

cart.xml :
======
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/bg">
  
    <Button android:id="@+id/btnChkOut" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="Checkout" />
      
        <Button android:id="@+id/btnGT" android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/btnChkOut"
        android:layout_height="wrap_content" android:text="Get Total :" />
      
    <TextView android:id="@+id/tvGTotal" android:layout_width="wrap_content"
    android:layout_marginTop="10dip"
    android:textStyle="bold"
        android:layout_height="wrap_content" android:layout_toRightOf="@+id/btnGT"
        android:paddingLeft="20dip" android:textColor="#000000" />
      
    <ListView android:layout_below="@+id/btnChkOut" android:id="@+id/listViewCart"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
</RelativeLayout>

cartitems.xml
-------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:id="@+id/imgView" android:layout_width="90dip"
        android:layout_height="80dip" />
    <TextView android:layout_marginTop="10dip" android:id="@+id/tvPrice"
        android:layout_width="80dip" android:layout_height="20dip"
        android:hint="price" android:layout_toRightOf="@+id/imgView"
        android:textStyle="bold" android:textColor="#000000" />
    <EditText android:id="@+id/etQuantity" android:layout_width="60dip"
        android:layout_height="35dip" android:layout_toRightOf="@+id/tvPrice"
        android:textSize="13sp" android:layout_marginTop="10dip" android:text="1"
        android:gravity="center" android:inputType="number" />
    <TextView android:id="@+id/tvTotal" android:layout_width="80dip"
        android:layout_height="20dip" android:hint="total"
        android:layout_toRightOf="@+id/etQuantity" android:paddingLeft="20dip"
        android:textStyle="bold" android:layout_marginTop="10dip"
        android:textColor="#000000" />
</RelativeLayout>

CartAdapter.java
-----------------------
package gadgetstore.utils;

import gadgetstore.screens.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class CartAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    public Double tot = 0.0;
    public Set<DataBean> mCart;
    public ArrayList<DataBean> cartList = new ArrayList<DataBean>();
    public HashMap<Integer, String> mQuantity = new HashMap<Integer, String>();
    public HashMap<Integer, Double> mPrice = new HashMap<Integer, Double>();
    public static HashMap<Integer, Double> total = new HashMap<Integer, Double>();
    public static HashMap<Integer, Double> total1 = new HashMap<Integer, Double>();
    public static HashMap<Integer, Double> total2 = new HashMap<Integer, Double>();
    int mPosition;
    private Iterator<DataBean> mCbean;
    public String price = "41";
    public Double pr = 0.0;

    public CartAdapter(Context context, Set<DataBean> data,
            LayoutInflater inflater) {
        total.clear();
        total1.clear();
        mCart = data;
        mContext = context;
        mLayoutInflater = inflater;
        mCbean = mCart.iterator();
        while (mCbean.hasNext()) {
            DataBean db = (DataBean) mCbean.next();
            cartList.add(db);
        }
        getTotalPrice();
    }

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

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    int count = 0;

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        final DataBean dbObj = cartList.get(position);
        final View v = mLayoutInflater.inflate(R.layout.cartitems, null);
        ImageView img = (ImageView) v.findViewById(R.id.imgView);
        int im = mContext.getResources().getIdentifier(dbObj.getImgpath(),
                "raw", mContext.getPackageName());
        img.setBackgroundResource(im);
        final TextView tv = (TextView) v.findViewById(R.id.tvPrice);
        tv.setText(dbObj.getPrice());
        final EditText etQuan = (EditText) v.findViewById(R.id.etQuantity);
        final int pos = position;
        final TextView brprice = (TextView) v.findViewById(R.id.tvTotal);
        brprice.setText(dbObj.getPrice());
        total.put(position, Double.parseDouble(dbObj.getPrice()));
        pr = pr + Double.parseDouble(brprice.getText().toString());
        mPosition = position;
        if (count != 0) {
            try {
                if (count != 0) {
                    String st = mQuantity.get(pos);
                    if (st.length() != 0)
                        etQuan.setText(st);
                    Double price = mPrice.get(pos);
                    if (price != null)
                        brprice.setText(price + "");

                }
            } catch (NullPointerException e) {

            }

        }

        etQuan.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {

                int quan;
                try {
                    mQuantity.put(pos, etQuan.getText().toString());
                    quan = Integer.parseInt(etQuan.getText().toString());
                } catch (NumberFormatException e) {
                    quan = 1;
                }
                double tprice = Double.parseDouble(dbObj.getPrice());
                brprice.setText("" + (tprice * quan));

                total.put(pos, (Double.parseDouble(dbObj.getPrice()) * quan));
                total1 = total;

                mPrice.put(pos, (tprice * quan));
                count++;
                price = quan + "";

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });

        total2.put(position, Double.parseDouble(etQuan.getText().toString()));

        return v;
    }

    public static HashMap<Integer, Double> getTotalPrice() {

        if (total1.size() == 0)
            return total;
        else
            return total1;

    }

    public static HashMap<Integer, Double> getQunatity() {
        return total2;
    }

    public ArrayList<DataBean> getCartlist() {
        return cartList;
    }
}
Viewcart.java
----------------------
package gadgetstore.screens;

import gadgetstore.utils.CartAdapter;
import gadgetstore.utils.DataBean;
import gadgetstore.utils.StaticUtils;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ViewCart extends Activity implements OnClickListener {
    private ListView lv;
    private LayoutInflater mLayoutInflater = null;
    public ArrayList<DataBean> mCList;
    double totPrice;
    private Button mBtnCheckout, mBtnGt;
    private TextView mTvGt;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cart);
        lv = (ListView) findViewById(R.id.listViewCart);
        mLayoutInflater = getLayoutInflater();
        final CartAdapter lAdapter = new CartAdapter(getApplicationContext(),
                StaticUtils.sCart, mLayoutInflater);
        lv.setAdapter(lAdapter);
        lv.setCacheColorHint(0);
        mCList = lAdapter.getCartlist();
        mBtnCheckout = (Button) findViewById(R.id.btnChkOut);
        mBtnGt = (Button) findViewById(R.id.btnGT);
        mTvGt = (TextView) findViewById(R.id.tvGTotal);
        mBtnCheckout.setOnClickListener(this);
        mBtnGt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v == mBtnCheckout) {
            startActivity(new Intent(ViewCart.this, PaypalScreen.class));
        } else if (v == mBtnGt) {

            HashMap<Integer, Double> price = new HashMap<Integer, Double>();
            price = CartAdapter.getTotalPrice();
            HashMap<Integer, Double> quantity = new HashMap<Integer, Double>();
            quantity = CartAdapter.getQunatity();

            int si = quantity.size();
            double d = 0.0;
            for (int j = 0; j < si; j++) {
                d = (quantity.get(j) * price.get(j)) + d;

            }

            mTvGt.setText("  " + d);

        }

    }

}


6 comments:

  1. hi this is indela

    i copy code and paste into my project its give compile error.please send me zip file
    thank you.
    my email id is indela447@gmail.com

    ReplyDelete
  2. hi indela u can get Source link at the end of source

    ReplyDelete
  3. At least two files are missing in the source code. One is DataBean.java and other is StaticUtils.java. Include these in your link "For Source Cod click here" (Spelling mistake of code).

    Nitin Rajurkar

    ReplyDelete
  4. i teseted ur application but when i entered number in edittext and moving to other edittext the values r changing to previous values...
    plz suggest me to solve this.

    Thanking you,
    nagu.

    ReplyDelete
  5. good tutorials..i like it..

    ***can u tell me how i save data in EditText?
    when i scroll the list item,the inserted data in a edit text in list item which is not shown in the list after scrolling is deleted automatically...

    plz need a quick reply..

    thanks in advance..

    ReplyDelete
    Replies
    1. Yes that time listview is Loaded Automatically , so if we enter any data we will lost , so save data, which is entered in Edit text before scroll for more info check adapter & listview methods ..

      Delete