从RecyclerViewAdapter启动新的Intent

时间:2015-02-15 16:03:09

标签: android android-intent android-recyclerview recycler-adapter

我有 RecyclerViewAdapter

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {



    private List<Cards> items;
    private int itemLayout;


    Context context;

    public RecycleViewAdapter(List<Cards> items, int itemLayout) {

        this.items = items;
        this.itemLayout= itemLayout;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

       View view=LayoutInflater.from(parent.getContext()).inflate(itemLayout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        Cards item=items.get(position);
        holder.title.setText(item.getName());
        String cardvalue = item.getCountry();
        String cardCode = item.getCode();
        String cardCountry = item.getCountry();


    }

    @Override
    public int getItemCount() {

        return items.size();
    }
    public static class  MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView title;
        public MyViewHolder(View itemView) {

            super(itemView);

            title= (TextView) itemView.findViewById(R.id.listText);

            title.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {


        }
    }
}

我需要启动新的Intent( OnCardsSelected.class ),但我不能将此行添加到我的方法onClick中:

Intent intent = new Intent (this, OnCardSelected.class);

我不明白我做错了什么,我只需要启动Intent并向这个意图提供一些信息,我读了一些手册但是不理解他们的解释所以我希望你会帮助我。谢谢

6 个答案:

答案 0 :(得分:8)

可能尝试从onClick方法启动Activity,然后使用v.getContext()代替this引用onClick方法上下文)作为Intent构造函数的第一个参数:

Intent intent = new Intent (v.getContext(), OnCardSelected.class);

答案 1 :(得分:1)

在onBindViewHolder方法中添加这行代码 Intent intent = new Intent(v.getContext(),YourClassName.class); 。v.getContext()startActivity(意向);

你很高兴。

答案 2 :(得分:0)

public RecycleViewAdapter(Context context,List<Cards> items, int itemLayout) {
    this.mContext = context;
    this.items = items;
    this.itemLayout= itemLayout;
}
...
Intent intent = new Intent(mContext, YOUR_ACTIVITY.class);
mContext.startActivity(intent)

答案 3 :(得分:0)

您已经将上下文传递到适配器中。因此,可以在onBindViewHolder()内部创建意图。

Intent intent = new Intent(context,OnCardsSelected.class); context.startActivity(intent);

答案 4 :(得分:0)

Intent i = new Intent(holder.itemView.getContext(), NewOnCLickAccount.class);
            holder.itemView.getContext().startActivity(i);

对我来说很好。

答案 5 :(得分:-1)

以下是我在适配器的BindViewHolder中使用OnClickListener的示例

@Override
public void onBindViewHolder(@NonNull ItemHolder holder, int position) {

    Item playerItem = (Item) itemList.get(position);

    itemName = playerItem.getName();
    itemPosition = holder.getAdapterPosition();
    itemHolder = holder;

    //Sets Text
    holder.name.setText(itemName);

    //holder.playerName.setTag(R.string.listSize, playerList.size());
    holder.name.setTag(position);

    //holder.image.setImageAlpha(playerItem.getImage());
    holder.image.setImageResource(playerItem.getImage());
    holder.image.setTag(position);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //goes to new activity passing the item name
            Intent intent = new Intent(itemHolder.itemView.getContext(), MapActivity.class);
            Bundle b = new Bundle();

            //get text for current item
            String textGet = itemName;
            //put text into a bundle and add to intent
            intent.putExtra("text", textGet);

            //get position to carry integer
            intent.putExtra("position", itemPosition);

            intent.putExtras(b);

            //begin activity
            itemHolder.itemView.getContext().startActivity(intent);
        }
    });

}