Answer the question
In order to leave comments, you need to log in
How to pass Arraylist from parent Activity to NavigationDrawerFragment ?
Good afternoon!
I can’t show the logs, AStudio does not throw it out.
I tried a bunch of options, ranging from accessing static variables to this:
main.java is the root activity for the side menu.
public class main extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@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));
}
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];
}
};
}
@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=getArguments().getParcelableArrayList("items");
mDrawerListView.setAdapter(new NavListAdapter(
getActionBar().getThemedContext(),
android.R.id.text1,
items));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderModel viewHolder = null;
NavListModel listViewItem = objects.get(position);
int listViewItemType = getItemViewType(position);
TextView textView = null;
ImageView imageView = null;
Button button = null;
if (convertView == null)
{
if (listViewItemType == TYPE_ADDCAR)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_addcar, null);
textView = (TextView) convertView.findViewById(R.id.addcar_title);
viewHolder = new ViewHolderModel(textView);
convertView.setTag(viewHolder);
viewHolder.getText().setText(listViewItem.getText());
}
else if (listViewItemType == TYPE_ITEM)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_item, null);
textView = (TextView) convertView.findViewById(R.id.item_title);
imageView = (ImageView) convertView.findViewById(R.id.item_icon);
viewHolder = new ViewHolderModel(textView,imageView);
viewHolder.getText().setText(listViewItem.getText());
viewHolder.getImage().setImageResource(R.drawable.ic_launcher);
convertView.setTag(viewHolder);
}
else if (listViewItemType == TYPE_CAR)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_car, null);
textView = (TextView) convertView.findViewById(R.id.car_title);
imageView = (ImageView) convertView.findViewById(R.id.car_icon);
button = (DontClickButton) convertView.findViewById(R.id.car_button);
viewHolder = new ViewHolderModel(textView,imageView,button);
viewHolder.getText().setText(listViewItem.getText());
viewHolder.getImage().setImageResource(R.drawable.ic_launcher);
viewHolder.getButton().setText("as");
convertView.setTag(viewHolder);
}
else if (listViewItemType == TYPE_USER)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_user, null);
textView = (TextView) convertView.findViewById(R.id.user_title);
imageView = (ImageView) convertView.findViewById(R.id.user_icon);
viewHolder = new ViewHolderModel(textView,imageView);
viewHolder.getText().setText(listViewItem.getText());
viewHolder.getImage().setImageResource(R.drawable.ic_launcher);
convertView.setTag(viewHolder);
}
else
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_header, null);
textView = (TextView) convertView.findViewById(R.id.header_title);
viewHolder = new ViewHolderModel(textView);
viewHolder.getText().setText(listViewItem.getText());
convertView.setTag(viewHolder);
}
}
else
{
viewHolder = (ViewHolderModel) convertView.getTag();
}
return convertView;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question