簡體   English   中英

如何為單個項目取消設置ListView按鈕的onClickListener-Android

[英]How to unset ListView button's onClickListener for a single item - Android

我已使用TableLayout生成以下購物車活動輸出。 輸出

所以listview有3個listviews(項目,價格,數量)和一個按鈕(用於刪除),我想給購物車一個標題。 因此,我將列表視圖的第一個項目初始化為標題(“項目名稱”,“價格”,“數量”,“刪除”)

現在的問題是,每當用戶單擊“刪除”按鈕(如圖中圓圈所示)時,標題也會被刪除。 (由於onClickListener默認情況下已應用於適配器類中的所有按鈕)

我應該如何刪除第一個Button上的onClickListener並保持其余按鈕的原樣?

另外:如果你們可以建議一些很棒的方式格式化購物車,以免顯得天真,那就太好了。 :)排燈節快樂,在此先感謝。 -Android newBee

cartlayout.xml文件

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:background="#ffffff">
<TableRow>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cartlist_layout"
android:orientation="horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t1"
    android:textStyle="normal|bold"
    android:padding="5dip"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/t2"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t2"
    android:padding="5dip"
    android:textStyle="normal|bold"
    android:layout_centerHorizontal="true"
    />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/t2"
    android:id="@+id/t3"
    android:textStyle="normal|bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/button6" />
</TableRow>>
</TableLayout>

Java代碼:

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_mycart,container,false);
    this.getActivity().setTitle("AADESH");

    cartlistview=(ListView)view.findViewById(R.id.listView1);

    db=new DatabaseHelper(this.getContext());
    db.onCreate();
    Cursor res=db.onView();
    int len=res.getCount();

    listCartItems = new ArrayList<CartItems>();
    listCartItems.add(new CartItems("Item Name", "Quantity", "Price","Delete"));

    if(len==0)
    {
        Toast.makeText(this.getContext(),"Cart is Empty.",Toast.LENGTH_SHORT).show();
        statusOfCart=false;
    }
    else {
        while (res.moveToNext()) {
            String itemname = res.getString(1).toString();  // 0 is id, 1 is name, 2 is qty, 3 price
            String itemqty = Integer.toString(res.getInt(2));
            String itemprice = Integer.toString(res.getInt(3)) ;
            Toast.makeText(this.getContext(),itemname,Toast.LENGTH_SHORT).show();
            listCartItems.add(new CartItems(itemname, itemqty, itemprice,"X"));
        }
    }
    CartListAdapter cartListAdapter = new CartListAdapter(getContext(), R.layout.cartlist_layout, listCartItems);
    cartlistview.setAdapter(cartListAdapter);

    return view;
}

}

這里的CartAdapter Java代碼:

public class CartListAdapter extends ArrayAdapter<CartItems> {
Context context;
int resLayout;
List<CartItems> listCartItems;
int pos;
public CartListAdapter(Context context,int resLayout,List <CartItems> listCartItems) {
    super(context, resLayout, listCartItems);
    this.context=context;
    this.resLayout=resLayout;
    this.listCartItems=listCartItems;
}

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v=View.inflate(context,resLayout,null);
    pos=position;
    TextView name=(TextView)v.findViewById(R.id.t1);
    TextView qty=(TextView)v.findViewById(R.id.t2);
    TextView price=(TextView)v.findViewById(R.id.t3);
    Button delete=(Button)v.findViewById(R.id.button6);

    CartItems cartItems=listCartItems.get(position);
    name.setText(cartItems.getItemname());
    qty.setText(String.valueOf(cartItems.getQty()));
    price.setText(String.valueOf(cartItems.getPrice()));
    delete.setText(String.valueOf(cartItems.getDel()));

    delete.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listCartItems.remove(position);
                    notifyDataSetChanged();

                }
            }
    );

    return v;
 }
}

執行listCartItems.remove(position); 僅當位置> 0時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM