0

I am in the following situation:

  • I have the default implementation of the navigation drawer from Android Studio with a navigationDrawerFragment.
  • I have a custom listView for this navigationDrawerFragment with a custom adapter.
  • Since I have different type of item in my listView, i use the classic holder pattern.

My problem is the following: I'd like to send data from my container activity (facebook profile picture and name) to the custom adapter to set the holder with these data.

how can I do that ? See the following code:

if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        drawerHolder = new DrawerItemHolder();

        view = inflater.inflate(layoutResID, parent, false);
        drawerHolder.ItemName = (TextView) view
                .findViewById(R.id.drawer_profileName);
        drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);

        drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);

        drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);

        drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);

        drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onLogoutButtonClicked();
            }
        });

        drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);

        drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);

        drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);

        view.setTag(drawerHolder);

    } else {
        drawerHolder = (DrawerItemHolder) view.getTag();

    }

    DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);

    if (dItem.isTitle()){
        drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
        drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.title.setText(dItem.getTitle());
    } else if (dItem.getImgResID() == 0) {
        drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
        drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.item.setText(dItem.getItem());
    } else {
        drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
        drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
    }

    return view;
}

I need in the else to set the data I passed to the custom adapter to the facebookProfilPicture.

ArnaudToutain
  • 178
  • 2
  • 3
  • 12
  • send your data to adapter's constructor – Shayan Pourvatan Aug 04 '14 at 07:46
  • I'm stupid, you're right. But how can I pass data to the drawer fragment ? mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); – ArnaudToutain Aug 04 '14 at 07:56
  • In the same place where you inflate the layout for the navigation drawer you can add the values. So if you inflate the menu in your main activity you can also do a findById on the navigation drawer layout and set the fields. – userM1433372 Aug 04 '14 at 07:59

1 Answers1

0

Pass the data in the constructor form your activity to the adapter.

public class YourAdapter {

    private final String facebookProfilePictureUrl;
    private final String facebookProfileName;

    public YourAdapter(String facebookProfilePictureUrl, String facebookProfileName) {
        this.facebookProfilePictureUrl = facebookProfilePictureUrl;
        this.facebookProfileName = facebookProfileName;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (view == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            drawerHolder = new DrawerItemHolder();

            view = inflater.inflate(layoutResID, parent, false);
            drawerHolder.ItemName = (TextView) view
                    .findViewById(R.id.drawer_profileName);
            drawerHolder.icon = (ProfilePictureView) view.findViewById(R.id.drawer_profileIcon);
            drawerHolder.title = (TextView) view.findViewById(R.id.drawer_titleName);
            drawerHolder.item = (TextView) view.findViewById(R.id.drawer_itemName);
            drawerHolder.logOut = (TextView) view.findViewById(R.id.drawer_logOut);
            drawerHolder.logOut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onLogoutButtonClicked();
                }
            });

            drawerHolder.titleLayout = (LinearLayout) view.findViewById(R.id.titleLayout);
            drawerHolder.itemLayout = (LinearLayout) view.findViewById(R.id.itemLayout);
                drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
            view.setTag(drawerHolder);
        } else {
            drawerHolder = (DrawerItemHolder) view.getTag();
        }

        DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);

        if (dItem.isTitle()){
            drawerHolder.titleLayout.setVisibility(LinearLayout.VISIBLE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.title.setText(dItem.getTitle());
        } else if (dItem.getImgResID() == 0) {
            drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.VISIBLE);
            drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.item.setText(dItem.getItem());
        } else {
            drawerHolder.titleLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.itemLayout.setVisibility(LinearLayout.INVISIBLE);
            drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
        }

        drawerHolder.title.setText(facebookProfileName);
        // Load the profile image here.
        return view;
    }
}
userM1433372
  • 5,345
  • 35
  • 38