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.