user1406716 user1406716 - 2 months ago 6
Java Question

How to detect from Activity class when a certain button in a row in RecylerView is clicked?

I am trying to detect when a certain button (

btn_removeApp
) in a row of a RecyclerView gets triggered. I am expecting that the function, also below,
rvOnClick_btn_AdapterAppsalreadySelected(View view, int position)
gets called when a button in any of the rows in the RecyclerView is clicked, but that does not happen.

Below is my attempt at the code:

MY Interface to detect the onClick from Activity Class

public interface RV_AppsalreadySelected_BtnClickListener {

public void rvOnClick_btn_AdapterAppsalreadySelected(View view, int position);
}


My Adapter_AppsalreadySelected.class:

public class Adapter_AppsalreadySelected
extends RecyclerView.Adapter<Adapter_AppsalreadySelected.MyViewHolder> {

private List<String> selAppsList;
private MyAppLogger mLog = new MyAppLogger();

private Context context;
private RV_AppsalreadySelected_BtnClickListener btn_clickListener;

public class MyViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
public TextView tv_selappname;
public ImageView iv_selappicon;
public Button btn_removeApp;

private String mItem;

public MyViewHolder(View view) {
super(view);
tv_selappname = (TextView) view.findViewById(R.id.tv_selappname);
btn_removeApp = (Button) view.findViewById(R.id.btn_removeApp);

btn_removeApp = (Button) view.findViewById(R.id.btn_removeApp);

btn_removeApp.setOnClickListener(this);
}

public void setItem(String item) {
mItem = item;
tv_selappname.setText(item);
}

@Override
public void onClick(View v) {

if(btn_clickListener != null) {
btn_clickListener.rvOnClick_btn_AdapterAppsalreadySelected(v, getAdapterPosition());
}
}

}

public Adapter_AppsalreadySelected(Context context,
List<String> selAppsList) {
this.context = context;
this.selAppsList = selAppsList;
}

@Override
public Adapter_AppsalreadySelected.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_appsalreadyselected, parent, false);

return new Adapter_AppsalreadySelected.MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(Adapter_AppsalreadySelected.MyViewHolder holder, int position) {
String app = selAppsList.get(position);
holder.tv_selappname.setText(app);
}

@Override
public int getItemCount() {
return selAppsList.size();
}
}


row_appsalreadyselected.xml



<ImageView
android:id="@+id/iv_selappicon"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>

<TextView
android:id="@+id/tv_selappname"
android:layout_width="0dp"
android:layout_weight="0.6"
android:layout_height="wrap_content"
android:text="Apps Name"
android:layout_gravity="center_vertical"
/>

<Button
android:id="@+id/btn_removeApp"
android:layout_width="0dp"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:clickable="false"

/>
</LinearLayout>


MyActivity class: This is where i want to get the onClick Listener:

public class SetupBlocksActivity extends AppCompatActivity
implements
RV_AppsalreadySelected_BtnClickListener {


@Override
public void rvOnClick_btn_AdapterAppsalreadySelected(View view, int position) {
//THIS FUNCTION NEVER GETS TRIGGERED EVEN WHEN I CLICK ON btn_removeApp on ANY OF THE ROWS IN THE RECYCLERVIEW

}

}

Answer

Try to add this line

if (context instanceof RV_AppsalreadySelected_BtnClickListener) 
            btn_clickListener = (RV_AppsalreadySelected_BtnClickListener) context;
Comments