如何将ArrayList从Activity发送到NavigationDrawerFragment?

时间:2014-12-07 10:51:29

标签: java android navigation-drawer

我需要将ActivityList中的ArrayList发送到NavigationDrawerFragment。我使用parcelable,但在片段中getArgument为null(

MYCLASS:

public class NavListModel implements Parcelable {
    private String Text;
    private int Type;
    private int Id;
    private String ImageURL=null;

    public NavListModel(int Type, int id, String Text,  String ImageURL)
    {
        this.Text=Text;
        this.Type=Type;
        this.Id=id;
        this.ImageURL=ImageURL;
    }

    public NavListModel(int Type, int id, String Text)
    {
        this.Text=Text;
        this.Type=Type;
        this.Id=id;
    }

    public void setText(String Text)
    {
        this.Text=Text;
    }

    public void setType(int Type)
    {
        this.Type=Type;
    }

    public void setId(int id)
    {
        this.Id=id;
    }

    public String getText()
    {
        return this.Text;
    }

    public int getType()
    {
        return this.Type;
    }

    public int getId()
    {
        return this.Id;
    }

    public String getImageURL()
    {
        return this.ImageURL;
    }

    public void setImageURL(String ImageURL)
    {
        this.ImageURL=ImageURL;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    private NavListModel(Parcel in)
    {
        Bundle bundle=in.readBundle();
        this.Id=(bundle.getInt("Id"));
        this.Type=(bundle.getInt("Type"));
        this.ImageURL=(bundle.getString("ImageURL"));
        this.Text=(bundle.getString("Text"));
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        Bundle bundle = new Bundle();
        bundle.putString("ImageURL",this.ImageURL);
        bundle.putString("Text",this.Text);
        bundle.putInt("Id", this.Id);
        bundle.putInt("Type",this.Type);
        dest.writeBundle(bundle);
    }

    public static final Parcelable.Creator<NavListModel> CREATOR = new Parcelable.Creator<NavListModel>() {

        @Override
        public NavListModel createFromParcel(Parcel source)
        {
            Bundle bundle = source.readBundle();
            int Id=(bundle.getInt("Id"));
            int Type=(bundle.getInt("Type"));
            String ImageURL=(bundle.getString("ImageURL"));
            String Text=(bundle.getString("Text"));
            if (ImageURL==null) {
                return new NavListModel(Type,Id,Text);
            }
            else
            {
                return new NavListModel(Type,Id,Text,ImageURL);
            }
        }

        @Override
        public NavListModel[] newArray(int size) {
            return new NavListModel[size];
        }
    };


}

OnCreate of Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<NavListModel> items = new ArrayList<NavListModel>();
        items.add(new NavListModel(NavListAdapter.TYPE_ADDCAR,2,"hhh"));
        items.add(new NavListModel(NavListAdapter.TYPE_CAR,3,"car","asd"));
        items.add(new NavListModel(NavListAdapter.TYPE_USER,4,"user","asd"));
        items.add(new NavListModel(NavListAdapter.TYPE_ITEM,5,"item","asd"));
        items.add(new NavListModel(NavListAdapter.TYPE_HEADER,1,"header"));
        items.add(new NavListModel(NavListAdapter.TYPE_HEADER,1,"header"));
        items.add(new NavListModel(NavListAdapter.TYPE_HEADER,1,"asd"));
        Bundle NavItems = new Bundle();
        NavItems.putParcelableArrayList("items", items);
        mNavigationDrawerFragment = new NavigationDrawerFragment();
        mNavigationDrawerFragment.setArguments(NavItems);
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

}

和OnCreateView:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        ArrayList<NavListModel> items = new ArrayList<NavListModel>();
        /**items[0]=new NavListModel(NavListAdapter.TYPE_HEADER,1,"asd");
        items[1]=new NavListModel(NavListAdapter.TYPE_ADDCAR,2,"hhh");
        items[2]=new NavListModel(NavListAdapter.TYPE_CAR,3,"car","asd");
        items[3]=new NavListModel(NavListAdapter.TYPE_USER,4,"user","asd");
        items[4]=new NavListModel(NavListAdapter.TYPE_ITEM,5,"item","asd");
        items[5]=new NavListModel(NavListAdapter.TYPE_HEADER,1,"header");
        items[6]=new NavListModel(NavListAdapter.TYPE_HEADER,1,"header");**/
        if (getArguments()!=null) {
            items = getArguments().getParcelableArrayList("items");
        }
        else
        items.add(new NavListModel(NavListAdapter.TYPE_HEADER,1,"asd"));
        items.add(new NavListModel(NavListAdapter.TYPE_ADDCAR,1,"asd"));
        mDrawerListView.setAdapter(new NavListAdapter(
                getActionBar().getThemedContext(),
                android.R.id.text1,
                items));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

getArguments在片段中为空:(什么不起作用,我该如何解决?

1 个答案:

答案 0 :(得分:2)

你的问题在这里:

mNavigationDrawerFragment = new NavigationDrawerFragment();
mNavigationDrawerFragment.setArguments(NavItems);
mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);

首先创建一个新的NavigationDrawerFragment(),然后设置参数。

然后在下一行中通过调用findFragmentById忽略所有内容并为mNavigationDrawerFragment分配一个新的片段实例(从xml中膨胀的实例)

正确的顺序是:

mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setArguments(NavItems);

现在应该可以了。