Android - 将数据从片段传递到活动?

时间:2014-10-01 02:33:19

标签: android listview android-fragments android-activity

你能给我一个将listview中的项目从fragment类传递到activity的listview的例子吗?感谢有人帮我谢谢!

1 个答案:

答案 0 :(得分:0)

查看Android开发人员培训网站。它实现了你所要求的,并且是一个更好的答案:

http://developer.android.com/guide/components/fragments.html

public static class FragmentA extends ListFragment {

    OnArticleSelectedListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(noteUri);
    }

}