gpt4 book ai didi

android - 将值从适配器传递到 fragment 类时出现问题?

转载 作者:行者123 更新时间:2023-11-29 19:05:32 25 4
gpt4 key购买 nike

我正在将值从适配器传递到 fragment 类,

这里是适配器类,

 rbFolder.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(selected != null)
{
selected.setChecked(false);
String meee = data.get(position).getTypeId();
System.out.println("*******so*inside********"+meee);

}

rbFolder.setChecked(true);
String so = data.get(position).getTypeId();
System.out.println("*******so*********"+so);
selected = rbFolder;
System.out.println("********selected*******"+selected);
Fragment homepage = new Fragment();
FragmentTransaction fragmentManager =((FragmentActivity)context).getSupportFragmentManager()
.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("name", so); //key and value
System.out.println("*****venki***meee*******"+so);

homepage.setArguments(bundle);
// fragmentManager.replace(R.id.content_frame, homepage);
fragmentManager.addToBackStack(null);
fragmentManager.commit();
}
});

在此,putstring 运行良好,但在我的 fragemt 类中我没有收到值。我在这个地方有麻烦。这里是 Fragmnet 类,

      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootview != null) {
ViewGroup parent = (ViewGroup) rootview.getParent();
if (parent != null)
parent.removeView(rootview);
}
try {

rootview = inflater.inflate(R.layout.homepage, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}

这里我从适配器获取值,但这不起作用,

   Bundle bundle = ((Activity)context).getIntent().getExtras();
if (bundle != null)
{
String strtext=getArguments().getString("name");
System.out.println("*******strtext*********"+strtext);

}

return rootview;
}

最佳答案

首先你必须调用你的HomeFragment() 而不是那样的fragment

Fragment homepage = new HomeFragment();
FragmentTransaction fragmentManager =((FragmentActivity)context).getSupportFragmentManager()
.beginTransaction();
Bundle bundle=new Bundle();
bundle.putString("name", so); //key and value
homepage.setArguments(bundle);
fragmentManager.replace(R.id.content_frame, homepage);
fragmentManager.addToBackStack(null);
fragmentManager.commit();

当你想在 home fragment 中获取值时

    if( getArguments() != null)
String strtext = getArguments().getString("name");

关于android - 将值从适配器传递到 fragment 类时出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com