-1

I created a listview adapter which a row contains a button. It works when i clicked on the button , but not when i click on the row This is my main activity

public class MainActivity extends AppCompatActivity {
.
.
.   
ListView lvProducts = (ListView) findViewById(R.id.lvProducts);
lvProducts.addHeaderView(getLayoutInflater().inflate(R.layout.product_list_header, lvProducts, false));

    ProductAdapter productAdapter = new ProductAdapter(this);
    productAdapter.updateProducts(Constant.PRODUCT_LIST);

    lvProducts.setAdapter(productAdapter);

    lvProducts.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Product product = Constant.PRODUCT_LIST.get(position - 1);
            Intent intent = new Intent(MainActivity.this, ProductActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("product", product);
            Log.d(TAG, "View product: " + product.getName());
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });

This is the adapter part

public class ProductAdapter extends BaseAdapter  {
bAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show();
            Cart cart = CartHelper.getCart();
            Log.d(TAG, "Adding product: " + product.getName());
            cart.add(product, 1);
            Intent intent = new Intent(context, ShoppingCartActivity.class);
            context.startActivity(intent);
        }
    });

The xml file of adapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200px"
android:layout_margin="5dp"
android:minHeight="50dp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/tvProductName"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.50"
    android:gravity="center"
    android:text=""/>

<ImageView android:id="@+id/ivProductImage"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.25"
    android:gravity="center"/>

<TextView
    android:id="@+id/tvProductPrice"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.24"
    android:gravity="center"
    android:text=""/>

<Button
    android:id="@+id/tvAddItem"
    android:layout_width="69dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="A"
    />
</LinearLayout>
beginner
  • 1
  • 2
  • can you please paste your xml code of adapter – Hasan shaikh Oct 14 '16 at 08:05
  • Possible duplicate of [Android : How to set onClick event for Button in List item of ListView](http://stackoverflow.com/questions/12596199/android-how-to-set-onclick-event-for-button-in-list-item-of-listview) – H Raval Oct 14 '16 at 08:05
  • what do you mean with "it worked when I clicked on the button but not when I clicked on the row"? If your problem is the button and it worked clicking on the button, I don't get whether the problem is – Pier Giorgio Misley Oct 14 '16 at 08:05
  • it starts different activity...for button it works...it will start shoppingcart activity...another is when i click inside the row...it does not start the productactivity – beginner Oct 14 '16 at 12:12
  • i did a little research on the focusability...i tried the following inside adapter xml for button... `android:focusable="false"`...it works...thanks guys – beginner Oct 14 '16 at 12:24

3 Answers3

1

In your custom layout file set

android:focusable="false"
android:focusableInTouchMode="false"

for your button.

anonymous
  • 401
  • 7
  • 15
0

Since your adapter layout contains the click listener view.i.e, in your case you are using the button view, which will block the listview click listner. So in your adapter parent layout you need add block descendent as below,

android:descendantFocusability="blocksDescendants"
Jeevanandhan
  • 1,073
  • 10
  • 18
  • it does not work...i did a little research on the focusability...i tried the following... `android:focusable="false"`...it works – beginner Oct 14 '16 at 12:22
0

You need to add listener for both button and your parent layout like below:

  public class ProductAdapter extends BaseAdapter implements View.OnClickListener {
    bAdd.setOnClickListener(this);
    layout.setOnClickListener(this);
    @Override
            public void onClick(View v) {
                //Toast.makeText(v.getContext(),"Button "+position,Toast.LENGTH_SHORT).show();
                Cart cart = CartHelper.getCart();
                Log.d(TAG, "Adding product: " + product.getName());
                cart.add(product, 1);
                Intent intent = new Intent(context, ShoppingCartActivity.class);
                context.startActivity(intent);
            }
        }
    }
subrahmanyam boyapati
  • 2,836
  • 1
  • 18
  • 28