将数据从活动发送到片段

时间:2015-02-13 17:02:24

标签: android android-activity android-fragments

按下侧面菜单时,我有一个侧边菜单。它应该采用SharedPreferences String并将其发送到FragmentFragment应设置TextView,它将显示在MainActivity上。

发送到片段的数据未显示在MainActivity中,我收到错误。任何解决方案?

主要活动代码(按下选项时): -

if(num == 0)
    {
        SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
        username = sp.getString("username", "DEFAULT");
        FragmentOne F1 = new FragmentOne();

        FragmentManager FM = getFragmentManager();
        FragmentTransaction FT = FM.beginTransaction();
        FT.add(R.id.relative_main, F1);
        F1.setTextV(username);
        FT.commit();

    }

片段

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

    textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
    View v = inflater.inflate(R.layout.fragment_one, container, false);

    return v;
}

public void setTextV(String username)
{
    textV.setText(username);
} 

2 个答案:

答案 0 :(得分:1)

以下问题在当前实施中:

1。如果fragment_one处于从onCreateView返回的布局中,请使用v来调用findViewById:

View v = inflater.inflate(R.layout.fragment_one, container, false);
textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
return v;

2。setTextV之后致电FT.commit();

    FragmentTransaction FT = FM.beginTransaction();
    FT.add(R.id.relative_main, F1);
    FT.commit();
    F1.setTextV(username);

因为您从SharedPreferences获取值以在TextView中显示,所以从onCreateView中的首选项中获取值并在TextView中显示。

答案 1 :(得分:0)

在这种特殊情况下,您不需要将数据发送到片段,因为在片段中您可以访问SharedPreferences。

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

        textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
        View v = inflater.inflate(R.layout.fragment_one, container, false);

        SharedPreferences sp = getActivity().getSharedPreferences("Login", Context.MODE_PRIVATE);
        username = sp.getString("username", "DEFAULT");
        textV.setText(username);

        return v;
    }

对于其他情况,从Activity向Fragment发送信息的最佳方法是使用Bundle。

if(num == 0)
    {
        SharedPreferences sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
        username = sp.getString("username", "DEFAULT");
        FragmentOne F1 = new FragmentOne();

        Bundle bundle = new Bundle();
        bundle.putString("username", username);
        // You can add all the information that you need in the same bundle

        F1.setArguments(bundle);

        FragmentManager FM = getFragmentManager();
        FragmentTransaction FT = FM.beginTransaction();
        FT.add(R.id.relative_main, F1);
        FT.commit();

    }

片段: -

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

        textV = (TextView) getActivity().findViewById(R.id.frag_one_textview);
        View v = inflater.inflate(R.layout.fragment_one, container, false);

        textV.setText(getArguments().getString("username", ""));

        return v;
    }
相关问题