如何将数据从容器活动传递到自定义适配器

时间:2014-08-04 07:45:47

标签: android listview custom-adapter android-viewholder

我遇到以下情况:

  • 我有Android Studio导航抽屉的默认实现,带有navigationDrawerFragment。
  • 我有一个带有自定义适配器的navigationDrawerFragment的自定义listView。
  • 由于我的listView中有不同类型的项目,因此我使用经典的持有者模式。

我的问题如下:我想将容器活动(facebook个人资料图片和名称)中的数据发送到自定义适配器,以便为持有者设置这些数据。

我该怎么做?请参阅以下代码:

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

我需要在else中将传递给自定义适配器的数据设置为facebookProfilPicture。

1 个答案:

答案 0 :(得分:0)

将构造函数中的数据从您的活动传递到适配器。

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