0

I am implementing onclicklistener and onlongclicklistener in my adapter for my recycler view and my recyclerview wont display after inserting Context in the constructor of my adapter class. Below shows my adapter class and activity onCreate

ListingNearbyAdapter.Class

public class ListingNearbyAdapter extends RecyclerView.Adapter<ListingNearbyAdapter.ListingNearbyViewHolder>{
    ArrayList<ListingNearby> listings = new ArrayList<ListingNearby>();
    Context context;

    public ListingNearbyAdapter(ArrayList<ListingNearby> listings, Context context){
        this.listings = listings;
        this.context = context;
    }

    @Override
    public ListingNearbyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listing_nearby_row_layout,parent,false);
        ListingNearbyViewHolder listingNearbyViewHolder = new ListingNearbyViewHolder(view, context, listings);

        return listingNearbyViewHolder;
    }

    @Override
    public void onBindViewHolder(ListingNearbyViewHolder holder, int position) {
        ListingNearby list = listings.get(position);
        holder.property_name.setText("Property Name:  " + list.getProperty_name());
        holder.type.setText("Property Type:  " + list.getType());
        holder.price.setText("Price:  PHP " + list.getPrice());
        holder.distance.setText("Distance:  " + String.valueOf(list.getDistance()) + " km");
    }

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

    public static class ListingNearbyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
        TextView property_name, type, price, distance;
        ArrayList<ListingNearby> listings = new ArrayList<ListingNearby>();
        Context context;

        public ListingNearbyViewHolder(View view, Context context, ArrayList<ListingNearby> listings){
            super(view);
            this.listings = listings;
            this.context = context;
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);

            property_name = (TextView) view.findViewById(R.id.ln_property_name);
            type = (TextView) view.findViewById(R.id.ln_type);
            price = (TextView) view.findViewById(R.id.ln_price);
            distance = (TextView) view.findViewById(R.id.ln_distance);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            ListingNearby listingNearby = this.listings.get(position);

            Intent intent = new Intent(this.context, PropertyDetails.class);
            intent.putExtra("property_id", listingNearby.getProperty_id());
            this.context.startActivity(intent);
        }

        @Override
        public boolean onLongClick(View v) {
            return false;
        }
    }
}

OnCreate:

recyclerView = (RecyclerView) findViewById(R.id.ln_recycler_view);
                    layoutManager = new LinearLayoutManager(this);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setHasFixedSize(true);
                    adapter = new ListingNearbyAdapter(list, this);
                    recyclerView.setAdapter(adapter);

i dont know where is the error, thanks in advance.

Jarvis Millan
  • 341
  • 1
  • 5
  • 17
  • where do i get the context required for the adapter? – Jarvis Millan Nov 20 '16 at 07:39
  • 1
    @Zeeshan why not passing context? Can´t see anywhere a hint in the API that this is not recommended....am I blind? – Opiatefuchs Nov 20 '16 at 07:45
  • 2
    Don't make your class implement `OnClickListener`, instead, create a new listener for each item inside `onBindViewHolder()` – Chisko Nov 20 '16 at 07:53
  • can you show me how? – Jarvis Millan Nov 20 '16 at 07:56
  • sorry, this is a duplicate. Search further, I will have to flag as dup. – Chisko Nov 20 '16 at 07:59
  • Possible duplicate of [RecyclerView onClick](http://stackoverflow.com/questions/24471109/recyclerview-onclick) – Chisko Nov 20 '16 at 08:00
  • @Opiatefuchs why pass context from activity to adapter when context is already there? Doesn't make sense. Its not base adapter or array adapter. – Zeeshan Shabbir Nov 20 '16 at 08:27
  • 1
    @Chisko `"instead, create a new listener for each item inside onBindViewHolder()"` so if you have 1000 items in your `RecyclerView` and you browse them all you will end in 1000 instances of the listener, if you implement `OnClickListener` inside `ViewHolder` you have 10-15 listeners max – pskink Nov 20 '16 at 08:50
  • @pskink RecyclerView is already optimized so you don't have to deal with non-critical issues like this – Chisko Nov 20 '16 at 08:51
  • @Chisko sorry, it is optimized for over allocation of the listeners? do you know when `onBindViewHolder` is called? and do you know how often you will create your listeners? – pskink Nov 20 '16 at 08:53
  • Yes. A list with a 1000 items is very useful to an end user... – Chisko Nov 20 '16 at 08:57
  • @ZeeshanShabbir you mean getting the context out of the `View` ? But what if the context is needed for other work not related to the `View`? – Opiatefuchs Nov 20 '16 at 09:00
  • @Opiatefuchs what other work? can you mention please – Zeeshan Shabbir Nov 20 '16 at 09:02
  • for example, calling another method from another class that needs context. just a simple scenario, no special one. If you want to do some stuff that is not related to the view directly, you also need a context. In some circumstances, it´s needed, than I guess using parameters is useful. – Opiatefuchs Nov 20 '16 at 09:05
  • @Chisko it does not matter if it has 1000 items or 100 items but browsed 5 times top and bottom – pskink Nov 20 '16 at 09:15
  • Feel free to give a good answer with the amount of info and context the OP gave and I'll gladly upvote you or whatever you want – Chisko Nov 20 '16 at 09:18
  • I agree with pskink, that´s a good point, that I also not thought about. For example a chat app, you will have so much views after a while, than I think it´s not a resource saving way. – Opiatefuchs Nov 20 '16 at 09:42
  • danke @Opiatefuchs, thats why `ViewHolder` has `get*Position()` methods – pskink Nov 20 '16 at 09:49
  • Yes it is a valid view. But we went really off the question. – Chisko Nov 20 '16 at 09:51
  • yeah, back to the question: Does the app crash or just nothing happens? – Opiatefuchs Nov 20 '16 at 11:15

0 Answers0