我们如何在recylerView中的选定itemView上传递数据

时间:2019-04-15 14:43:32

标签: android android-intent recycler-adapter

我想将所选的ItemView文本(此处为类视图)传递给下一个Activity,以便我可以使用该名称保存其他文件,该怎么做呢?我尝试使用intent进行操作,但它总是给我一个空异常。 这是我的回收站适配器,我在项目视图上的单击侦听器上添加了一个集合,但我想使用放置附加功能传递名称



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

    public List<ClassPost> class_list;
    public Context context;



    public ClassRecyclerAdapter(List<ClassPost> class_list){

        this.class_list = class_list;

    }


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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.class_list_items, parent, false);
        return new ViewHolder(view);
    }

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

        final String class_name_data = class_list.get(position).getClass_name();
        holder.setClassText(class_name_data);



        String class_session_data = class_list.get(position).getClass_session();
        holder.setClassSession(class_session_data);

    }

    @Override
    public int getItemCount() {
        return class_list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public View view;

        public TextView classView , sessionView;


        public ViewHolder(final View itemView) {
            super(itemView);

            view = itemView;


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(), "Welcome to the Class"  , Toast.LENGTH_SHORT).show();
                    Intent newintent = new Intent(view.getContext(), ClassActivity.class);

                    view.getContext().startActivity(newintent);


                }
            });





        }

        public void setClassText(String class_text){

            classView = view.findViewById(R.id.class_name);
            classView.setText(class_text);


        }

        public  void setClassSession (String class_session_text){

            sessionView = view.findViewById(R.id.class_session);
            sessionView.setText(class_session_text);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我不确定发生了什么,但是请尝试以下操作:

  public class ViewHolder extends RecyclerView.ViewHolder{

    public View view;

    public TextView classView , sessionView;


    public ViewHolder(final View itemView) {
        super(itemView);

        view = itemView;
        classView = view.findViewById(R.id.class_name);
        sessionView = view.findViewById(R.id.class_session);
 }

为您提供onBindViewHolder

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



    final String class_name_data = class_list.get(position).getClass_name();
    ((ViewHolder) holder).classView.setText(class_name_data);



    String class_session_data = class_list.get(position).getClass_session();
    ((ViewHolder) holder).sessionView.setText(class_session_data);

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

     Toast.makeText(context, "Welcome to the Class"  , 
     Toast.LENGTH_SHORT).show();
     Intent newintent = new Intent(context, 
          ClassActivity.class);

     context.startActivity(newintent);

        }
    });
}

让我知道是否可以正常工作