【问题标题】:Android - Passing data from fragment to activity?Android - 将数据从片段传递到活动?
【发布时间】:2014-10-01 02:33:19
【问题描述】:

你能给我一个将列表视图中的项目从片段类传递到活动列表视图的示例吗?如果有人帮助我,谢谢!

【问题讨论】:

    标签: android listview android-fragments android-activity


    【解决方案1】:

    查看 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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多