Now that I got my RecyclerView working, and I can communicate between my Activity and my Adapter, I wanted to create an OnClickListener, which I did with success.
But somehow, it only works on buttons, not on other items.
In the Layout of my RecyclerView, I am having the following:
<TextView
android:id="@+id/tvWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:singleLine="true"
android:text="tvWebsite" />
<Button
android:id="@+id/btnWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
So the TextView is clickable. The button is too, but that is default behaviour.
In my Adapter class, I am having the following:
public class CompanyAdapter extends RecyclerView.Adapter<CompanyAdapter.ViewHolder>
{
private Context mContext;
private ArrayList<Company> companyname;
public CompanyAdapter (Context context, ArrayList<Company> list)
{
this.mContext = context;
companyname = list;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView tvCompanyName;
TextView tvVatID;
TextView tvPostalCode;
TextView tvPhoneNumber;
TextView tvProprietorName;
TextView tvWebsite;
TextView tvEmail;
TextView tvFirstAvailableDate;
TextView tvFirstAvailableTimeBlock;
TextView tvAvailableInPostalCode;
TextView tvAvailableNumberOfPersons;
Button btnWebsite;
public ViewHolder(@NonNull final View itemView)
{
super(itemView);
tvCompanyName = itemView.findViewById(R.id.tvCompanyName);
tvVatID = itemView.findViewById(R.id.tvVatNo);
tvPostalCode = itemView.findViewById(R.id.tvPostalCode);
tvPhoneNumber = itemView.findViewById(R.id.tvPhoneNumber);
tvProprietorName = itemView.findViewById(R.id.tvProprietorName);
tvWebsite = itemView.findViewById(R.id.tvWebsite);
tvEmail = itemView.findViewById(R.id.tvEmail);
tvFirstAvailableDate = itemView.findViewById(R.id.tvFrstAvailableDate);
tvFirstAvailableTimeBlock = itemView.findViewById(R.id.tvFirstAvailableTimeBlock);
tvAvailableInPostalCode = itemView.findViewById(R.id.tvPostalCodeAvailable);
tvAvailableNumberOfPersons = itemView.findViewById(R.id.tvAvailableNumberOfPersons);
btnWebsite = itemView.findViewById(R.id.btnWebsite);
tvWebsite.setClickable(true);
tvWebsite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
if (mContext instanceof SearchForHelp) {
((SearchForHelp) mContext).ClickWebsite();
}
}
});
btnWebsite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mContext instanceof SearchForHelp) {
((SearchForHelp) mContext).ClickButton();
}
}
});
}
@Override
public void onClick(View view) {
}
}
@NonNull
@Override
public CompanyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_items, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull CompanyAdapter.ViewHolder viewHolder, int i) {
viewHolder.itemView.setTag(companyname.get(i));
viewHolder.tvCompanyName.setText(companyname.get(i).getStrCompanyName());
viewHolder.tvVatID.setText(companyname.get(i).getStrVatID());
viewHolder.tvPostalCode.setText(companyname.get(i).getStrPostalCode());
viewHolder.tvPhoneNumber.setText(companyname.get(i).getStrPhoneNumber());
viewHolder.tvProprietorName.setText(companyname.get(i).getStrProprietorName());
viewHolder.tvWebsite.setText(companyname.get(i).getStrWebsite());
viewHolder.tvEmail.setText(companyname.get(i).getStrEmail());
viewHolder.tvFirstAvailableDate.setText(companyname.get(i).getStrFirstAvailableDate());
viewHolder.tvFirstAvailableTimeBlock.setText(companyname.get(i).getStrFirstAvailableTimeBlock());
viewHolder.tvAvailableInPostalCode.setText(companyname.get(i).getStrAvailableInPostalCode());
viewHolder.tvAvailableNumberOfPersons.setText(companyname.get(i).getStrAvailableNumberOfPersons());
viewHolder.tvWebsite.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
//Toast.makeText(getClass(),"Test",Toast.LENGTH_SHORT).show();
//viewHolder.tvCompanyName.setText("Changed");
}
});
}
@Override
public int getItemCount() {
return companyname.size();
}
}
And finally, in my Activity:
public void ClickButton()
{
Toast.makeText(getApplicationContext(),"Clicked on Button",Toast.LENGTH_LONG).show();
}
public void ClickWebsite()
{
Toast.makeText(getApplicationContext(),"Clicked on TextView",Toast.LENGTH_LONG).show();
}
Please note, that
tvWebsite.setClickable(true);
has been added to see if it made any difference, but it didn't.
If I click on the button, it toastes "Clicked on button", but if I click on the TextView, nothing happens.
So what do I miss in order to make the TextView tvWebsite clickable?