我需要将ArrayList从Activity发送到NavigationDrawerFragment。我使用parcelable,但片段中的getArgument为null(
我的课:
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;
}
getArguments在片段中为null :(什么是行不通的,我该如何解决?
最佳答案
您的问题在这里:
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);
现在应该可以了。
关于java - 如何将ArrayList从Activity发送到NavigationDrawerFragment?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27341865/