如何从cardview启动其他屏幕

时间:2015-12-23 08:16:34

标签: android

我尝试从持有人类触摸卡片视图时启动新屏幕但是当我点击卡片视图应用程序崩溃时...  这是我的RecyclerViewHolders类的代码

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView countryName;
public ImageView countryPhoto;
public RecyclerViewHolders(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    countryName = (TextView)itemView.findViewById(R.id.tv_cardTitle);
    countryPhoto = (ImageView)itemView.findViewById(R.id.iv_cardImages);
}
@Override
public void onClick(View view) {
    Toast.makeText(view.getContext(), "Clicked Country Position = " + getPosition(), Toast.LENGTH_SHORT).show();
}

}

RecyclerViewAdapter类的代码

公共类RecyclerViewAdapter扩展了RecyclerView.Adapter {

private List<ItemObject> itemList;
private Context context;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
    this.itemList = itemList;
    this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_row, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
    return rcv;
}
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
    holder.countryName.setText(itemList.get(position).getName());
    holder.countryPhoto.setImageResource(itemList.get(position).getPhoto());
}
@Override
public int getItemCount() {
    return this.itemList.size();
}

}

当我试图从RecyclerViewHolder类启动其他屏幕时,我的应用程序正在崩溃,请指导我...

1 个答案:

答案 0 :(得分:0)

使用以下代码:

view.getContext().startActivity(new Intent(view.getContext(),SecondActivity.class));

希望它有用......