【问题标题】:sending data from activity to fragment将数据从活动发送到片段
【发布时间】:2015-02-13 17:02:24
【问题描述】:

当按下侧面菜单时,我有一个侧面菜单。它应该把SharedPreferencesString 发送到Fragment。并且Fragment 应该设置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);
} 

【问题讨论】:

  • 你忘了问问题。

标签: android android-activity android-fragments


【解决方案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. 在FT.commit(); 之后调用setTextV :

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

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

【讨论】:

  • 我尝试用v.findViewById(R.id.frag_one_textview) 更新Fragment 并在FT.commit(); 之后初始化setTextV 仍然显示错误。
【解决方案2】:

在这种特殊情况下,您不需要向片段发送数据,因为在片段中您可以访问 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;
    }

【讨论】:

  • 什么错误?如果需要帮助,请附上堆栈跟踪或其他内容
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-27
  • 2018-11-30
  • 2012-09-26
相关资源
最近更新 更多