简体   繁体   中英

java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1

I'm getting this error:

java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1

I'm pretty sure I know exactly what's causing it, but I don't know how to fix it.

My app loads a users friends from the database. When the user has at least 1 friend to put in the list view, it's fine. When the user is brand new and has no friends yet, the app crashes because the listview has a count of 0.

Is this simply a case of error handling?

If I don't post all the necessary relevant code please let me know!

Here is my adapter:

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {

    Context context;
    int resourceId;
    LayoutInflater inflater;
    private Context mContext;

    @Override

    public int getViewTypeCount() {                 

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }


    ArrayList<HashMap<String, String>>  items;
    public MyAdapter (Context context, int resourceId, ArrayList<HashMap<String, String>> items)
    {
        super(context, resourceId, items);
        mContext = context;
        this.items =items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if (convertView == null){

            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.fbphoto = (ImageView)convertView.findViewById(R.id.fbphoto);
            holder.name = (TextView)convertView.findViewById(R.id.name);

   convertView.setTag(holder);

        } else {

            holder = (ViewHolder)convertView.getTag();
        }



        final HashMap<String,String> item = (HashMap<String,String> ) items.get(position);
        if (item != null)
        {

            String facebookProfilePicUrl = "https://graph.facebook.com/"+item.get(TAG_FACEBOOKID)+"/picture?width=150&height=150";

            Picasso.with(mContext)
            .load(facebookProfilePicUrl)
            .placeholder(R.drawable.no_image)
            .into(holder.fbphoto);

holder.name.setText(item.get(TAG_USERNAME));

 }

        return convertView;
    }

    public class ViewHolder
    {
        ImageView fbphoto;
        TextView    name;

    }
}

I think you miss the point of ViewTypeCount. You should return the number of Different View Types in your list. This is important for recycling of the Views inside the List.

Imaging you have 2 Types of Listitems, one with a white Background and one with black Background. When you return 2 as ViewTypeCount the Listview knows ok, there a 2 types of Listitems and will not mix them up in the getView view recycling.

so just use:

   public int getViewTypeCount() {                 
        return 1;
    }

or dont override that method at all.

Use this

@Override
public int getViewTypeCount() {
    if(getCount() > 0){
        return getCount();
    }else{
        return super.getViewTypeCount();
    }

}

getViewTypeCount returns the number of different types of views this adaptor can return. Since you're only returning one type of view, this should just return 1.

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