简体   繁体   中英

getItem(int) clashes with getItem(int)

I'm new to Java, so please excuse me if this is a dumb question. I got this class:

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {

Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
private FriendInfo[] friends = null;

public void setFriendList(FriendInfo[] friends)
{
    this.friends = friends;
}

public int getCount() {

    return friends.length;
}


public FriendInfo getItem(int position) {
    return friends[position];
}

public long getItemId(int position) {

    return 0;
}

public CustomDrawerAdapter(Context context, int layoutResourceID,
                           List<DrawerItem> listItems) {
    super(context, layoutResourceID, listItems);
    this.context = context;
    this.drawerItemList = listItems;
    this.layoutResID = layoutResourceID;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    DrawerItemHolder drawerHolder;
    View view = convertView;

    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_itemName);
        drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_icon);

        view.setTag(drawerHolder);

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

    }

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


    drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
            dItem.getImgResID()));
    drawerHolder.ItemName.setText(friends[position].userName);

    return view;
}

private static class DrawerItemHolder {
    TextView ItemName;
    ImageView icon;
}

At FriendInfo I get the following error:" 'getItem(int)' in '(My Package name).CustomDrawerAdapter' clashes with 'getItem(int)' in 'android.widget.ArrayAdapter'; attempting to use incompatible return type" I'm new to Java and I don't know how to fix this, could somebody help me?

Your CustomDrawerAdapter extends ArrayAdapter<T> with T being a DrawerItem. T is a type parameter which is replaced by a type argument when you define your CustomDrawerAdapter class, the type argument being DrawerItem in your case.

So CustomDrawerAdapter is basically an adapter pulling the items to show from an array of DrawerItems.

The ArrayAdapter defines a method

public T getItem (int position) 

which will be

public DrawerItem getItem (int position)

for your class CustomDrawerAdapter.

You might want to read this tutorial about generics: http://docs.oracle.com/javase/tutorial/java/generics . Note that generics are not easy to understand but you absolutely need a basic understanding when doing Android development.

If you decide to override that method then you can't change the return value unless the method has a different signature (name, plus the number and the type of its parameters).

You have to ask yourself: is CustomDrawerAdapter an adapter pulling its data from an array (or list) of FriendInfo or from an array of DrawerItem? Depending on the answer your class definition would be

public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem>

or

public class CustomDrawerAdapter extends ArrayAdapter<FriendInfo>

Using both classes to back the ArrayAdapter doesn't make sense unless FriendInfo is extending DrawerItem and that seems unlikely. If you really need a second array of Objects (FriendInfo) for whatever purposes, then don't modify the ArrayAdapter methods (like getCount(), getItemId(int) or getItem(int)) unless you know exactly what you're doing. Eg your getCount() is doomed to fail because it returns the size of FriendInfo[] instead of the number of DrawerItems and FriendInfo[] might not have the same number of elements (it certainly doesn't have before you call the setFriendList and who guarantees that the passed array has the same number of elements as the List you pass in the constructor?).

Because it extends ArrayAdapter<DrawerItem> , getItem needs to return a DrawerItem . If you can't find a clean way to fix this while extending ArrayAdapter, it's pretty easy to make your own adapter implementation by extending BaseAdapter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM