Android Fragments 返回空值的getArguments

vxbzzdmp  于 2023-02-19  发布在  Android
关注(0)|答案(2)|浏览(165)

我想把数据从我的Activity发送到我的Fragment。我现在要做的事情如下。

String itemDescription = workAssignmentItem.getDescription();
Bundle bundle = new Bundle();
bundle.putString("itemDescription", itemDescription);
FirstFragment.newInstance(bundle);

然后,在我的片断中,我这样做:

public static FirstFragment newInstance(Bundle bundle) {
    FirstFragment fragment = new FirstFragment();
    fragment.setArguments(bundle);
    return fragment;
}

然而,当我尝试执行“getArguments().getString(“itemDescription”);'在我的onCreate中,如下所示:

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

    description = getArguments().getString("itemDescription");

}

getArguments返回null。我不太清楚为什么它会返回null,因为互联网上的多个来源都说这是实现它的方法。
谁能给我指一下正确的方向?先谢了

t8e9dugd

t8e9dugd1#

在Fragment中传递单个字符串不需要bundle

String itemDescription = workAssignmentItem.getDescription();
FirstFragment.newInstance(itemDescription);

片段更改如下:

public static FirstFragment newInstance(String itemDescription){
    FirstFragment fragment=new FirstFragment();
    bundle args=new Bundle();
    args.putString("itemDescription",itemDescription);
    fragment.setArguments(args);
    return fragment;
}

和onCreate,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    description = getArguments().getString("itemDescription");
}
yiytaume

yiytaume2#

所以实际上我有个问题
“捆绑包空”
我通过实现onFragmentResult方法来解决这个问题。

*背景:我想将一个片段内的选定联系人传递到另一个片段内的微调器中 *

我将数据传递到Bundle的位置:

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.item_done) {
            // Create a String array to store the names of the selected contacts
            ArrayList<String> selectedContacts = new ArrayList<>();

            for (int i=0; i < listView.getCount(); i++) {
                if (listView.isItemChecked(i)) {
                    selectedContacts.add(listView.getItemAtPosition(i).toString());
                }
            }
            
            // Add the String array to the bundle
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("SELECTED_CONTACTS", selectedContacts);
            Log.d("ContactsFragment", "Setting result with selectedContacts: " + selectedContacts);
            getParentFragmentManager().setFragmentResult("SELECTED_CONTACTS", bundle);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这里是我检索数据的地方:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_sender, container, false);

    // Find the spinner views in the layout and set their adapters
    responsesSpinner = view.findViewById(R.id.responses_spinner);
    contactsSpinner = view.findViewById(R.id.contacts_spinner);

    getParentFragmentManager().setFragmentResultListener("SELECTED_CONTACTS", this, new FragmentResultListener() {
                @Override
                public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle) {

                    if (requestKey.equals("SELECTED_CONTACTS")) {
                        ArrayList<String> selectedContacts = bundle.getStringArrayList("SELECTED_CONTACTS");
                        Log.d("SenderFragment", "Received selectedContacts: " + selectedContacts);

                        if (selectedContacts != null && selectedContacts.size() > 0) {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, selectedContacts);
                            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            contactsSpinner.setAdapter(adapter);
                        }
                    }else{
                        Toast.makeText(getContext(), "not working", Toast.LENGTH_SHORT).show();
                    }
                }
            });

    return view;
}

相关问题