繁体   English   中英

如何将字符串从 recycleView 适配器传递给片段

[英]How can I pass string from recycleView adapter to fragment

我正在尝试将字符串从适配器传递到片段并在单击图像时移动到此片段
但它没有用

在我的适配器中


        holder.imageProfile.setOnClickListener(new View.OnClickListener() {
                Intent intent = new Intent(mContext, ProfileFragment.class);
                intent.putExtra("publisherId", post.getPublisher());
                mContext.startActivity(intent);
}

在“onCreateView”的片段中

 Intent intent = getIntent();
 String profileId=intent.getStringExtra("publisherId");

我怎样才能做到这一点?

在你的适配器中

     holder.imageProfile.setOnClickListener(new View.OnClickListener() {
 @Override
            public void onClick(View view) {
                     Fragment fragment = new ProfileFragment();
                    AppCompatActivity activity = (AppCompatActivity) view.getContext();
                    FragmentManager fragmentManager = activity.getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    Bundle bundle = new Bundle();
                    bundle.putString("publisherId", post.getPublisher()); //key and value
                    fragment.setArguments(bundle);
                    fragmentTransaction.replace(R.id.your_fragment_containar, fragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
    }
});

在“onCreate”的片段中

String profileId = getArguments().getString("publisherId");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM