0

I have a problem, I have a button that is in a layout without activity (without class), the layout is called item_list_products. That button is there, but I want to use the .setOnClickListener event in my HomeFragment. Is there a way to solve that? since that is why it gives me the error.

Mi codigo: HomeFragment.class

       public class HomeFragment extends Fragment {
          private HomeViewModel homeViewModel;
          Button bttn_comprar;
          public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
          homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
          View root = inflater.inflate(R.layout.fragment_home, container, false);
          bttn_comprar = (Button) root.findViewById(R.id.comprarBttn);

          //This is where I get the error, since the button is in a different layout.
          bttn_comprar.setOnClickListener(new View.OnClickListener() {
                     @Override
                     public void onClick(View view) {

                     }
          });
        }

Mi codigo de layout: item_list_productos

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/idImagen"
            android:layout_width="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginRight="10dp"/>

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

            <TextView
                android:id="@+id/nombreId"
                android:layout_marginTop="10dp"
                android:layout_marginRight="10dp"
                android:text="Nombre producto"
                android:layout_marginBottom="5dp"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/precioId"
                android:text="Precio"
                android:textSize="20dp"
                android:textColor="#000000"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:id="@+id/comprarBttn"
                android:layout_width="230dp"
                android:layout_height="35dp"
                android:textColor="#FFFFFF"
                android:background="#248761"
                android:text="Comprar"/>

        </LinearLayout>
    </LinearLayout>


</LinearLayout>
Kuls
  • 2,047
  • 21
  • 39
  • 1
    where is your `fragment_home` xml – Md. Asaduzzaman Nov 20 '19 at 07:49
  • Instead of `fragment_home` use `item_list_productos` as your `comprarBttn` button in this layout – Md. Asaduzzaman Nov 20 '19 at 07:50
  • If `item_list_products` is the layout for a `ListView` or `RecyclerView` `Adapter`, you cannot set the `Button`'s `OnClickListener` like that. It would need to be handled in the `Adapter`. – Mike M. Nov 20 '19 at 07:57
  • @Mike M Exactly, thanks for understanding my problem that is what I try to do. But how do I handle it on the adapter? – Felipe Rosado Nov 20 '19 at 17:47
  • It kinda depends on whether it's in a `ListView` or a `RecyclerView`. First, let me say that list items generally don't have a single `Button` in them. If there's only one click action per item, the usual pattern is to have a click on the whole item itself handle that. If you change to that design, and remove the ` – Mike M. Nov 21 '19 at 03:44
  • There's a `RecyclerView` example in [this answer](https://stackoverflow.com/a/40584425), where you would simply change the `itemView.setOnClickListener(this)` call in the `ViewHolder` to use your `Button` instead. There are similar examples for `ListView` out there, too, but I'm not sure which you're using. – Mike M. Nov 21 '19 at 03:44

1 Answers1

0

You're trying to call onClicklisterner on a view which is not a child view of your fragment layout

public class HomeFragment extends Fragment {
      private HomeViewModel homeViewModel;
      Button bttn_comprar;
public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
      homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
      View root = inflater.inflate(R.layout.item_list_productos, container, false);

      bttn_comprar = (Button) root.findViewById(R.id.comprarBttn);

      bttn_comprar.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {

                 }
      });
    }

Change your fragment layout to item_list_productos.xml or add your button comprarBttn to your fragment_home.xml layout.

CHINNA CHARY
  • 21
  • 1
  • 7