如何将Stings从片段变为活动

时间:2017-06-06 15:08:49

标签: android android-activity fragment

我有一个适用于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;
    }

MainActivity:

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" );



    }

2 个答案:

答案 0 :(得分:0)

您必须在片段中使用接口:

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将是您从片段传递给活动的字符串。

答案 1 :(得分:0)

您只需使用捆绑包将数据从您的片段传输回您的活动:

//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");

        }