我在Start Activity
课程中有Adapter
,我使用此代码但不起作用。我使用Recycler View
而不是列表视图。
如何更正此代码?
public class CardViewDataAdapter_smiley extends RecyclerView.Adapter<CardViewDataAdapter_smiley.ViewHolder> {
public String[] mDataset;
static public Context context;
public CardViewDataAdapter_smiley(String[] myDataset) {
mDataset = myDataset;
}
public CardViewDataAdapter_smiley(Context context) {
this.context = context;
}
@Override
public CardViewDataAdapter_smiley.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_smiley, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.tvtinfo_text.setText(mDataset[position].toString());
viewHolder.versionName = mDataset[position];
}
@Override
public int getItemCount() {
return mDataset.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView tvtinfo_text;
public String versionName;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
tvtinfo_text = (TextView) itemLayoutView
.findViewById(R.id.cardview_title_smiley);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,SMS_Smiley.class);
context.startActivity(intent);
}
});
itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(v.getContext(), test text" + versionName,
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}}
答案 0 :(得分:2)
可能context
是null
:
使用单个构造函数传递String Array和Context:
public CardViewDataAdapter_smiley(String[] myDataset,Context context) {
this.mDataset = myDataset;
this.context = context;
}
或
没有在构造函数中传递Context而使用v.getContext()
来启动Click on Click:
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),SMS_Smiley.class);
v.getContext().startActivity(intent);
}