如何在android的基本适配器中删除自定义listview中的行

如何在android的基本适配器中删除自定义listview中的行,android,arraylist,android-listview,custom-lists,Android,Arraylist,Android Listview,Custom Lists,在行自定义列表视图中显示itemimage、itemname、rate、qty、price和delete按钮。。如果单击“删除”按钮,则表示希望删除listview中的整行 我试试这个代码 public class CustomAdapter extends BaseAdapter { public static Double x = 0.0; public static Double z; ArrayList<Integer> selectprice = new Arra

在行自定义列表视图中显示itemimage、itemname、rate、qty、price和delete按钮。。如果单击“删除”按钮,则表示希望删除listview中的整行

我试试这个代码

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

}
公共类CustomAdapter扩展了BaseAdapter{
公共静态双x=0.0;
公共静态双z;
ArrayList selectprice=新建ArrayList();
公共静态ArrayList arr1=新ArrayList();
公共静态ArrayList itemprice=新ArrayList();
公共静态ArrayList itemimage=新ArrayList();
ArrayList总计=新的ArrayList();
公共语境;
私人充气机;
HashMap=newHashMap();
公共CustomAdapter(上下文上下文,ArrayList arr,
ArrayList价格,ArrayList图像){
上下文=上下文;
充气器=充气器。从(上下文);
arr1=arr;
项目价格=价格;
itemimage=图像;
系统输出打印项次(项目价格);
System.out.println(“arr:+arr.size());
对于(int i=0;i
这是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>       

我获取了itemimage itemname price的所有数组,然后将其显示到自定义列表视图中。若我按delete按钮,意味着它将删除listview中的特定行。请帮助我

您需要调用

Adapter.notifyDataSetChanged() 
在对
数组列表中的数据源进行更改后,请尝试以下操作:

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();
    }
});
调用“notifyDataSetChanged”意味着与视图关联的数据已被更改,因此视图必须刷新自身以使其与数据保持更新。

适配器。删除(itemimage); . . . adapter.notifyDataSetChanged()

其中“adapter”是将数据存储到listview中的适配器名称,如(listview.setadapter(adapter)),您知道的“itemimage”是什么