将String传递给Activity中的片段

时间:2015-10-22 20:38:38

标签: android android-fragments

我需要将一些值传递给Activity类中的片段。 我尝试将包传递给片段但是得到了null。 我需要接收值,然后在片段活动中运行AsyncTask。

代码段FirstFragment.java:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.first_frag, container, false);
        Bundle bundle = getArguments();
        if(bundle!=null)
        {
            sensorUID=getArguments().getString("senID");
            System.out.println("we got this: " + sensorUID);
        }
        System.out.println("we got this2: "+ sensorUID);
        new MyAsyncTask().execute();
        return v;
    }

代码(活动类):

package test.sensorv2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;


public class scatter extends FragmentActivity {
    protected String sensorUID;
    protected String tankname;
    protected String SCALE_M;
    protected String SCALE_C;
    protected String DPs;
    protected String UNITs;
    String np;
    int k = 0;
    float m;
    float c;
    int numVar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scatter);
        sensorUID = getIntent().getStringExtra("sUID");
        tankname = getIntent().getStringExtra("tNAME");
        SCALE_C = (getIntent().getStringExtra("tSCALEC"));
        SCALE_M = (getIntent().getStringExtra("tSCALEM"));
        DPs = (getIntent().getStringExtra("tDP"));
        UNITs = (getIntent().getStringExtra("tUNIT"));
        numVar = Integer.parseInt(DPs);

        Bundle b=new Bundle();
        b.putString("key", sensorUID);
        FirstFragment f=new FirstFragment();
        f.setArguments(b);

        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {


        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int pos) {
            switch (pos) {

                case 0:
                    return FirstFragment.newInstance("FirstFragment, Instance 1");
                case 1:
                    return SecondFragment.newInstance("SecondFragment, Instance 1");
                default:
                    return ThirdFragment.newInstance("ThirdFragment, Default");
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

    @Override
    public Fragment getItem(int pos) {
        Fragment fragment;
        Bundle args = new Bundle();
        args.putString("sendID", sensorUID);

        switch (pos) {
            case 0:
                fragment = FirstFragment.newInstance("FirstFragment, Instance 1");
                break;
            case 1:
                fragment = SecondFragment.newInstance("SecondFragment, Instance 1");
                break;
            default:
                fragment = ThirdFragment.newInstance("ThirdFragment, Default");
        }
        fragment.setArguments(args);
        return fragment;
    }

然后你可以使用`getArguments.getString(" sendID")来检索你片段中的参数;

或者您可以将您的ID作为参数传递给newInstance,例如fragment = FirstFragment.newInstance(senId);,在新实例中,您将数据从newInstance传递到onCreateview,如下所示:

 public static Fragment newInstance(String senID){
   FirstFragmentf = new FirstFragment();

   Bundle args = new Bundle();
   args.putString("senID", sendID);
   f.setArguments(args);

    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sensorID= getArguments().getString("sendID");

}

答案 1 :(得分:0)

在片段中创建一个字段:

public Bundle bundle;

并在mainactivity中将您的包分配给

f.bundle=b;

并删除

Bundle bundle=getArguments();

答案 2 :(得分:0)

在你的活动中:

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

        //Actiity to Fragment
        Bundle bundle = new Bundle();
        bundle.putString("test", "From Activity");
        // set Fragmentclass Arguments
        TestFragment testFragment = new TestFragment();
        testFragment.setArguments(bundle);
        testFragment.myTest();

    }

在你的片段中:

public void myTest(){
        //Get String from Activity
        String stringText = getArguments().getString("test");
        Log.d(LOG_TAG, "The String from the Activity = " + stringText);
    }