RecyclerView.Adapter get position of a position in my list
I created RecyclerView.Adapter
and I want to populate the list. So now my problem is that I don't know how to implement the get position for my list.
below you can see my code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<Result> _contents;
public RecyclerViewAdapter(List<Result> contents) {
this._contents = contents;
}
@Override
public int getItemCount() {
return _contents.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View view = null;
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_small, parent, false);
Result tempResult = _contents.get(position);
TextView temp = (TextView) view.findViewById(R.id.text_city_name);
temp.setText(tempResult.getInfo().getCity().getName());
return new RecyclerView.ViewHolder(view) {
};
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
}
}
therefore the function onCreateViewHolder
does not get the ID properly it is always the same ID. How can I fix this or implement to get the correct position of the mi list? ΒΏWhat is happening now is listing the correct number of items, but always the same item first. I think it is simple, but I cannot figure out how to implement it.
Thank!!
source to share
I fixed the problem implementing my own ViewHolder, I was a little confused. Below is the code that works correctly.
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
List<Result> _contents;
public RecyclerViewAdapter(List<Result> contents) {
this._contents = contents;
}
@Override
public int getItemCount() {
return _contents.size();
}
@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = null;
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_small, parent, false);
return new RecyclerViewAdapter.ViewHolder(view);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Result tempResult = _contents.get(position);
holder.mTextView.setText(tempResult.getInfo().getCity().getName());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.text_city_name);
}
}
}
source to share
I'm not sure why you want the position of your list. You can get the position in onBindViewHolder . However, you can put any data in the ViewHolder and use it later.
public class RecyclerViewAdapter extends RecyclerView.Adapter<ResultViewHolder> {
List<Result> _contents;
public RecyclerViewAdapter(List<Result> contents) {
this._contents = contents;
}
public Result getItem(int position) {
return _contents != null ? _contents.get(position) : null;
}
@Override
public int getItemCount() {
return _contents.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card_small, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(ResultViewHolder holder, int position;) {
Result result = getItem(position);
holder.position = position;
holder.result = result;
holder.cityName.setText(result.getInfo().getCity().getName());
}
public static class ResultViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
int position = -1;
Result result;
TextView cityName;
public ResultViewHolder(View view) {
super(view);
cityName = (TextView) view.findViewById(R.id.text_city_name);
view.setOnClickListener(this);
}
public void onClick(View v) {
// do whatever you want with list posisiton and result...
}
}
}
source to share
The onBindViewHolder method receives the position as the second argument. you can manipulate your view in this method.
package com.virgooll.avaa.shop.views;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.virgooll.avaa.R;
import com.virgooll.avaa.detailedIndex.model.StoreItem;
import java.util.List;
public class MasonryAdapter extends RecyclerView.Adapter<MasonryAdapter.StoreItemViewHolder> {
List<StoreItem> items;
int itemsIndex;
public MasonryAdapter(List<StoreItem> items) {
this.items = items;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public MasonryAdapter.StoreItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
itemsIndex = i;
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detailed_index_list_item, viewGroup, false);
MasonryAdapter.StoreItemViewHolder pvh = new MasonryAdapter.StoreItemViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(MasonryAdapter.StoreItemViewHolder holder, int position) {
holder.setImage(position);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class StoreItemViewHolder extends RecyclerView.ViewHolder {
StoreItemViewHolder(View itemView) {
super(itemView);
}
public void setImage(int position) {
ImageView image = (ImageView) itemView.findViewById(R.id.detailed_index_fragment_inner_list_view_image);
image.setImageResource(items.get(position).photoId);
}
}
}
source to share