Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getItemCount()错误_Java_Android_Android Recyclerview - Fatal编程技术网

Java getItemCount()错误

Java getItemCount()错误,java,android,android-recyclerview,Java,Android,Android Recyclerview,6-06 11:18:51.673 2631-2631/com.example.user.helloworld/AndroidRuntime:致命异常:main 进程:com.example.user.helloworld,PID:2631 java.lang.IndexOutOfBoundsException:索引0无效,大小为0 我得到错误行公共类CarAdapter扩展了RecyclerView.Adapter{ 和行public int getItemCount() 我想返回1个以上的(

6-06 11:18:51.673 2631-2631/com.example.user.helloworld/AndroidRuntime:致命异常:main 进程:com.example.user.helloworld,PID:2631 java.lang.IndexOutOfBoundsException:索引0无效,大小为0

我得到错误行
公共类CarAdapter扩展了RecyclerView.Adapter{

和行
public int getItemCount()

我想返回1个以上的
(dataBeen和modelsBeen)

如何修复它

谢谢你的帮助

CarAdapter.java

public class CarAdapter extends RecyclerView.Adapter<CarAdapter.MyViewHolder>{


    private List<Car.DataBean> dataBeen;
    private List<Car.DataBean.ModelsBean> modelsBeen;

    public CarAdapter(List<Car.DataBean> dataBeen, List<Car.DataBean.ModelsBean> modelsBeen) {
        this.dataBeen = dataBeen;
        this.modelsBeen = modelsBeen;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_custom,parent,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.brandname.setText(dataBeen.get(position).getBrand_name());
        holder.name.setText(modelsBeen.get(position).getName());

    }

    @Override
    public int getItemCount() {
        return dataBeen.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView brandname,name;


        public MyViewHolder(View itemView) {
            super(itemView);
            brandname = (TextView)itemView.findViewById(R.id.textBrandname);
            name = (TextView) itemView.findViewById(R.id.txtModelsName);

        }
    }
}
公共类CarAdapter扩展了RecyclerView.Adapter{
私人名单数据库;
私有列表模型;
公共CARAAdapter(列表数据项、列表模型项){
this.dataBeen=dataBeen;
this.modelsBeen=modelsBeen;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutInflater.from(parent.getContext())
.充气(R.布局.项目\自定义,父项,假);
返回新的MyViewHolder(itemView);
}
@凌驾
公共无效onBindViewHolder(MyViewHolder,int位置){
holder.brandname.setText(dataBeen.get(position.getBrand_name());
holder.name.setText(modelsBeen.get(position.getName());
}
@凌驾
public int getItemCount(){
返回dataBeen.size();
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图品牌名称,名称;
公共MyViewHolder(查看项目视图){
超级(项目视图);
brandname=(TextView)itemView.findViewById(R.id.textBrandname);
name=(TextView)itemView.findViewById(R.id.txtModelsName);
}
}
}
java

    public class Car {

        private boolean success;
        private List<DataBean> data;

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public List<DataBean> getData() {
            return data;
        }

        public void setData(List<DataBean> data) {
            this.data = data;
        }

        public static class DataBean {


            private String brand_name;
            private List<ModelsBean> models;

            public String getBrand_name() {
                return brand_name;
            }

            public void setBrand_name(String brand_name) {
                this.brand_name = brand_name;
            }

            public List<ModelsBean> getModels() {
                return models;
            }

            public void setModels(List<ModelsBean> models) {
                this.models = models;
            }

            public static class ModelsBean {


                private String name;
                private String detail;
                private int price;
                private int created_year;
                private String image;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

                public String getDetail() {
                    return detail;
                }

                public void setDetail(String detail) {
                    this.detail = detail;
                }

                public int getPrice() {
                    return price;
                }

                public void setPrice(int price) {
                    this.price = price;
                }

                public int getCreated_year() {
                    return created_year;
                }

                public void setCreated_year(int created_year) {
                    this.created_year = created_year;
                }

                public String getImage() {
                    return image;
                }

                public void setImage(String image) {
                    this.image = image;
                }
            }
        }
    }
公车{
私人布尔成功;
私人名单数据;
公共布尔值isSuccess(){
回归成功;
}
public void setSuccess(布尔值成功){
成功=成功;
}
公共列表getData(){
返回数据;
}
公共无效设置数据(列表数据){
这个数据=数据;
}
公共静态类数据库{
私有字符串品牌名称;
私有列表模型;
公共字符串getBrand_name(){
返回品牌名称;
}
公共品牌名称(字符串品牌名称){
this.brand\u name=品牌名称;
}
公共列表getModels(){
收益模型;
}
公共模型(列表模型){
这个。模型=模型;
}
公共静态类ModelsBean{
私有字符串名称;
私有字符串细节;
私人int价格;
每年创建的私人int;
私有字符串图像;
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getDetail(){
返回详细信息;
}
公共void集合详细信息(字符串详细信息){
this.detail=细节;
}
public int getPrice(){
退货价格;
}
公共无效设置价格(整数价格){
这个价格=价格;
}
公共int getCreated_year(){
每年产生的收益;
}
公共无效集合创建年份(整数创建年份){
this.created_year=created_year;
}
公共字符串getImage(){
返回图像;
}
公共void setImage(字符串图像){
这个图像=图像;
}
}
}
}
MainActivity.java

 public class MainActivity extends AppCompatActivity {

        public static final String URL = "http://xxxx/";
        private static final String TAG ="MainActivity";

        private RecyclerView recyclerView;
        private CarAdapter carAdapter;
        List<Car.DataBean> cars;
        List<Car.DataBean.ModelsBean> carss;
        private  RecyclerView.LayoutManager layoutManager;





        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            recyclerView = (RecyclerView) findViewById(R.id.Recycler_item);

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            Request request = chain.request().newBuilder()
                                    .addHeader("Accept", "Application/JSON").build();

                            return chain.proceed(request);
                        }
                    }).build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            CarInterface service = retrofit.create(CarInterface.class);
            Call<Car> call = service.listCar();
            call.enqueue(new Callback<Car>() {
                @Override
                public void onResponse(Call<Car> call, retrofit2.Response<Car> response) {
                    Log.d(TAG, "onResponse"+ response.body());

                    if (response.isSuccessful()){
                        cars = new ArrayList<Car.DataBean>();
                        carss = new ArrayList<Car.DataBean.ModelsBean>();
                        Car result = response.body();
                        cars = result.getData();
//and I want to show name in Car.DataBean.ModelsBean How to set it?????

                        carAdapter = new CarAdapter(cars,carss);

                        layoutManager = new LinearLayoutManager(getApplicationContext());
                        recyclerView.setLayoutManager(layoutManager);
                        recyclerView.setAdapter(carAdapter);
                    }
                }

                @Override
                public void onFailure(Call<Car> call, Throwable t) {

                }
            });

        }

    }
public类MainActivity扩展了AppCompatActivity{
公共静态最终字符串URL=”http://xxxx/";
私有静态最终字符串TAG=“MainActivity”;
私人回收站;
私人卡拉达普特卡拉达普特;
列出车辆清单;
列出cars;
private RecyclerView.LayoutManager LayoutManager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(recyclerView)findViewById(R.id.Recycler\U项);
OkHttpClient OkHttpClient=新建OkHttpClient.Builder()
.addInterceptor(新拦截器(){
@凌驾
公共响应拦截(链)引发IOException{
请求=chain.Request().newBuilder()
.addHeader(“接受”、“应用程序/JSON”).build();
返回链。继续(请求);
}
}).build();
改装改装=新改装.Builder()
.baseUrl(URL)
.客户(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
CarInterface服务=改装.create(CarInterface.class);
Call=service.listCar();
call.enqueue(新回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
Log.d(标记“onResponse”+response.body());
if(response.issusccessful()){
cars=新阵列列表();
carss=新ArrayL