将参数传递给XML中声明的片段

时间:2013-05-08 01:04:59

标签: android android-fragments

我使用ListFragment构建一个简单的应用程序,结构如下:

片段从ListFragment扩展

  • OneListFragment
  • TwoListFragment

活动从FragmentActivity扩展

  • MainActivity
  • DetailActivity

当前逻辑已定义:在MainActivity用户单击OneListFragment中的项目,启动DetailActivity,我需要将DetailActivity的附加内容传递给TwoListFragment,但我不知道如何?

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final Cursor c = (Cursor) mAdapter.getItem(position);

    final Intent intent = new Intent(getActivity(), DetailActivity.class);
    intent.putExtra(KEY, c.getString(0));
    startActivity(intent);
}

detail_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/detailed_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.package.TwoListFragment" />
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

只需在ListActivity中使用putExtra,然后在TwoListFragment中使用getActivity()。getIntent()。get *** Extra()〜