将值从Recyclerview传递到Fragment

时间:2016-09-08 22:29:26

标签: java android-studio android-fragments android-recyclerview

我有一个回收站视图,用于显示行中的用户信息,每行都有 TextView显示名称和另一个textview显示作业;我使用接口在recyclerview中处理OnitemClick来启动片段,它将显示用户数据名称和作业,我尝试使用bundle将来自recyclerview适配器的值传递给片段,但是bundle总是为null,

这是我的Adapter类

public class AuthorAdapter extends RecyclerView.Adapter<AuthorAdapter.MyViewHolder> {
private LayoutInflater inflater;
private Realm mRealm;
private RealmResults<AuthorInfo> results;
private LaunchFragment lanch;

public AuthorAdapter(Context context, RealmResults<AuthorInfo> realmResults, Realm realm, LaunchFragment mLaunch) {
    inflater = LayoutInflater.from(context);
    results = realmResults;
    mRealm=realm;
    lanch=mLaunch;
}


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

    View view = inflater.inflate(R.layout.custom_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view,lanch);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    AuthorInfo current = results.get(position);


    holder.name.setText(current.getName());
    holder.job.setText(current.getJob());


}


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




public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView name;
    TextView job;
    ImageView image;
    CheckBox checkbox;

    ItemClickListener itemclicklistener;

    LaunchFragment launchFragment;

    public MyViewHolder(View itemView,LaunchCards listener) {
        super(itemView);

        name = (TextView) itemView.findViewById(R.id.tv_name);
        job = (TextView) itemView.findViewById(R.id.tv_job);
        image = (ImageView) itemView.findViewById(R.id.iv_author);
        checkbox = (CheckBox) itemView.findViewById(R.id.checkbox);
        checkbox.setOnClickListener(this);
        lanunchFragment=listener;
        image.setOnClickListener(this);

    }

    public void setClickListener(ItemClickListener ic) {
        this.itemclicklistener = ic;

    }


    @Override
    public void onClick(View view) {


        Fragment fragmentGet = new FragmentCard();
        Bundle bundle = new Bundle();
        bundle.putString("name",results.get(getAdapterPosition()).getName());
        fragmentGet.setArguments(bundle);

        lanunchFragment.onMark(getAdapterPosition());

    }
}

}

这里是Fragment类

public class FragmentCard extends Fragment {
TextView name;
TextView job;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.card_fragment,container,false);

    Bundle arguments=getArguments();
    if (arguments !=null){
    String nameUser=arguments.getString("name");
    String jobUser=arguments.getString("job");

    name= (TextView)view.findViewById(R.id.get_author);
    job= (TextView)view.findViewById(R.id.get_job);

    name.setText(nameUser);
    job.setText(jobUser);}

    return view;
}

}

所以任何想法为什么bundle总是空的?

0 个答案:

没有答案