我正在我的适配器中实现onclicklistener
和onlongclicklistener
以获取我的回收站视图和
在我的适配器类的构造函数中插入Context后,我的recyclerview
不会显示。下面显示了我的适配器类和活动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);
我不知道错误在哪里,提前谢谢。