我遇到以下情况:
我的问题如下:我想将容器活动(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。
答案 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;
}
}