2015-05-10 104 views
0

默认情况下,我在创建家庭片段启动时有一个导航抽屉。现在从我的主要活动中,我进入下一个活动,我执行一些操作并使用onActivityResult返回mainactivity。在片段中获取活动结果

高兴在这里一切工作正常。这是我的问题。

我在我的HomeFragment中有一个listview,所以在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,其中I需要更新这些值:

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; 
    } 



} 

我没有收到任何错误,并且没有显示listview。请告诉我。

+0

@ downvoter护理的人发表意见 – coder

+0

我已经发布了2个回答。我认为我使用newInstance()的最新版本足以满足您的需求。 –

回答

0

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

感谢所有谁帮我出:)

0

从我可以看到,你从来没有设置你的适配器到你的listView。

所以,添加下面创建一个新的AdapterListViewData和做.notifyDataSetChanged()

listViewData.setAdapter(adapterListViewData); 
+0

问题是没有设置适配器,但无法获取添加到列表视图的详细信息。 – coder

0

有实现这一目标的不同方式之间。

我主要使用事件总线来进行碎片和活动之间的通信。 您可以使用这个强烈推荐的Square队的Otto公交车。

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

您可以SubscribeFragment的事件。当您在ActivityProduce中得到结果并发送到您的Fragment时。

0

似乎有一个时间onCreateView()之间的HomeFragmentonActivityResult()在MainActivity问题。 Google建议使用Bundle作为数据参数,接口用于Activity和Fragment(s)之间的通信。设置起来并不困难,并且在您的应用程序中用于其他目的是很好的知识。

无论如何,我已经发布了一个答案,详细解释@Passing data between fragments contained in an activity(搜索我的用户名,如果没有立即发现)。 我喜欢展示一个片段,将数据发送到一个片段,从谷歌网页@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); 
0

我只是想到了另外一个,可能是更好的,更方便的答案。由于您取决于onCreateView()HomeFragment,您可以通过newInstance()传递数据。 Google网页@Fragment。搜索“静态MyFragment newInstance”文本就是一个很好的例子。对于活动,搜索“Fragment newFragment = MyFragment.newInstance(”From Arguments“)”的文本。代码片段在该网页为您HomeFragment:

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的很好的理解。 祝你好运,玩得开心,保持我们的发布...

相关问题