0
我需要從Activity發送ArrayList到NavigationDrawerFragment。我用parcelable,但在片段getArgument爲空(如何從活動發送ArrayList到NavigationDrawerFragment?
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中活動的:
@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;
}
getArgumen ts是零片段:(什麼'不工作,我該如何解決它?