how to delete row in custom listview in base adapter in android

naveen picture naveen · May 18, 2012 · Viewed 7k times · Source

I am displayed itemimage,itemname,rate,qty,price and delete button in row custom listview .. if i click the delete button means i want remove whole row in listview.

I try this code

    public class CustomAdapter extends BaseAdapter {

public static Double x = 0.0;
public static Double z;
ArrayList<Integer> selectprice = new ArrayList<Integer>();
public static ArrayList<String> arr1 = new ArrayList<String>();
public static ArrayList<String> itemprice = new ArrayList<String>();
public static ArrayList<Bitmap> itemimage = new ArrayList<Bitmap>();
ArrayList<Integer> total = new ArrayList<Integer>();
public Context Context;
private LayoutInflater inflater;

HashMap<String, String> map = new HashMap<String, String>();

public CustomAdapter(Context context, ArrayList<String> arr,
        ArrayList<String> price, ArrayList<Bitmap> image) {
    Context = context;
    inflater = LayoutInflater.from(context);
    arr1 = arr;
    itemprice = price;

    itemimage = image;
    System.out.println(itemprice);
    System.out.println("arr: " + arr.size());

    for (int i = 0; i < price.size(); i++) {

        x = x + Double.parseDouble(price.get(i));

    }

}

public int getCount() {
    // TODO Auto-generated method stub
    return arr1.size();

}

// public void remove(ViewHolder v) {
// arr1.remove(arr1);
// notifyDataSetChanged();
// }

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return arr1.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.selecteditemlistview, null);

        holder = new ViewHolder();

        holder.textViewSelectedText = (TextView) convertView
                .findViewById(R.id.selectedtext);
        holder.price = (TextView) convertView
                .findViewById(R.id.selectitemprice);
        holder.image = (ImageView) convertView
                .findViewById(R.id.selectitemimage);
        holder.qty = (EditText) convertView.findViewById(R.id.selectqty);
        holder.total = (TextView) convertView.findViewById(R.id.price);
        holder.delete = (Button) convertView.findViewById(R.id.delete);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final Double price1 = Double.parseDouble(itemprice.get(position));
    int qut = Integer.parseInt(holder.qty.getText().toString());
    final Double total = (price1 * qut);
    System.out.println(total);
    holder.textViewSelectedText.setText(arr1.get(position));
    holder.price.setText(itemprice.get(position));
    holder.image.setImageBitmap(itemimage.get(position));
    holder.total.setText(String.valueOf(total));

    holder.delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            arr1.remove(position);//here only i tried to remove the row in custom listview
            itemprice.remove(position);
            itemimage.remove(position);

        }
    });
    holder.qty.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub
            if (!hasFocus) {
                final EditText Caption = (EditText) v;
                Caption.setFocusable(true);
                holder.qty.setFocusable(true);
                Double q = Double.parseDouble(holder.qty.getText()
                        .toString());
                double result = (price1 * q);

            }

        }
    });

    return convertView;
}

class ViewHolder {

    Button delete = null;
    TextView textViewSelectedText = null;
    TextView price = null;
    ImageView image = null;
    EditText qty = null;
    TextView total = null;
}

}

this my selecteditemlistview.xml

        <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
       <ImageView
  android:id="@+id/selectitemimage" 
  android:layout_width="75dip"
  android:layout_height="79dip" android:src="@drawable/stub"        
      android:scaleType="centerCrop"/>

   <TextView
  android:id="@+id/selectedtext" 
  android:textColor="#000000"
  android:layout_width="150dp"
  android:layout_height="wrap_content"
  android:layout_marginLeft="80dip"
  android:layout_weight="1"
  android:textSize="15sp" />

  <TextView
  android:id="@+id/selectitemprice"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
 android:textColor="#000000"
  android:textAppearance="?android:attr/textAppearanceMedium" 
  android:layout_marginLeft="250dp"/>

  <EditText
  android:id="@+id/selectqty"
   android:maxLength="3" 
  android:layout_width="40dp"
   android:text="1"
  android:layout_height="40dp" 
  android:singleLine="true"
  android:layout_alignParentTop="true" 
  android:layout_marginLeft="36dp"
  android:layout_toRightOf="@+id/selectitemprice"
   >

  <requestFocus />      
  </EditText>

 <TextView
  android:id="@+id/price" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="400dp"
  android:layout_toRightOf="@+id/editText1" 
 android:inputType="none"
 android:textColor="#000000"
  android:editable="false"
  android:textAppearance="?android:attr/textAppearanceMedium" />

 <Button
   android:id="@+id/delete"
   android:layout_width="100dp"
   android:layout_height="50dp"
   android:text="delete"

   android:layout_marginLeft="630dp"


   />

</RelativeLayout>       

I taken all arrays of itemimage itemname price and then displayed into a custom listview. if i press delete button means it delete that particular row in listview. please help me

Answer

Andro Selva picture Andro Selva · May 18, 2012

Try this,

holder.delete.setOnClickListener(new OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        arr1.remove(position);//here only i tried to remove the row in custom listview
        itemprice.remove(position);
        itemimage.remove(position);
        notifyDataSetChanged();
    }
});

Calling "notifyDataSetChanged" means that the data associated with the view has been changed and hence the view has to refresh itself to keep it updated with the data.