【问题标题】:Get Activity Result in Fragment获取片段中的活动结果
【发布时间】:2015-07-21 04:02:03
【问题描述】:

默认情况下,在创建我的主片段启动时,我有一个导航抽屉。现在从我的 mainactivity 转到下一个活动,在那里我执行一些操作并使用 onActivityResult 返回到 mainactivity。

到目前为止,一切正常。我的问题来了。

我的 HomeFragment 中有一个列表视图,因此在 onActivityResult 中我得到了在其他活动中执行的值。现在如何将这些值传递给我的主片段。

这就是我从 mainactivity 启动新活动的方式:

Intent i = new Intent(MainActivity.this, AddTask.class);
startActivityForResult(i,1);

现在在 AddTask 中,我执行一些操作并发送回主活动,如下所示:

Intent i = new Intent(AddTask.this,MainActivity.class);
setResult(RESULT_OK, i);
finish();

现在在我的 MainActivity onActivityResult 中:

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == 1) {
                if(resultCode == RESULT_OK){
                     AddedTask = data.getStringExtra("NewTask");
                     CategoryName= data.getStringExtra("CategoryItem");
                     TaskTime=data.getStringExtra("TaskTime");

                    SharedPreferences settings = getSharedPreferences("taskdetails", 
                         Context.MODE_PRIVATE);

                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("NewTask", AddedTask);
                    editor.putString("CategoryItem", CategoryName);
                    editor.putString("TaskTime", TaskTime);
                    editor.commit();

                }
                if (resultCode == RESULT_CANCELED) {
                    //Write your code if there's no result
                }
            }
     }

现在我的 HomeFragment 中有一个 listView,我需要在其中更新这些值:

public class HomeFragment extends Fragment  {
    ListView lv;
    static final String TAG ="HomeFragment";
    private AdapterListViewData adapterListViewData; 
    private ArrayList<DataShow> listData = new ArrayList<DataShow>();
    private ListView listViewData;
    String CategoryName,TaskTime;
    String AddedTask;
    public HomeFragment(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        listViewData = (ListView)rootView.findViewById(R.id.listViewData);

        SharedPreferences settings = this.getActivity().getSharedPreferences("taskdetails", 
                Context.MODE_PRIVATE);
         AddedTask = settings.getString("NewTask",null);
         CategoryName= settings.getString("CategoryItem",null);
         TaskTime=settings.getString("TaskTime",null);
         listData.add(new DataShow(AddedTask,CategoryName,TaskTime));
         adapterListViewData = new AdapterListViewData(getActivity().getBaseContext(),listData);
         adapterListViewData.notifyDataSetChanged();
        return rootView;
  }



}

我没有收到任何错误,并且未显示列表视图。请给我建议。

【问题讨论】:

  • @downvoter-关注评论
  • 我已经发布了 2 个答案。我认为我最新的使用 newInstance() 足以满足您的需求。

标签: android android-fragments android-listview


【解决方案1】:

据我所知,您从未将适配器设置为您的 listView。

因此,在创建新的 AdapterListViewData 和执行 .notifyDataSetChanged() 之间添加以下内容

listViewData.setAdapter(adapterListViewData);

【讨论】:

  • 问题不是设置适配器而是无法获取添加到listview的详细信息。
【解决方案2】:

有不同的方法可以实现这一目标。

我主要使用事件总线在片段和活动之间进行通信。 您可以使用 Square 团队强烈推荐的 Otto 事件总线。

http://square.github.io/otto/

您可以Subscribe 参加Fragment 中的活动。当您在ActivityProduce 中获得结果时,该事件并发送到您的Fragment

【讨论】:

    【解决方案3】:

    MainActivity 中的HomeFragmentonCreateView() 和onActivityResult() 之间似乎存在时间问题。 Google 建议将 Bundle 用于数据参数,使用 Interface 用于 Activity 和 Fragment 之间的通信。设置起来并不难,而且对于您的应用中的其他用途而言,它是很好的知识。

    不管怎样,我过去已经发布了一个带有详细解释的答案@Passing data between fragments contained in an activity(搜索我的用户名,如果没有立即找到)。 我想展示一个 sn-p,将数据发送到片段的示例,来自 Google 网页 @Communicating with Other Fragments

    // Create fragment and give it an argument for the selected article
    ArticleFragment newFragment = new ArticleFragment();
    Bundle args = new Bundle();
    args.putInt(ArticleFragment.ARG_POSITION, position);
    newFragment.setArguments(args);
    

    【讨论】:

      【解决方案4】:

      我只是想到了另一个可能更好、更简单的答案。由于您依赖于 HomeFragmentonCreateView(),因此您可以通过 newInstance() 传递数据。谷歌网页@Fragment。搜索文本“static MyFragment newInstance”就是一个很好的例子。对于 Activity,在文本中搜索“Fragment newFragment = MyFragment.newInstance("From Arguments")”。在该网页上为您的 HomeFragment 编写代码 sn-ps:

      public static class MyFragment extends Fragment {
          CharSequence mLabel;
      
          /**
           * Create a new instance of MyFragment that will be initialized
           * with the given arguments.
           */
          static MyFragment newInstance(CharSequence label) {
              MyFragment f = new MyFragment();
              Bundle b = new Bundle();
              b.putCharSequence("label", label);
              f.setArguments(b);
              return f;
          }
      

      对于活动:

      @Override protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_arguments);
      
          if (savedInstanceState == null) {
              // First-time init; create fragment to embed in activity.
              FragmentTransaction ft = getFragmentManager().beginTransaction();
              Fragment newFragment = MyFragment.newInstance("From Arguments");
              ft.add(R.id.created, newFragment);
              ft.commit();
          }
      }
      

      我相信您可以自己轻松地替换代码。您的代码显示了对 Android 的良好理解。 祝你好运,玩得开心,随时关注我们...

      【讨论】:

        【解决方案5】:

        问题是导航抽屉在 mainactivity 中有菜单,而不是在 homefragment 中。所以 onActivityResult 指向 mainactivity 而不是我的 HomeFragment。将菜单更改为 HomeFragment 后解决了我的问题。

        感谢所有帮助我的人:)

        【讨论】:

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