将数据从ListView传递到Fragment

时间:2014-06-04 08:05:43

标签: android listview android-fragments

我的拳头屏幕上有ListView,显示了一些信息。当我点击每个项目时,我在同一页面中有另一个listView,其中包含新信息和textview,我想在文本视图中显示前一个列表视图行,

请您告诉我如何实现这一点,

提前致谢!

这是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="######">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:visibility="gone"
        android:text="test" />

    <TextView
        android:id="@+id/list_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:text="@string/no_message"
        android:textColor="#000000"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium"
        android:gravity="center"></TextView>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</LinearLayout>

这是我的java文件,显示了listview和

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

    View view = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView)view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);

    getActivity().getActionBar().setTitle(R.string.title_forms);

    TextView details = (TextView)view.findViewById(R.id.detail_header);

    details.setVisibility(View.VISIBLE);

    return view;
}

这是我的一个片段,它有onItemClick Listener

公共类ListFragment extends Fragment实现了AdapterView.OnItemClickListener {

public static final String TAG = "ListFragment";

private ListView listView;
private ArrayList<String> testIds;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setHasOptionsMenu(true);
    tripIds = new ArrayList<String>();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);
    listView = (ListView) view.findViewById(R.id.listView);
    listView.setOnItemClickListener(this);

    registerForContextMenu(listView);

    return view;
}

这是我的onItemClick:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    chooseTest(testIds.get(position));

    String selectedTestId = testIds.get(position);

    Helpers.saveToSharedPreferences(getActivity(),
            Constants_Prefs.SELECTED_TOP_LEVEL_RECORD,
            Constants_Keys.SELECTED_TEST_ID,


          selectedTestId);

我不知道如何获得该信息

    FormTypeListFragment passDataTo = new FormTypeListFragment();
    Bundle bundle = new Bundle();
    bundle.putString("DATA", selectedTestId);
    passDataTo.setArguments(bundle);



}

2 个答案:

答案 0 :(得分:0)

在启动片段以显示所选项目之前,使用setArguments()方法在您的案例中添加键值对字符串,然后在您的片段中使用片段方法getArguments()来保留您传递的字符串

答案 1 :(得分:0)

将bundle中的值传递为:

设置:

YourFragment fragmnt = new YourFragment ();
Bundle bundle  =  new Bundle();
bundle.putString("item","name");
fragmnt.setArgument(bundle);

获取:

然后在你的片段中,用:

检索数据(例如在onCreate()中)
Bundle bundle = this.getArguments();
String myValue = bundle.getInt("item", "");