0

in a cardview i have an imageview, and two textview. I'm having problems trying to figurate out how to change the text of one of the textview when someone click on the cardview....is this even posible?

The card view are controlled by a recyclerview....in total I have to around 20 cardview.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="10dp"
    app:cardCornerRadius="5dp"
    android:foreground="?android:attr/selectableItemBackground">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:background="@color/transparent"
        android:padding="0dp"
        android:scaleType="fitCenter"
        android:src="@drawable/card1" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:text="Text Here"
        android:textColor="@color/red" />

    <TextView
        android:id="@+id/product"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="SUV"
        android:textColor="@color/grey_font"
        android:textSize="11dp" />

Here is the click of the card view, I get the posistion of the cardview, but don't know how to get the textview inside the cardview.

public void onItemClick(int position) {
   Log.e(TAG, "Bien: " + position);
}

public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
    Items  currentItem = vReyclerViewList.get(position);

    String heading      = currentItem.getHeading();
    String title        = currentItem.getTitlee(); 

    holder.vHeading.setText(heading);
    holder.vTitle.setText(title);
}



public class RecyclerViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public TextView product; 

        public RecyclerViewHolder(View itemView) {
            super (itemView);
            title       = itemView.findViewById(R.id.title);
            product         = itemView.findViewById(R.id.product); 

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(vListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            vListener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }
Pedro
  • 1,440
  • 2
  • 15
  • 36
  • 1
    Put an id to the TextView you want to change during card view click event. Then setOnclickListener to the CardView, on the callback of clicklistener get the reference of the TextView if it exists set a new text to it. Simple. – CodeDaily May 13 '19 at 20:57
  • @CodeDaily I can get the position of the cardview, but still lost on how to get the textview :/ – Pedro May 13 '19 at 21:04
  • I see, are you using an adapter ? – CodeDaily May 13 '19 at 21:05
  • post the adapter and I can help you. – CodeDaily May 13 '19 at 21:05
  • @CodeDaily I have updated the questio nwith part of the adapter(view HoldeR) let me know if you need to see all the source code – Pedro May 13 '19 at 21:09
  • Try see here. I think that is a solution for you https://stackoverflow.com/questions/55983616/how-to-implement-two-onclicklisteners-on-recyclerview-item-click/55984886#55984886 – Bogojob May 13 '19 at 21:52

1 Answers1

3

You have to override the onBindViewHolder() method then implement as below.

Override 
public void  onBindViewHolder(RecyclerViewHolder holder, int position) {
   Model model = getItem(position);
   holder.bind(model);
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder {

  public final TextView title;
  public final TextView product;

  public RecyclerViewHolder(View itemView) {
    super (itemView);
    title    = itemView.findViewById(R.id.title);
    product  = itemView.findViewById(R.id.product);
  }

  public void bind(final Model model){
    itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
       title.setText(model.getText());
      }
    });
  }
}
CodeDaily
  • 756
  • 4
  • 17
  • Thanks you for helping me, but I'm really confuse my onBindViewHolder, looks diffenret to what you have :/ sorry for not be at your level on android...im really new on this – Pedro May 13 '19 at 21:40