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

标签 android arraylist android-listview custom-lists

我在行自定义 ListView 中显示了项目图像、项目名称、费率、数量、价格和删除按钮。如果我单击删除按钮意味着我想删除 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;
}

}

这是我的 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 的所有数组,然后显示到自定义 ListView 中。如果我按删除按钮意味着它删除 ListView 中的特定行。请帮助我

最佳答案

试试这个,

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” 意味着与 View 关联的数据已更改,因此 View 必须自行刷新以使其随数据更新。

关于android - 如何在android中的基本适配器中删除自定义 ListView 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10647168/

相关文章:

android - 有没有办法在 Android 中将 ListView 项目编号存储在 int 中?

android - Google App Engine 示例应用抛出 503

java - Java中的可变长度(动态)数组

android - ListView 中的三个状态检查 TextView

java - 根据日期时间对对象的数组列表进行排序

java - 如何从此哈希表获取信息

android - ImageView 在 getView() 时崩溃自定义适配器

java - 在Android项目中使用HTML解析器

android - 新导航架构中的圆形显示动画

android - Dagger 2 静态注入(inject)