我试图在我的适配器中使用两种类型的视图和两种类型的ViewHolders,但是我看到以下错误:
java.lang.ClassCastException: com.Sleep.Adapters.FriendsListAdapterFromKesh$ViewHolder2 cannot be cast to com.Sleep.Adapters.FriendsListAdapterFromKesh$ViewHolder1
at com.Sleep.Adapters.FriendsListAdapterFromKesh.getView(FriendsListAdapterFromKesh.java:226)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2186)
at android.widget.ListView.makeAndAddView(ListView.java:1845)
at android.widget.ListView.fillUp(ListView.java:712)
at android.widget.ListView.fillGap(ListView.java:651)
以下是完整的适配器类:
public class FriendsListAdapterFromKesh extends ArrayAdapter<FriendListEntryItem> {
List<FriendListEntryItem> friends;
List<FriendListEntryItem> friendsWithoutPoints;
Context context;
private LayoutInflater inflater;
private LayoutInflater mLayoutInflater;
private int type;
private static String ROOT_DIRECTORY_PATH = Environment.getExternalStorageDirectory() + File.separator + ".Sleep/Photos old";
FriendsTab friendsTab;
public FriendsListAdapterFromKesh(Context context, final List<FriendListEntryItem> friends, final List<FriendListEntryItem> friendsWithoutPoints) {
super(context, 0);
this.context = context;
inflater = LayoutInflater.from(context);
this.friendsWithoutPoints = friendsWithoutPoints;
this.friends = friends;
friendsTab = new FriendsTab();
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCountFriendsWithPoints() {
return friends.size();
}
public List<FriendListEntryItem> getFriends() {
return friends;
}
public List<FriendListEntryItem> getFriendsWithoutPoints() {
return friendsWithoutPoints;
}
@Override
public int getCount() {
return friends.size() + friendsWithoutPoints.size();
}
FriendListEntryItem ei = friends.get(position - step);
return ei.userId + "," + ei.socType + "," + ei.name;
}
static class ViewHolder1 {
public ImageView image;
public TextView title;
public TextView subtitle;
}
static class ViewHolder2 {
public ImageView image;
public TextView title;
public TypefacedButton button;
}
@Override
public int getItemViewType(int pos) {
if (pos < friends.size())
return 1;
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
if (convertView == null) {
if (viewType == 1) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_frnds_item_with_points, parent, false);
ViewHolder1 viewHolder1 = new ViewHolder1();
viewHolder1.title = (TextView) convertView.findViewById(R.id.list_item_title_friend);
viewHolder1.image = (ImageView) convertView.findViewById(R.id.imageFriendAva);
viewHolder1.subtitle = (TextView) convertView.findViewById(R.id.list_item_friend_woke_time);
convertView.setTag(viewHolder1);
} else {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_frnds_item_without_points, parent, false);
ViewHolder2 viewHolder2 = new ViewHolder2();
viewHolder2.title = (TextView) convertView.findViewById(R.id.list_item_title_friend);
viewHolder2.image = (ImageView) convertView.findViewById(R.id.imageFriendAva);
viewHolder2.subtitle = (TextView) convertView.findViewById(R.id.list_item_friend_woke_time);
convertView.setTag(viewHolder2);
}
} else {
if (convertView.getTag().getClass().equals(ViewHolder1.class)) {
ViewHolder1 holder1 = (ViewHolder1) convertView.getTag();
final FriendListEntryItem ei = friends.get(position);
if (ei != null) {
if (holder1.title != null) {
holder1.title.setText(ei.name);
}
if (holder1.button != null) {
holder1.button.setFriendItem(ei);
}
if (holder1.image != null) {
File picture = findPicture(ei.name);
if (picture != null && picture.exists()) {
Picasso.with(context).load("file://" + picture.getAbsolutePath()).fit().centerCrop().into(holder1.image);
}
}
}
} if (convertView.getTag().getClass().equals(ViewHolder2.class)) {
ViewHolder2 holder2 = (ViewHolder2) convertView.getTag();
final FriendListEntryItem ei = friendsWithoutPoints.get(position - friends.size());
if (ei != null) {
if (holder2.title != null) {
holder2.title.setText(ei.name);
}
if (holder2.button != null) {
holder2.button.setFriendItem(ei);
}
if (holder2.image != null) {
File picture = findPicture(ei.name);
if (picture != null && picture.exists()) {
Picasso.with(context).load("file://" + picture.getAbsolutePath()).fit().centerCrop().into(holder2.image);
}
}
}
}
}
return convertView;
}
答案 0 :(得分:1)
您没有覆盖getViewTypeCount
。你需要添加
@Override
public int getViewTypeCount() {
return 2;
}
FriendsListAdapterFromKesh
类中的,以便您的自定义适配器知道预期有多少种不同的视图。由于您没有覆盖getViewTypeCount
,您的适配器甚至可能无法检查getItemViewType
返回的值,它只是使所有视图都相同(第一个)类型。