以前我使用的是火力回收器适配器,我使用最终字符串NameKey=getRef(位置). getKey();获取我的子值,并使用putExtraIntent发送该值并在另一个活动中获取该值。现在我使用自定义回收器适配器,我无法使用此代码获取键值并在putExtraIntent中使用它。我怎么能做到这一点?
以前使用Firebase适配器的FirebaseRecyclerAdapter适配器=new FirebaseRecyclerAdapter(选项){
@Override
public NameHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.author_list_cv, parent, false);
return new NameHolder(view);
}
@Override
protected void onBindViewHolder(NameHolder holder, int position, AuthorsCLA model) {
final String AuthorNameKey = getRef(position).getKey();
holder.name.setText(model.getAuthorname());
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
quotesListIntent.putExtra("nameID", AuthorNameKey);
startActivity(quotesListIntent);
}
}); `
现在我正在尝试这个
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
final AuthorsCLA authorsModel = authorlist.get(holder.getAdapterPosition());
final String AuthorNameKey = getRef(position).getKey();
switch (holder.getItemViewType()) {
case A: {
NamesHolder nameHolder = (NamesHolder) holder;
nameHolder.name.setText(authorsModel.getAuthorname());
nameHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), AuthorQuotesDetailActvity.class);
intent.putExtra("nameID",AuthorNameKey );
mcontext.startActivity(intent);
Toast.makeText(view.getContext(), authorsModel.getAuthorname(), Toast.LENGTH_SHORT).show();
}
});
但是getRef显示一个错误,当我将鼠标悬停在它上面时,它说无法解析方法getRef(int)。
我觉得问题是你用的是新的
com.android.support:回收r视图-v 7:
回收站视图和它的工作方式有点不同。这是一个教程,基本上来自该课程:
(onCreateViewHolder
正在设置ViewHolder
并且不需要GetRef()
,但是如果您不创建自定义,则不存在
public class Recycler_View_Adapter extends RecyclerView.Adapter<View_Holder> {
List<Data> list = Collections.emptyList();
Context context;
public Recycler_View_Adapter(List<Data> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public View_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate the layout, initialize the View Holder
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
View_Holder holder = new View_Holder(v);
holder .itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder .getAdapterPosition();
// This is org.greenrobot.eventbus
Application.getInstance().getEventBus().post(new OnHistoryClickEvent(position));
}
});
holder .itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int position = holder .getAdapterPosition();
// This is org.greenrobot.eventbus
Application.getInstance().getEventBus().post(new OnHistoryLongClickEvent(position));
return true;
}
});
return holder;
}
@Override
public void onBindViewHolder(View_Holder holder, int position) {
//Use the provided View Holder on the onCreateViewHolder method to populate the current row on the RecyclerView
holder.title.setText(list.get(position).title);
holder.description.setText(list.get(position).description);
holder.imageView.setImageResource(list.get(position).imageId);
//animate(holder);
}
@Override
public int getItemCount() {
//returns the number of elements the RecyclerView will display
return list.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
// Insert a new item to the RecyclerView on a predefined position
public void insert(int position, Data data) {
list.add(position, data);
notifyItemInserted(position);
}
// Remove a RecyclerView item containing a specified Data object
public void remove(Data data) {
int position = list.indexOf(data);
list.remove(position);
notifyItemRemoved(position);
}
}
那个View_Holder
public class View_Holder extends RecyclerView.ViewHolder {
CardView cv;
TextView title;
TextView description;
ImageView imageView;
View_Holder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
}
用这个
nameHolder.name.setTag(position);
nameHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = (int) view.getTag();
String AuthorNameKey = getRef(position).getKey()
Intent quotesListIntent = new Intent(AuthorsList.this, AuthorQuotesDetailActvity.class);
quotesListIntent.putExtra("nameID", AuthorNameKey);
startActivity(quotesListIntent);
}
});