如何使用Recycler viewHolder setOnClickListener点击事件传递数据?

时间:2017-07-16 12:21:09

标签: android

我在card view内使用recycler view,并在其中放置一个image viewtext view。 在下面的图片点击代码中,我将转到next activity,但intent.putExtra("NATIVE_NAME",getDataAdapter1.getImageTitleName())未通过。在视图中点击我希望数据发送到下一个活动

 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;

    List<GetDataAdapter> getDataAdapter;

    ImageLoader imageLoader1;

    public RecyclerViewAdapter(List<GetDataAdapter> getDataAdapter, Context context){

        super();
        this.getDataAdapter = getDataAdapter;
        this.context = context;
    }

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

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);

        ViewHolder viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder Viewholder, int position) {

        final GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        imageLoader1 = ServerImageParseAdapter.getInstance(context).getImageLoader();

        imageLoader1.get(getDataAdapter1.getImageServerUrl(),
                ImageLoader.getImageListener(
                        Viewholder.networkImageView,//Server Image
                        R.drawable.placeholder,//Before loading server image the default showing image.
                        android.R.drawable.ic_dialog_alert //Error image if requested image dose not found on server.
                )
        );

        Viewholder.networkImageView.setImageUrl(getDataAdapter1.getImageServerUrl(), imageLoader1);

        Viewholder.ImageTitleNameView.setText(getDataAdapter1.getImageTitleName());
        Viewholder.networkImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), SingleView.class);
                intent.putExtra("NATIVE_NAME",getDataAdapter1.getImageTitleName());
                view.getContext().startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {

        return getDataAdapter.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView ImageTitleNameView;
        public NetworkImageView networkImageView ;

        public ViewHolder(View itemView) {

            super(itemView);

            ImageTitleNameView = (TextView) itemView.findViewById(R.id.textView_item) ;

            networkImageView = (NetworkImageView) itemView.findViewById(R.id.VollyNetworkImageView1) ;

        }


    }
}

SingleView.java

 name =(TextView) findViewById(R.id.name);
    Bundle bundle = getIntent().getExtras();

    if (getIntent().getStringExtra("NATIVE_NAME") != null){
        name.setText(bundle.getString("NATIVE_NAME"));

    }
  

我知道这是重复但我尝试了很多方法不适合我。   提前谢谢。

0 个答案:

没有答案