将数据从活动传递到片段而没有替换片段的问题

时间:2017-08-28 13:47:44

标签: java android android-fragments

我的代码如下,但数据未从activity传递到fragment。我不知道为什么!

活动:

  public  class ScrollableTabsActivity2 extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private Button logout_user;
    //private SQLiteHandler db;
    private SessionManager session;
    private TextView txtName;
    private TextView txtEmail;
    int s;
    String intentt="";
    File file;
    Uri filePath;
    ListView lv;
    ArrayList<Integer> hm=new ArrayList<>();
    String path=null;
    ArrayList<Planet> planetList=new ArrayList();

    private String myString = "azz";
     ArrayList<Integer> arraylistInteger;
    String value="";
    String numero_tavolo="";
    ArrayList<Integer> hm3=new ArrayList<>();
    ArrayList<Integer> hm_quantitàpizze=new ArrayList<>();
    TextView numero_tavolo_text_view;


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


        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        txtName = (TextView) findViewById(R.id.name);
        txtEmail = (TextView) findViewById(R.id.numero_telefonico);
        numero_tavolo_text_view=(TextView)findViewById(R.id.numero_tavolo);
        logout_user=(Button) findViewById(R.id.crisbio);

        logout_user.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                 logoutUser();
            }
        });



        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpagerr);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabss);
        tabLayout.setupWithViewPager(viewPager);


        Bundle extras = getIntent().getExtras();
        if (extras != null) {
             value = extras.getString("PATH_FILE_DA_AGGIORNARE");
            numero_tavolo=extras.getString("NUMERO_TAVOLO");
            file = new File(value);
            System.out.println("FILE:" + file);

            FileInputStream is = null;
            try {
                is = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = null;
            try {
                dBuilder = dbFactory.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }
            Document doc = null;
            try {
                doc = dBuilder.parse(is);
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            Element element = doc.getDocumentElement();
            element.normalize();

            int a;
            int b;
            NodeList nList = doc.getElementsByTagName("checkboxes_pizza");
            hm.clear();
            hm_quantitàpizze.clear();

            for (int i = 0; i < nList.getLength(); i++) {
                Node node = nList.item(i);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element element2 = (Element) node;
                    //tv1.setText(tv1.getText()+"\nName : " + getValue("name", element2)+"\n");

                    String id = getValue("id", element2);
                    a = Integer.parseInt(id);
                    String idd = getValue("quantita", element2);
                    b = Integer.parseInt(idd);


                    System.out.println("XMLLLLLLLLLL:" + b);

                    hm.add(a);
                    hm_quantitàpizze.add(b);

                    System.out.println("AZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ:" + hm_quantitàpizze);
                    System.out.println("LAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:" + hm);






                }

            }
           Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("oki", hm);
            bundle.putIntegerArrayList("okiquantitapizze", hm_quantitàpizze);
            System.out.println("OGGETTO BUNDLE:" + bundle);
            MyListFragment2 myFragment = new MyListFragment2();
            myFragment.setArguments(bundle);



          /*  MyListFragment2 myFragment = new MyListFragment2();
            myFragment.setArguments(bundle);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            transaction.replace(R.id.acab, myFragment);

            transaction.commit();
*/

            // MyListFragment2.newInstance(hm,hm_quantitàpizze);
         /*   FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            //transaction.replace(R.id.a, myFragment);
            //transaction.replace(R.id.porcozio, myFragment);
            transaction.replace(android.R.id.content, myFragment);


            transaction.commit();
*/

        }

       numero_tavolo_text_view.setText(numero_tavolo);
    }

片段:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        System.out.println("BUNDLES prima:" + bundle);

        bundle=getArguments();

        System.out.println("BUNDLES dopo:" + bundle);



        if (bundle != null) {
            strtext2 = bundle.getIntegerArrayList("oki");
            System.out.println("BUNDLES1 dentro:" + strtext2);

            quantitàpizze2 = bundle.getIntegerArrayList("okiquantitapizze");
            System.out.println("BUNDLES2 dentro:" + quantitàpizze2);


        }

}




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the custom_spinner_items for this fragment
        //super.onDestroy();


        SharedPreferences settings = getContext().getSharedPreferences("states", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();


        ViewGroup rootView2 = (ViewGroup) inflater.inflate(R.layout.fragment_list_2_2, container, false);






        return rootView2;


    }

activity.xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:orientation="horizontal"
                android:id="@+id/minchia">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/welcome"
                    android:textSize="20dp"
                    android:layout_marginLeft="5dp"
                    />
                <Button
                    android:id="@+id/crisbio"
                    android:layout_width="122dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="40dip"
                    android:background="@color/btn_logut_bg"
                    android:text="stampa"
                    android:textAllCaps="false"
                    android:textColor="@color/white"
                    android:textSize="15dp"
                    android:layout_marginLeft="5dp"
                    android:onClick="logoutUser"

                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="20dp"
                    android:layout_marginLeft="5dp"
                    android:id="@+id/numero_tavolo"
                    />

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:textColor="@color/lbl_name"
                    android:textSize="24dp"
                    android:layout_marginLeft="5dp"
                    />
            </LinearLayout>



        </android.support.v7.widget.Toolbar>


        <android.support.design.widget.TabLayout
            android:id="@+id/tabss"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            android:background="@color/colorPrimaryDark" />

    </android.support.design.widget.AppBarLayout>



    <android.support.v4.view.ViewPager
        android:id="@+id/viewpagerr"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >
        <FrameLayout
            android:id="@+id/cristo"
            android:layout_marginTop="110dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        </FrameLayout>

    </android.support.v4.view.ViewPager>

   <!-- <FrameLayout
        android:id="@+id/cristo"
        android:layout_marginTop="110dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    </FrameLayout>
-->
</android.support.design.widget.CoordinatorLayout>

错误是:

  

BUNDLES dopo:当我尝试传递数据时为空

非常感谢您在这件事上的时间和帮助。

PS:对于@VivekMishra,我得到了你的建议:

enter image description here

0 个答案:

没有答案