2016-02-28 14 views
0

我正在使用Arraylist來存儲用戶購物車。 Arraylist保存物品添加到購物車的詳細信息。在ViewCart有刪除ImageButton。我想要用戶點擊刪除按鈕該項目,其所有細節應從ArrayList刪除。從列表視圖中獲取Arraylist的索引

我怎樣才能得到被點擊的Arraylist項目的索引,以便我可以刪除它。 Cart view is screenshot is here我想讓用戶點擊刪除圖標整行必須從Arraylist刪除。在圖像的ArrayList大小爲2

列表視圖cartitems.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/cyan" 
      android:weightSum="100" > 

      <TextView 
       android:id="@+id/tvcartid" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/tvcartname" 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_marginBottom="1dp" 
       android:layout_marginLeft="1dp" 
       android:layout_marginTop="1dp" 
       android:layout_weight="30" 
       android:background="@color/white" 
       android:gravity="center" 
       android:padding="0dp" /> 

      <TextView 
       android:id="@+id/tvcartcategory" 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_marginBottom="1dp" 
       android:layout_marginLeft="1dp" 
       android:layout_marginTop="1dp" 
       android:layout_weight="20" 
       android:background="@color/white" 
       android:gravity="center" 
       android:text="TextView" 
       android:visibility="gone" /> 

      <TextView 
       android:id="@+id/tvcartprice" 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_marginBottom="1dp" 
       android:layout_marginLeft="1dp" 
       android:layout_marginTop="1dp" 
       android:layout_weight="20" 
       android:background="@color/white" 
       android:gravity="center" 
       android:text="TextView" /> 

      <ImageView 
       android:id="@+id/tvcartimage" 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_marginBottom="1dp" 
       android:layout_marginLeft="1dp" 
       android:layout_marginTop="1dp" 
       android:layout_weight="30" 
       android:background="@color/white" 
       android:gravity="center" /> 

      <ImageButton 
       android:id="@+id/bdelete" 
       android:layout_width="0dp" 
       android:layout_height="70dp" 
       android:layout_marginBottom="1dp" 
       android:layout_marginLeft="1dp" 
       android:layout_marginTop="1dp" 
       android:layout_weight="20" 
       android:background="@color/white" 
       android:gravity="center" 
       android:onClick="DeleteItem" 
       android:paddingBottom="10dp" 
       android:paddingLeft="5dp" 
       android:paddingRight="5dp" 
       android:paddingTop="10dp" 
       android:scaleType="fitCenter" 
       android:src="@drawable/delete" /> 
     </TableRow> 
    </LinearLayout> 
</ScrollView> 

ViewCart.java

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewcart); 
    ArrayList<HashMap<String, String>> items; 
    AddtoCart obj = (AddtoCart) getApplicationContext(); 
    items = obj.getCart(); 
    Log.i("SIZE", String.valueOf(obj.getSize())); 

    ListAdapter adapter = new SimpleAdapter(ViewCart.this, items, R.layout.cartitems, 
      new String[] {TAG_PID, TAG_NAME, TAG_CATEGORY, TAG_PRICE,TAG_IMAGE}, 
      new int[] { R.id.tvcartid, R.id.tvcartname, R.id.tvcartcategory 
        , R.id.tvcartprice, R.id.tvcartimage}); 
    setListAdapter(adapter); 


} 

public void DeleteItem(View v) { 
    //What should i write here? 
} 
} 
+0

你可以在代碼中調用'DeleteItem'嗎?請發佈更多代碼 –

+0

@shayanpourvatan DeleteItem()函數通過xml代碼中的'ImageButton'調用。 '' – Infinity

回答

0

,而不是通過XML設置onClickListener,你可以將它設置在這樣的自定義適配器中:

public class CustomSimpleAdapter extends SimpleAdapter { 


    private List<? extends Map<String, ?>> mData; 

    public CustomSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
     mData = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = super.getView(position, convertView, parent); 
     final Object data = mData.get(position); 
     v.findViewById(R.id.bdelete).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        mData.remove(data); 
        notifyDataSetChanged(); 
      } 
     }); 
     return v; 
    } 
} 

這樣,您可以獲取與點擊的視圖對應的數據,修改列表以反映刪除操作,然後調用notifyDataSetChanged()刷新列表。