Breaking News
Loading...

Deleting A Particular Row in listView Using getView method

9:17 PM
In My Previous Posts i Explained How to Add ImageView And CheckBox And Edit Text and Some Related tasks .
 
In this post I am Giving Simple Explanations about How to Delete a Particular row Selected By User , when user Clicks A button in List View simple its Removed selected Row and Refresh getView Method.

In this ListView contains in ImageView, TextView ,EditText,TextView ,And Button
in a single Row  .

files :

===
cart.xml ( for ListView Layout view )
cartitems.xml ( listView Items )
cartAdapter.java ( For ListView Items and major operations  )
Viewcart.java ( ListView Operations)

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="80dip"
        android:layout_height="wrap_content" android:text="Checkout" />

    <Button android:id="@+id/btnGT" android:layout_width="88dip"
        android:layout_toRightOf="@+id/btnChkOut" android:layout_height="wrap_content"
        android:text="Get Total :" />

    <TextView android:id="@+id/tvGTotal" android:layout_width="60dip"
        android:textStyle="bold" android:layout_marginTop="12dip"
        android:layout_height="wrap_content" android:layout_toRightOf="@+id/btnGT"
        android:textColor="#000000" />
    <Button android:id="@+id/btnCont" android:layout_width="50dip"
        android:layout_height="40dip" android:background="@drawable/home"
        android:layout_alignParentRight="true" />

    <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" />
        <Button  
        android:background="@drawable/del"
        android:layout_below="@+id/etQuantity"
        android:id="@+id/btnDC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/etQuantity"
        android:layout_marginLeft="40dip"
       
        />
</RelativeLayout>

cartadapter.java
----------------------------
package gadgetstore.utils;

import gadgetstore.screens.R;
import gadgetstore.screens.ViewCart;

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

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
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 = null;
    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) {
        cartList.clear();
        total.clear();
        total1.clear();
        total2.clear();
        mCart = data;
        mCbean = null;
        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 int pos = position;
        final View vv = arg1;
        final DataBean dbObj = cartList.get(position);
        final View v = mLayoutInflater.inflate(R.layout.cartitems, null);
        ImageView img = (ImageView) v.findViewById(R.id.imgView);
        final Button btnDC = (Button) v.findViewById(R.id.btnDC);
        btnDC.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // deleting a item for array list
                StaticUtils.sCart.remove(cartList.get(pos));
                // calling activty for listview refresh when u clik a button
                Intent myIntent = new Intent(vv.getContext(), ViewCart.class);
                vv.getContext().startActivity(myIntent);
               

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

        return total;

    }

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

    public ArrayList<DataBean> getCartlist() {
        return cartList;
    }
}


viewCrt.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.util.Log;
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, mBtnContShop;
    private TextView mTvGt;

    public ViewCart() {
    }

    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);
        try {
            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);
            mBtnContShop = (Button) findViewById(R.id.btnCont);
            mBtnContShop.setOnClickListener(this);
        } catch (Exception e) {
            Log.w(" view cart exception ", e.toString());
        }

    }

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

            try {

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

            } catch (Exception e) {
                Log.w(" error ", e.toString());
            }

        } else if (v == mBtnContShop) {
            startActivity(new Intent(ViewCart.this, HomeScreenTab.class));
        }

    }

}



For Source Code Click Here

0 komentar:

Post a Comment

 
Toggle Footer