gpt4 book ai didi

java - 如何将 ArrayList 从 Activity 发送到 NavigationDrawerFragment?

转载 作者:行者123 更新时间:2023-12-02 05:11:04 25 4
gpt4 key购买 nike

我需要将 ArrayList 从 Activity 发送到 NavigationDrawerFragment。我使用 Parcelable,但在 fragment 中 getArgument 为 null(

我的类(class):

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];
}
};


}

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;
}

fragment 中的 getArguments 为空:(什么不起作用以及如何修复它?

最佳答案

您的问题在这里:

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

首先创建一个新的 NavigationDrawerFragment() 并为其设置参数。

然后在下一行中,您通过调用 findFragmentById 忽略所有内容,并为 mNavigationDrawerFragment 分配一个新的 fragment 实例(从 xml 扩充的实例)

正确的顺序是这样的:

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

现在应该可以工作了。

关于java - 如何将 ArrayList 从 Activity 发送到 NavigationDrawerFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com