title: How to unset ListView button's onClickListener for a single item - Android
body: <p>I have used a TableLayout to produce the following cart activity output.
   <a href="https://i.stack.imgur.com/lfziq.png" rel="nofollow"><img src="https://i.stack.imgur.com/lfziq.png" alt="output"></a>.</p>
  
  <p>So the listview has 3 listviews(Item,Price,quantity) and a button (for delete)
  I wanted to give a heading to the cart. So, I initalized the first item of the listview as the title ('Item Name','Price','Quantity','Delete')</p>
  
  <p>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)</p>
  
  <p>How should i remove the onClickListener on the first Button and keep it as it is for the rest of the buttons ?</p>
  
  <p><strong>Extra:</strong> 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</p>
  
  <p>cartlayout.xml file</p>
  
  <pre><code>&lt;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"&gt;
  &lt;TableRow&gt;
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/cartlist_layout"
  android:orientation="horizontal"&gt;
  
  &lt;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"
      /&gt;
  &lt;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"
      /&gt;
  
      &lt;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" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
  
          android:id="@+id/button6" /&gt;
  &lt;/TableRow&gt;&gt;
  &lt;/TableLayout&gt;
  </code></pre>
  
  <p>The Java Code:</p>
  
  <pre><code>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&lt;CartItems&gt;();
      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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And the CartAdapter Java Code here:</p>
  
  <pre><code>public class CartListAdapter extends ArrayAdapter&lt;CartItems&gt; {
  Context context;
  int resLayout;
  List&lt;CartItems&gt; listCartItems;
  int pos;
  public CartListAdapter(Context context,int resLayout,List &lt;CartItems&gt; 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;
   }
  }
  </code></pre>