【问题标题】:How to get Stings from Fragment to Activity如何从 Fragment 获取 Stings 到 Activity
【发布时间】:2017-06-06 15:08:48
【问题描述】:

我有一个与 Stings 配合使用的应用程序,但我不知道如何将字符串从片段发送到活动。有人帮助我,但我不太了解代码。所以如果有人有问题,他可以问我,我可以回答它

请帮帮我

片段:

public class Fragment_1 extends Fragment {



    String text;
    int zähler = 0;
    String teile[];
    String in = "", in2 = "", in3 = "";
    ListView listView;
    public String [] liste;
    String value = "MIT";



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

        Bundle bundle = new Bundle();
        bundle.putString("Key",value);

        Intent intent = getActivity().getIntent();
        intent.putExtras(bundle);

        View view = inflater.inflate(R.layout.fragment_1, container, false);

        listView = (ListView) view.findViewById(R.id.listView);
       // final Button but = (Button) view.findViewById(R.id.but1) ;





        new doit().execute();



        return view;
    }

主活动:

public class MainActivity extends AppCompatActivity {
    String mesg = "MIT";





    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);


        Bundle data = getIntent().getExtras();
        if (data != null) {

            String receivedString = data.getString("Key");

            Log.d("MainActivity", receivedString);
        }





        Log.d("MainActivity", "Bitte nicht" );



    }

【问题讨论】:

    标签: android android-activity fragment


    【解决方案1】:

    你必须在你的片段中使用一个接口:

    public class MyFragment extends Fragment
    {
    
    private MyInterface mListener;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.mylayout, container, false);
    }
    
    @Override
    public void onActivityCreated(Bundle bundle)
    {
        super.onActivityCreated(bundle);
        mListener.passString("Hello from fragment!");
    }
    
    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        if (context instanceof MyFragment.MyInterface)
        {
            mListener = (MyInterface) context;
        }
        else
        {
            throw new RuntimeException(context.toString());
        }
    }
    
    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }
    
    public interface MyInterface
    {
        void passString(String string);
    }
    
    }
    

    在你的活动中你必须实现接口的方法:

    public class MyActivity extends AppCompatActivity implements MyFragment.MyInterface
    {
    String myString;
    //other methods like onCreate and so on...
    
    @Override
    public void passString(String string)
    {
        myString = string;
    }
    
    }
    

    myString 将是您从片段传递给活动的字符串。

    【讨论】:

      【解决方案2】:

      你在这里,只需使用一个包将数据从你的片段传输回你的活动:

      //sending data from your fragment
      Bundle bundle = new Bundle();
      bundle.putString("KEY",value);
      
      Intent intent = getActivity().getIntent();
      intent.putExtras(bundle);
      
      
      //Receving data inside your Activity
      Bundle data = getIntent().getExtras();
      if (data != null)
              {
                  String receivedString= data.getString("KEY");
      
              }
      

      【讨论】:

      • 数据对我来说始终为空
      • 你是如何从片段返回到活动的??
      • 如你所写。片段中的第一部分和活动中的第二部分
      • 不,不,我要问的是,当您在片段中时,您如何返回访问数据的活动。
      • 抱歉,我解决了。有用。但是,将intent.putExtra(bundle); 更改为intent.putExtras(bundle);,因为 putExtras 方法用于捆绑,putExtra 用于字符串等等。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 1970-01-01
      相关资源
      最近更新 更多