【问题标题】:android - How to Intent RecyclerView in AdapterClassandroid - 如何在 AdapterClass 中意图 RecyclerView
【发布时间】:2017-06-16 10:15:23
【问题描述】:

我需要我的RecyclerView 可以按下图像并转到下一个活动。我有Adapter 代码。

public class MakananAdapter extends RecyclerView.Adapter<MakananAdapter.ViewHolder> {
ArrayList<Makanan> makananList;

public MakananAdapter(ArrayList<Makanan> makananList)
{
    this.makananList = makananList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Makanan tanaman = makananList.get(position);
    holder.tvJudul.setText(tanaman.judul);
    /*holder.tvHarga.setText(tanaman.harga);*/
    holder.ivFoto.setImageDrawable(tanaman.foto);
    holder.tvJudul.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}



@Override
public int getItemCount() {
    if(makananList!=null)
        return makananList.size();
    return 0;
}

public class ViewHolder extends RecyclerView.ViewHolder {
    ImageView ivFoto;
    TextView tvJudul;
    TextView tvHarga;

    public ViewHolder(View itemView) {
        super(itemView);
        ivFoto = (ImageView) itemView.findViewById(R.id.imageView);
        tvJudul = (TextView) itemView.findViewById(R.id.textViewJudul);
        /*tvHarga = (TextView) itemView.findViewById(R.id.textViewHarga);*/
    }
}}

如何在 AdapterClass 中 Intent RecyclerView?

【问题讨论】:

  • 你有什么问题?
  • 您的 onClick 方法为空...
  • 我的问题是如何从适配器中获取图像,因为我的图像视图来自 arraylist,而不是活动

标签: java android android-recyclerview


【解决方案1】:

让你的构造函数像这样

private Context context;
public MakananAdapter(ArrayList<Makanan> makananList, Context context) {
    this.makananList = makananList;
    this.context = context;
}

初始化适配器时传递context

MakananAdapter = new MakananAdapter(makananList, this);

在你的onClickListner 的适配器中这样做

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

        Intent openNextActivity = new Intent(context, yourActivityName.class);
        context.startActivity(openNextActivity)
     }
});

【讨论】:

  • 这是上述问题的正确答案...需要在构造函数上传递Activity上下文才能使用Intents。
猜你喜欢
  • 1970-01-01
  • 2016-10-07
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
相关资源
最近更新 更多