Bundle getArguments返回null

时间:2018-02-07 13:52:07

标签: java android fragment bundle

首先,其他问题没有帮助我,我无法解决我的问题。 好了,现在我有2个片段,我必须从一个片段中获取已检查的元素,将它们添加到bundle并获取其他片段。我正在进行调试,我发现在setArguments()中他保存了已检查的元素,但是当我想在其他片段中获取参数时,它返回null。怎么了?

public class FragmentLanguage extends Fragment {

public static final String TEXT = "language";
PersonalInfoFragment info = new PersonalInfoFragment();

View mainView;
ListView listView;

Button clear, next;
MyAdapter adapter = null;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.listview, container, false);
    listView = mainView.findViewById(R.id.list_view);

    select_Lang();

    checkButtonClick();

    return mainView;
}

private void select_Lang() {

    ArrayList<Item_obj> itemList = new ArrayList<>();
    Item_obj item_obj = new Item_obj("Armenian", R.drawable.download, false);
    itemList.add(item_obj);
    item_obj = new Item_obj("Russian", R.drawable.download, false);
    itemList.add(item_obj);

    //create an ArrayAdapter from the String Array
    adapter = new MyAdapter(getActivity(), R.layout.listview_items, itemList);

    // Assign adapter to ListView
    listView.setAdapter(adapter);

}

private void checkButtonClick() {
    next = mainView.findViewById(R.id.next);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ArrayList<Item_obj> arrayList = adapter.items_list;

            for (int i = 0; i < arrayList.size(); i++) {
                Item_obj item = arrayList.get(i);
                if (item.isSelected()) {
                    Bundle bundle = new Bundle();
                    bundle.putString(TEXT, item.getLanguage());
                    info.setArguments(bundle);
                    getFragmentManager().beginTransaction().replace(R.id.container,
                            new PersonalInfoFragment()).addToBackStack(null).commit();
                }
            }
        }
    });
}}

好的,这里是我想要获取已保存参数的片段。

public class PersonalInfoFragment extends Fragment {

public static final String TEXT = "language";

View mainView;

EditText skills_description;
Button  save, languages;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.personal_info, container, false);
    init(mainView);

    return mainView;
}

private void init(View v) {
    skills_description = v.findViewById(R.id.skills);
    languages = v.findViewById(R.id.select_language);
    save = v.findViewById(R.id.save);

    languages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getFragmentManager().beginTransaction().replace(R.id.container,
                    new FragmentLanguage()).addToBackStack(null).commit();
        }
    });

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = getArguments();
            if (bundle != null) {
                Toast.makeText(getActivity(), bundle.getString(TEXT), Toast.LENGTH_SHORT).show();
            }
        }
    });
}}

但他返回null。怎么解决?感谢。

这是myAdapter课程。

public class MyAdapter extends BaseAdapter {

Context ct;
LayoutInflater lInflater;
ArrayList<Item_obj> objects;
PersonalInfoFragment info = new PersonalInfoFragment();

MyAdapter(Context context, ArrayList<Item_obj> itemObj) {
    ct = context;
    objects = itemObj;
    lInflater = (LayoutInflater) ct
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return objects.size();
}

@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.listview_items, parent, false);
    }

    Item_obj io = getItem_obj(position);
    ((TextView) view.findViewById(R.id.select_lang)).setText(io.language);
    ((ImageView) view.findViewById(R.id.imageView)).setImageResource(io.image);

    CheckBox cb = view.findViewById(R.id.checkbox);
    cb.setOnCheckedChangeListener(myCheckChangeList);
    cb.setTag(position);
    cb.setChecked(io.isSelected);

    return view;
}

Item_obj getItem_obj(int position) {
    return (Item_obj) getItem(position);
}

ArrayList<Item_obj> getCheck() {
    ArrayList<Item_obj> item = new ArrayList<>();
    for (Item_obj i : objects) {
        if (i.isSelected)
            item.add(i);
    }
    return item;
}


OnCheckedChangeListener myCheckChangeList = new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        getItem_obj((Integer) buttonView.getTag()).isSelected = isChecked;
    }
};
}

3 个答案:

答案 0 :(得分:0)

您的info成员与new PersonalInfoFragment()不同。在前者上,你设置Bundle而不是后者。不幸的是,你使用后者来提交交易

答案 1 :(得分:0)

您正在全球化PersonalInfoFragment并将bundle设置为其引用,并且您正在发送一个没有任何包的新对象

getFragmentManager().beginTransaction().replace(R.id.container,
                        new PersonalInfoFragment()).addToBackStack(null).commit();

替换new PersonalInfoFragment() with info

答案 2 :(得分:0)

在replace()方法中传递信息你的问题将解决 -
但你应该在for循环之外提交你的片段

   int itemCount = 0;
      Bundle bundle = new Bundle();
      for (int i = 0; i < arrayList.size(); i++) {
         Item_obj item = arrayList.get(i);
        if (item.isSelected()) {
                itemCount++;
              bundle.putString(TEXT+"i"+, item.getLanguage());
         }
        }
       bundle.putInt("itemCount",itemCount);
        info.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(R.id.container,
       info).addToBackStack(null).commit();

现在,您可以将其他片段上的所有值设为 -

Bundle bundle = getArguments();
        int itemCount = bundle.getInt("itemCount",0);
        if(itemCount > 0){
            for (int i = 0; i < itemCount ; i++) {
              String language =  bundle.getString(TEXT+"i");
            }
        }