java.lang.IllegalArgumentException: 不能有一个 viewTypeCount < 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24854861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:13:34  来源:igfitidea点击:

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

javaandroid

提问by pkelly

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.

我的应用程序从数据库加载用户朋友。当用户至少有 1 个朋友要放入列表视图时,就可以了。当用户是全新的并且还没有朋友时,应用程序崩溃,因为列表视图的计数为 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;

    }
}

采纳答案by Michele

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.

我认为您错过了 ViewTypeCount 的重点。您应该在列表中返回不同视图类型的数量。这对于回收列表中的视图很重要。

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.

想象一下你有 2 种类型的 Listitems,一种是白色背景,一种是黑色背景。当您返回 2 作为 ViewTypeCount 时,Listview 知道可以,有 2 种类型的 Listitems 并且不会在 getView 视图回收中混合它们。

so just use:

所以只需使用:

   public int getViewTypeCount() {                 
        return 1;
    }

or dont override that method at all.

或者根本不覆盖该方法。

回答by Gabe Sechan

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.

getViewTypeCount 返回此适配器可以返回的不同类型视图的数量。由于您只返回一种类型的视图,因此应该只返回 1。

回答by kamal khalaf

Use this

用这个

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

}

}