3

我在行自定义列表视图中显示 itemimage、itemname、rate、qty、price 和删除按钮 .. 如果我单击删除按钮意味着我想删除列表视图中的整行。

我试试这段代码

    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 的所有数组,然后显示到自定义列表视图中。如果我按下删除按钮意味着它会删除列表视图中的特定行。请帮我

4

3 回答 3

6

尝试这个,

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”意味着与视图关联的数据已更改,因此视图必须刷新自身以保持数据更新。

于 2012-05-18T05:39:51.830 回答
1

你需要调用

Adapter.notifyDataSetChanged() 

在您对数据源进行更改后,这些数据源位于您的ArrayLists.

于 2012-05-18T05:35:03.197 回答
0

适配器.remove(itemimage); . . . 适配器.notifyDataSetChanged() ;

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

于 2012-12-24T04:28:42.873 回答