Deleting A Particular Row in listView Using getView method


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 ) ( For ListView Items and major operations  ) ( ListView Operations)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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" />

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="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" />
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) {
        mCart = data;
        mCbean = null;
        mContext = context;
        mLayoutInflater = inflater;
        mCbean = mCart.iterator();
        while (mCbean.hasNext()) {
            DataBean db = (DataBean);

    public int getCount() {
        return cartList.size();

    public Object getItem(int arg0) {

        return null;

    public long getItemId(int arg0) {

        return 0;

    int count = 0;

    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(;
        final Button btnDC = (Button) v.findViewById(;
        btnDC.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

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

        int im = mContext.getResources().getIdentifier(dbObj.getImgpath(),
                "raw", mContext.getPackageName());
        final TextView tv = (TextView) v.findViewById(;
        final EditText etQuan = (EditText) v.findViewById(;

        final TextView brprice = (TextView) v.findViewById(;
        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)
                    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));
                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;
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.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) {
        lv = (ListView) findViewById(;
        mLayoutInflater = getLayoutInflater();
        final CartAdapter lAdapter = new CartAdapter(getApplicationContext(),
                StaticUtils.sCart, mLayoutInflater);
        try {
            mCList = lAdapter.getCartlist();
            mBtnCheckout = (Button) findViewById(;
            mBtnGt = (Button) findViewById(;
            mTvGt = (TextView) findViewById(;
            mBtnContShop = (Button) findViewById(;
        } catch (Exception e) {
            Log.w(" view cart exception ", e.toString());


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



Search & Download lots of Eclipse-Android Tutorials:-
  Using Pdf Wallet you can find useful and free Android Tutorials.


Please Select File-Format

PDF  - eBooks in pdf format.
PPT  - Presentations:2003.
PPTX- Presentations:2007+.
DOC - Microsoft Word Files:2003.
DOCXMicrosoft Word Files:2007+.
XLS  - Excel File format:2003.
XLSX - Excel File Format:2007+.
CHM - Compiled Help Files.
RTF  - Rich Text format.
TXT  - Text Files. 

1 comment:

  1. Hi! Can you give me more details on some freelancing sites that will help me make money? I am a housewife and am looking for some place where I can post my jobs without leaving my home. I worked as a web developer before becoming a stay-at-home mom. Will I get job offers as a freelancer? Your blog was very useful. I checked out blog commenting service,creative logo design and sites. They come with high ratings and good reviews. Let me give a try. What do you say?