gpt4 book ai didi

java - 单击 RecyclerView 项时出现 IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-01 22:22:50 25 4
gpt4 key购买 nike

单击 RecyclerView 的项目时遇到问题,应用程序崩溃并给出输出:

java.lang.IndexOutOfBoundsException: Invalid index 24, size is 20
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.google.gson.JsonArray.get(JsonArray.java:147)
at com.devpocket.kvartirka.MainActivity.itemClicked(MainActivity.java:334)
at com.devpocket.kvartirka.Adapters.OffersAdapter$ViewHolder.onClick(OffersAdapter.java:161)
at android.view.View.performClick(View.java:5162)
at android.view.View$PerformClick.run(View.java:20873)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)

java.lang.IndexOutOfBoundsException: Invalid index 24, size is 20

我使用的代码是这样的,这是一个适配器:

public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.ViewHolder> {

private ClickListener clickListener;

private CityData cityData;

private ArrayList<CityData> cityItemList = new ArrayList<CityData>();
private Context mContext;
private String text;

public static Button headerButton1;

public OffersAdapter(Context context, ArrayList<CityData> cityItemList, String text) {
this.cityItemList = cityItemList;
this.mContext = context;
this.text = text;
}

@Override
public int getItemViewType(int position) {

int viewType;

if (position == 0) {
viewType = 0;
} else {
viewType = 1;
}

return viewType;
}

@Override
public OffersAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, final int viewType) {

View itemLayoutView;
ViewHolder viewHolder;

if (viewType == 0) {
itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.logo_layout, viewGroup, false);
viewHolder = new ViewHolder(itemLayoutView, viewType);
} else {
itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.offers_singleitem, viewGroup, false);
viewHolder = new ViewHolder(itemLayoutView, viewType);
}

return viewHolder;
}

@Override
public void onBindViewHolder(OffersAdapter.ViewHolder viewHolder, int position) {

if (position > 0) {
cityData = cityItemList.get(position-1);
String type = cityData.getType();

if ("flat".equals(type)) {
viewHolder.address.setText(cityData.getAddress());
viewHolder.description.setText(cityData.getDescription());
viewHolder.roomNumbers.setText(cityData.getRoomNumbers());
if (TextUtils.isEmpty(cityData.getMetro())) {
viewHolder.metroImageView.setVisibility(View.GONE);
}
viewHolder.metro.setText(cityData.getMetro());
viewHolder.prices.setText(cityData.getPrices() + " ₽");
Ion.with(viewHolder.offerImage)
.fitXY()
.load(cityData.getURL());
} else if("cottage".equals(type)) {
viewHolder.address.setText(cityData.getAddress());
viewHolder.description.setText(cityData.getDescription());
viewHolder.roomNumbers.setVisibility(View.GONE);
viewHolder.cottageImage.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(cityData.getMetro())) {
viewHolder.metroImageView.setVisibility(View.GONE);
}
viewHolder.metro.setText(cityData.getMetro());
viewHolder.prices.setText(cityData.getPrices() + " ₽");
Ion.with(viewHolder.offerImage)
.fitXY()
.load(cityData.getURL());
}
}

}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView roomNumbers; //количество комнат
public TextView address; //адресс квартиры
public TextView description; //описание квартиры
public TextView metro; //описание квартиры
public TextView prices; //цена за день
public ImageView offerImage; //картинка квартиры
public ImageView cottageImage; //обозначение коттеджа
public ImageView metroImageView; //значок метро

public ViewHolder(View itemView, int position) {
super(itemView);


if(position > 0) {
roomNumbers = (TextView) itemView.findViewById(R.id.roomNumbers);
address = (TextView) itemView.findViewById(R.id.addressTextView);
description = (TextView) itemView.findViewById(R.id.conditionsTextView);
metro = (TextView) itemView.findViewById(R.id.metroTextView);
offerImage = (ImageView) itemView.findViewById(R.id.imageView);
prices = (TextView) itemView.findViewById(R.id.priceTV);
cottageImage = (ImageView) itemView.findViewById(R.id.cottageImage);
metroImageView = (ImageView) itemView.findViewById(R.id.metroImageView);
itemView.setOnClickListener(this);
} else {
headerButton1 = (Button) itemView.findViewById(R.id.headerButton1);
headerButton1.setText(Html.fromHtml(text));
headerButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, FilterActivity.class);
mContext.startActivity(intent);
}
});
}


}

@Override
public void onClick(View v) {

if(clickListener != null) {
notifyDataSetChanged();
clickListener.itemClicked(v, getPosition());
}
}
}

@Override
public int getItemCount() {
return (null != cityItemList ? cityItemList.size() : 0);
}

public void setClickListener(ClickListener clickListener) {
this.clickListener = clickListener;
}

public interface ClickListener {
public void itemClicked(View view, int position);
}

}

这就是将数据添加到适配器中的方法:

private ArrayList<CityData> cityList = new ArrayList<CityData>();

CityData cityItem = new CityData();
cityItem.setAddress(String.valueOf(flatAddress));
cityItem.setType(type);
cityItem.setDescription(description);
cityItem.setRoomNumbers(rooms);
cityItem.setMetro(metro);
cityItem.setUrl(imageURL);
cityItem.setPrices(day);
cityList.add(cityItem);
adapter.notifyDataSetChanged();
adapter.setClickListener(MainActivity.this);

以及项目点击

@Override
public void itemClicked(View view, int position) {

String offerData = null;

for(int i = 0; i <= position; i++ ) {

if(i == position) {
JsonElement obj = jsonObjectToPass.get(position-1);
offerData = String.valueOf(obj);
}
}

Intent intent = new Intent(MainActivity.this, OfferInfo.class);
intent.putExtra("offerData", offerData);
intent.putExtra("position", position);
startActivity(intent);

}

最佳答案

将 itemClicked() 更改为此

@Override
public void itemClicked(View view, int position) {
String offerData = null;

JsonElement obj = jsonObjectToPass.get(position-1);
offerData = String.valueOf(obj);

Intent intent = new Intent(MainActivity.this, OfferInfo.class);
intent.putExtra("offerData", offerData);
intent.putExtra("position", position);
startActivity(intent);
}

直接获取位置时无需for循环

关于java - 单击 RecyclerView 项时出现 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com