Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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;

    }
}
like image 731
pkelly Avatar asked Jul 20 '14 20:07

pkelly


2 Answers

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.

like image 151
Michele Avatar answered Nov 03 '22 11:11

Michele


Use this

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

}

like image 22
kamal khalaf Avatar answered Nov 03 '22 09:11

kamal khalaf