捆绑对象的问题

时间:2017-08-25 15:03:34

标签: java android android-fragments

我有捆绑对象的问题。当我将数据(onCLick按钮)从活动传递到片段时,我获取了bundle对象的值,并且在我将值传递给适配器之后......之后我遇到了问题:bundle对象再次从activity获取值(但现在值为null)并传递对适配器的空值...我不知道为什么在第一次捆绑后再次从活动中获取价值...我希望你能帮助我

我向玩具展示我的代码

活动:

     Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("oki", hm);
            bundle.putIntegerArrayList("okiquantitapizze", hm_quantitàpizze);

            System.out.println("PERO:" + bundle);
/*
            MyListFragment2 myFragment = new MyListFragment2();
            myFragment.setArguments(bundle);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.pero, myFragment);
            transaction.commit();*/

            MyListFragment myFragment = new MyListFragment();
            myFragment.setArguments(bundle);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.a, myFragment);
            transaction.commit();

FRAGMENT:

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

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


        bundle = getArguments();

        System.out.println("BUNDLES1 dopo:" + bundle);
        if (bundle != null) {


            strtext2 = bundle.getIntegerArrayList("oki");
            quantitàpizze2=bundle.getIntegerArrayList("okiquantitapizze");
            System.out.println("CAZZ2:" + strtext2);

            System.out.println("PRESO2:" + quantitàpizze2);




        }



}

2 个答案:

答案 0 :(得分:0)

试试这个:
向片段添加静态Sub SelectFile() Const msoFileDialogFilePicker As Long = 3 Dim xlObj As Excel.Application Dim objDialog As Office.FileDialog Dim pstPath As String Set xlObj = Excel.Application Set objDialog = xlObj.FileDialog(msoFileDialogFilePicker) With objDialog .AllowMultiSelect = False .Title = "Select your PST File" .ButtonName = "Ok" .Show If .SelectedItems.Count = 0 Then MsgBox ("No file selected.") Else pstPath = .SelectedItems(1) MsgBox ("You have selected: " & pstPath) End If End With xlObj.Quit() Set objDialog = Nothing End Sub 方法。

Public Sub SelectFile()

    Const msoFileDialogFilePicker As Long = 3
    Dim xlObj As Excel.Application
    Dim objDialog As Office.FileDialog
    Dim pstPath As String

    xlObj = New Excel.Application
    objDialog = xlObj.FileDialog(msoFileDialogFilePicker)

    With objDialog
        .AllowMultiSelect = False
        .Title = "Select your PST File"
        .ButtonName = "Ok"
        .Show()

        If .SelectedItems.Count = 0 Then
            MsgBox("No file selected.")
        Else                
            pstPath = .SelectedItems(1)
            MsgBox("You have selected: " & .SelectedItems(1))
        End If

    End With
    xlObj.Quit()
    objDialog = Nothing
End Sub

并像这样实例化片段:

newInstance()

答案 1 :(得分:0)

我的建议不是重新创建片段,没有必要。添加标签时,请在标签中添加标签。

transaction.replace(R.id.a, myFragment, "MyListFragment");

在你的片段中创建一个这样的方法:

public void updateValues(ArrayList<Integer> list1, ArrayList<Integer> list2){
    //do your stuff and update the UI 
}

然后在onClick()中找到片段并调用方法。

public void onClick(View view) {
        MyListFragment fragment = (MyListFragment) manager.findFragmentByTag("MyListFragment");
        if (fragment != null && fragment.isVisible()) {
            fragment.updateValues(list1, list2);
        } else {
            Bundle bundle = new Bundle();
            bundle.putIntegerArrayList("oki", list1);
            bundle.putIntegerArrayList("okiquantitapizze", list2);
            fragment.setArguments(bundle);
            manager.beginTransaction()
                    .replace(R.id.a, fragment, "MyListFragment")
                    .commit();
        }
    }