简体   繁体   English

如何为单个项目取消设置ListView按钮的onClickListener-Android

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

I have used a TableLayout to produce the following cart activity output. 我已使用TableLayout生成以下购物车活动输出。 输出 .

So the listview has 3 listviews(Item,Price,quantity) and a button (for delete) I wanted to give a heading to the cart. 所以listview有3个listviews(项目,价格,数量)和一个按钮(用于删除),我想给购物车一个标题。 So, I initalized the first item of the listview as the title ('Item Name','Price','Quantity','Delete') 因此,我将列表视图的第一个项目初始化为标题(“项目名称”,“价格”,“数量”,“删除”)

Now The problem is, Whenever the user clicks on delete button (as circled in the pic), The title also gets deleted. 现在的问题是,每当用户单击“删除”按钮(如图中圆圈所示)时,标题也会被删除。 (Since the onClickListener has been applied to all buttons by default in the adapter class) (由于onClickListener默认情况下已应用于适配器类中的所有按钮)

How should i remove the onClickListener on the first Button and keep it as it is for the rest of the buttons ? 我应该如何删除第一个Button上的onClickListener并保持其余按钮的原样?

Extra: If you guys can suggest some awesome way to format this cart so that it doesnt look so naive, It'd be great. 另外:如果你们可以建议一些很棒的方式格式化购物车,以免显得天真,那就太好了。 :) Happy Diwali and Thanks in Advance. :)排灯节快乐,在此先感谢。 - Android newBee -Android newBee

cartlayout.xml file 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>

The Java Code: 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;
}

} }

And the CartAdapter Java Code here: 这里的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;
 }
}

execute listCartItems.remove(position); 执行listCartItems.remove(position); only when position > 0. 仅当位置> 0时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android ListView和OnClickListener:如何获取所选项目 - Android ListView and OnClickListener: How to get the selected item Android:如何将Button的OnClickListener设置为ListView? - Android: How to set OnClickListener for Button is ListView? 如何在Android的ListView中为按钮设置OnClickListener? - How to set the OnClickListener for button in ListView in Android? android在列表视图中的按钮上设置onclicklistener - android setting onclicklistener on button in listview 如何为自定义listView项的单独部分设置onClickListener? [Android]产品 - How to set onClickListener for separate parts of custom listView item? [Android] 如何在每个列表视图的项目中更新按钮的文本?(Android) - How to update a button's text in each listview's item ?(Android) 如何在Android的ListView标头中将OnClickListener添加到图像按钮? - How to add OnClickListener to image button in the ListView Header in Android? 如何处理Android ListView的项目单击按钮和特定区域? - How to handle Android ListView's item click for button and a specific area ? 带有ListView的Android AsyncTask包含Button onClickListener - Android AsyncTask with ListView containing Button onClickListener 如何在android中显示特定的单行项目onclick的listview项目以及上一个/下一个按钮? - How to show particular single row item onclick of listview item along with previous / next button in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM