我有这个在click事件中获得RecyclerView位置:
List<Posts> Posts;
public Posts getItem(int position) {
return Posts.get(position);
}
和此函数返回一个位置列表:
public String getPid() {
return idp;
}
在这篇帖子列表中我有:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
//Imageloader to load image
private ImageLoader imageLoader;
private final Context context;
//List to store all posts
List<Posts> Posts;
public Posts getItem(int position) {
return Posts.get(position);
}
//Constructor of this class
public CardAdapter(List<Posts> Post, Context context){
super();
//Getting all
this.Posts = Post;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.posts_list, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Getting the particular item from the list
Posts post = Posts.get(position);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
//Showing data on the views
holder.imageView.setImageUrl(post.getImageUrl(), imageLoader);
holder.textViewName.setText(post.getName());
holder.textViewPublisher.setText(post.getPublisher());
holder.setIsRecyclable(false);
}
@Override
public int getItemCount() {
return Posts.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
//Views
public NetworkImageView imageView;
public TextView textViewName;
public TextView textViewPublisher;
//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewPublisher = (TextView) itemView.findViewById(R.id.textViewPublisher);
textViewName.setClickable(true);
textViewName.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
// what to do next?
}
}
}
我想知道的是我如何从我想要的位置获得这个idp值。
我的代码:
def jsonTxt = '''{
"hotels": [{
"roomInformation": [{
"roomPrice": 618.4
}],
"totalPrice": 618.4
},
{
"roomInformation": [{
"roomPrice": 679.79
}],
"totalPrice": 679.79
}
]
}'''
答案 0 :(得分:1)
int position = getAdapterPosition();
Post p = getItem(position);
但是,你真的需要这个职位吗?
ViewHolder可以保留Post ...
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
// Data
public Post post;
//Views
public NetworkImageView imageView;
并且
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Getting the particular item from the list
Posts post = Posts.get(position); // or use getItem(position);
holder.post = post;
// You could even make a holder.position variable... your choice.
然后,只需使用onClick
中的ViewHolder.this.post
即可