-3

Can't get my OnClick listener to work. I have made an onClick listener in another fragment but that method is not working in this fragment. If you look at my code, the way I used the onclick listener before that worked was:

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

But it wont work in the code below. For some rease the alertItemView is red and I am not sure why. I did the exact same as my other code but this time around it is not working. Any ideas on why or if you have a better way I can set an onclick listener to get information from each position?

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {
private List<AlertReference> AlertItem;
public Context context;

public AlertAdapter(List<AlertReference> AlertItem, Context context) {
    this.AlertItem = AlertItem;
    this.context = context;
}

@NonNull
@Override
public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.alert_list_item, parent, false);

    return new AlertViewHolder(v);

}

@Override
public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
    final AlertReference alertItemHolder = AlertItem.get(position);

    holder.text_AlertCoin.setText(alertItemHolder.getCoin_asset());
    holder.text_AlertAmount.setText(alertItemHolder.getCoin_amount());

}

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

public class AlertViewHolder extends RecyclerView.ViewHolder {
    public TextView text_AlertCoin;
    public ImageView image_AlertCoin;

    public AlertViewHolder(View alertItemView) {
        super(alertItemView);
        text_AlertCoin = (TextView) 
alertItemView.findViewById(R.id.TV_CoinAsset);            
alertItemView.findViewById(R.id.image_CoinAlert);
    }
}
} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
nyjxrb30
  • 78
  • 1
  • 3
  • 11

2 Answers2

0

Create a referce of that view inside your AlertViewHolderClass and setOnClickListner something like this:

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {
    private List<AlertReference> AlertItem;
    public Context context;

    public AlertAdapter(List<AlertReference> AlertItem, Context context) {
        this.AlertItem = AlertItem;
        this.context = context;
    }

    @NonNull
    @Override
    public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.alert_list_item, parent, false);

        return new AlertViewHolder(v);

    }

    @Override
    public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
        final AlertReference alertItemHolder = AlertItem.get(position);

        holder.text_AlertCoin.setText(alertItemHolder.getCoin_asset());
        holder.text_AlertAmount.setText(alertItemHolder.getCoin_amount());
        holder.alertItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

    }

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

    public class AlertViewHolder extends RecyclerView.ViewHolder {
        public TextView text_AlertCoin;
        public ImageView image_AlertCoin;
        public View alertItemView;

        public AlertViewHolder(View alertItemView) {
            super(alertItemView);
            this.alertItemView = alertItemView;
            text_AlertCoin = (TextView)
                    alertItemView.findViewById(R.id.TV_CoinAsset);
            alertItemView.findViewById(R.id.image_CoinAlert);

        }
    }
}

Hope this helps

Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19
0

This is very simple using a little DataBinding and a custom listener.

Here is your new AlertAdapter:

public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder>  {

    public Context context;
    public AlertListener alertListener;

    private ArrayList<AlertReference> alertItems;

    public AlertAdapter(Context context, ArrayList<AlertReference> alertItems, AlertListener alertListener) {
        this.alertItems = alertItems;
        this.context = context;
    }

    @NonNull
    @Override
    public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AlertViewHolder(DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.alert_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final AlertViewHolder holder, final int position) {
        final AlertReference alertItem = alertItems.get(position);

        holder.binding.setAlertItem(alertItem);
        holder.binding.setHandler(alertListener);
    }

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

    public class AlertViewHolder extends RecyclerView.ViewHolder {

        public AlertBinding binding;

        public AlertViewHolder(View alertItemView) {
            super(alertItemView);

            binding = DataBindingUtil.bind(alertItemView);
        }
    }

    public interface AlertListener {
        public void onAlertTapped(AlertReference reference);
    }
}

I don't know what your layout file looks like, but it would need to be something along these lines:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="AlertBinding">

        <variable
            name="alertItem"
            type="package.for.your.AlertReference" />

        <variable
            name="handler"
            type="package.for.your.AlertListener" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="@{ (view) -> handler.onAlertItemClicked(alertItem) }">

        <TextView
            android:id="@+id/text_alert_coin"
            android:text="@{ alertItem.getCoin_amount() }"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_alert_amount"
            android:text="@{ alertItem.getCoin_asset() }"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</layout>

The <layout> tag at the top enables DataBinding on the layout, and setting the variable on the bound layout in your adapter sets the values in the layout automatically. This way, you can pass your listener through to your layout directly from the Activity or Fragment where you've instantiated your AlertAdapter. Like this:

AlertAdapter alertAdapter = new AlertAdapter(context, yourAlertItemsList, new AlertAdapter.AlertListener() {
    @Override
    public void onAlertItemClicked(AlertReference alertReference){
        // Your alertReference code goes here
    }
});

Also, be sure you're enabling DataBinding by adding this to your module-level build.gradle file:

android {
    ...

    dataBinding {
        enabled true
    }
}

Please let me know if you have any questions!

Edit: More on DataBinding - https://developer.android.com/topic/libraries/data-binding/index.html

LukeWaggoner
  • 8,869
  • 1
  • 29
  • 28