如何通过数据之间的活动在Android中使用意向片段 [英] How to pass the data in between activity to fragment using intent in Android

查看:152
本文介绍了如何通过数据之间的活动在Android中使用意向片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以存储的价值在一个变量,现在我想通过该变量为碎片。

I can store the value in one variable now I want pass that variable into fragment.

通过以​​下code,我能够加载片段。

With the following code I am able to load fragments.

   public class AndroidListFragmentActivity extends Activity {
Fragment2 f2;
public static String itemname;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apetiserfragement);
    itemname=getIntent().getStringExtra("itemname");
    Bundle args=new Bundle();
    args.putString("itemname", itemname);
    f2=new Fragment2();
    f2.setArguments(args);

}
}

(在这里我使用XML页面加载片段)了itemname

(Here I load fragment using XML page) itemname

输出被分成成2个窗口之一延长listfragment(用于列表视图)。一个用于片段

The output is split into into 2 windows one for extend for listfragment(for listview). One for fragments

Fragment2.xml

Fragment2.xml

    public class Fragment2 extends Fragment {
String itemname;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    System.out.println(getArguments().getString("itemname"));

    return inflater.inflate(R.layout.fragment2, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

}

  }

AndroidListFragmentActivity在这个类了itemname我想通过Fragment2.class。

AndroidListFragmentActivity in this class itemname I want pass Fragment2.class.

推荐答案

使用中的一个<提供的包一个href="http://developer.android.com/reference/android/app/Fragment.html#instantiate%28android.content.Context,%20java.lang.String,%20android.os.Bundle%29">fragment构造的,然后你应该能够做<$检索捆绑 C $ C> getArguments()就在你要在 onCreateView()

Use the bundle provided in one of the fragment constructors, and then you should be able to retrieve the Bundle by doing getArguments() right where you want to in onCreateView()

例如:

YourFragment.instantiate(context,"fragName",yourBundleWithValue);

然后,在你的片段:

Then, in your fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    getArguments().getStringValue("name"); //Returns your string value

    return inflater.inflate(R.layout.fragment2, container, false);
}

这篇关于如何通过数据之间的活动在Android中使用意向片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆