6-06 11:18:51.673 2631-2631 / com.example.user.helloworld / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.user.helloworld,PID:2631 java.lang.IndexOutOfBoundsException:索引0无效,大小为0
我收到错误行public class CarAdapter extends RecyclerView.Adapter<CarAdapter.MyViewHolder>{
和第public int getItemCount()
行
我想要返回超过1 (dataBeen and modelsBeen)
如何解决?
感谢您的帮助; D
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);
}
}
}
Car.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;
}
}
}
}
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) {
}
});
}
}