Reputation: 93
I'm stuck with this problem: the AdapterPosition never changes, when I click on an item from my recyclerview it's always -1.
public class RecyclerMapAdapter extends RecyclerView.Adapter<RecyclerMapAdapter.ViewHolder> {
Context context;
ArrayList<Features> features;
ImageLoader imageLoader;
public static String TAG = MapsActivity.class.getSimpleName();
public RecyclerMapAdapter(Context context, ArrayList<Features> features) {
this.context = context;
this.features = features;
}
@Override
public RecyclerMapAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_row, parent, false);
final RecyclerMapAdapter.ViewHolder viewHolder = new RecyclerMapAdapter.ViewHolder(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListMapActivity mapsActivity = (ListMapActivity) context;
Intent intent = new Intent(mapsActivity, DescriptionMapActivity.class);
Features feature = features.get(viewHolder.getAdapterPosition());
MyProperties myProperties = feature.getProperties();
intent.putExtra("data", myProperties);
mapsActivity.startActivity(intent);
}
});
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Features feature = features.get(position);
MyProperties myProperties = feature.getProperties();
holder.title_map.setText(myProperties.getName());
imageLoader = ServerImageAdapter.getInstance(context).getImageLoader();
imageLoader.get(myProperties.getImage_url(),
ImageLoader.getImageListener(
holder.iv_map,//Server Image
R.mipmap.ic_launcher,//Before loading server image the default showing image.
android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
)
);
holder.iv_map.setImageUrl(myProperties.getImage_url(), imageLoader);
}
@Override
public int getItemCount() {
return features.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
public TextView title_map;
public TextView dist;
public NetworkImageView iv_map;
public ViewHolder(View itemView) {
super(itemView);
title_map = (TextView) itemView.findViewById(R.id.tv_map_list);
dist = (TextView) itemView.findViewById(R.id.dist_map);
iv_map = (NetworkImageView) itemView.findViewById(R.id.map_row_image);
}
}
}
To help you understand the context here's how my stuff works:
I'm on a map with markers, I can open new activity by clicking the InfoWindow for each marker, and I also have a menu item which open the recyclerview with all the markers and when I click on an item from the list it should open the same activity as when I clicked on infoWindow.
I have everything except when I click on an item from the list because getAdapterPosition() return -1.
I already use this kind of implementation of recyclerview for something else and it works fine the only difference is one is fragment (which works) and the other is activity(which doesn't).
Upvotes: 1
Views: 1248
Reputation: 4513
you were creating a viewHolder, performing operations on it. but at the end, you were sending completely new instance of view holder to RecyclerView
@Override
public RecyclerMapAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.map_row, parent, false);
final RecyclerMapAdapter.ViewHolder viewHolder = new RecyclerMapAdapter.ViewHolder(v);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ListMapActivity mapsActivity = (ListMapActivity) context;
Intent intent = new Intent(mapsActivity, DescriptionMapActivity.class);
Features feature = features.get(viewHolder.getAdapterPosition());
MyProperties myProperties = feature.getProperties();
intent.putExtra("data", myProperties);
mapsActivity.startActivity(intent);
}
});
return viewHolder;
}
Upvotes: 1