gpt4 book ai didi

android - 使用 Pull To Refresh 时出现 IndexOutOfBoundsException 错误

转载 作者:行者123 更新时间:2023-11-30 01:37:40 26 4
gpt4 key购买 nike

我正在使用 Pull To Refresh 方法。当我尝试刷新时,我得到了

java.lang.IndexOutOfBoundsException: Invalid index 4, size is 4

imageShop.setImageResource(imgShopsImg.get(position));//这一行。

这是我的适配器类。

public class SalesAdapter extends BaseAdapter {

Context context;
ArrayList<String> shopname;
ArrayList<String> shoparea;
ArrayList<String> lastsync;
ArrayList<String> amount;
ArrayList<String> urll;
ArrayList<String> dp;
ArrayList<String> dpunm;
ArrayList<String> dppwd;
String x = "\u20B9" + " ";

public SalesAdapter(Context context, ArrayList<String> shopName, ArrayList<String> shopArea,
ArrayList<String> lasts, ArrayList<String> totamount,
ArrayList<String> urr, ArrayList<String> dppp,
ArrayList<String> dpuuu, ArrayList<String> dpppwd) {

this.context = context;
this.shopname = shopName;
this.shoparea = shopArea;
this.lastsync = lasts;
this.amount = totamount;
this.urll = urr;
this.dp = dppp;
this.dpunm = dpuuu;
this.dppwd = dpppwd;
}

@Override
public int getCount() {
return shoparea.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
ArrayList<Integer> imgShopsImg = new ArrayList<Integer>();
imgShopsImg.add(R.color.pallete_1);
imgShopsImg.add(R.color.pallete_2);
imgShopsImg.add(R.color.pallete_3);
imgShopsImg.add(R.color.pallete_4);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.mainlistitem, null);

final TextView itemName = (TextView) convertView.findViewById(R.id.shopname);
final TextView itemId = (TextView) convertView.findViewById(R.id.amount);
final TextView itemMinPrice = (TextView) convertView.findViewById(R.id.sync);
final TextView itemLocation = (TextView) convertView.findViewById(R.id.txtShopLocation);
final TextView posurl = (TextView) convertView.findViewById(R.id.url);
final TextView posdb = (TextView) convertView.findViewById(R.id.dp);
final TextView posdpunm = (TextView) convertView.findViewById(R.id.dpunm);
final TextView posdppwd = (TextView) convertView.findViewById(R.id.dppwd);
final ImageView imageShop = (ImageView) convertView.findViewById(R.id.img_shops);
itemName.setText(shoparea.get(position));
itemLocation.setText(shopname.get(position));
itemId.setText(x + String.valueOf(amount.get(position)));


Log.e("LastSync", String.valueOf(lastsync + "Position" + position));
if (lastsync.get(position).contains("Just Now"))
itemMinPrice.setText("Last Updated: " + String.valueOf(lastsync.get(position)));
else
itemMinPrice.setText("Last Updated: " + String.valueOf(lastsync.get(position)) + "ago");
posurl.setText(String.valueOf(urll.get(position)));
posdb.setText(String.valueOf(dp.get(position)));
posdpunm.setText(String.valueOf(dpunm.get(position)));
posdppwd.setText(String.valueOf(dppwd.get(position)));
imageShop.setImageResource(imgShopsImg.get(position)); // ERROR IN THIS LINE
}
return convertView;
}
}

当我使用 Log.e 打印位置时,我得到以下位置值...

position0
position1
position2
position3
position4

但我的 ArrayList imgShopsImg 中只有 4 个值...索引为 0,1,2,3。

ArrayList<Integer> imgShopsImg = new ArrayList<Integer>();
imgShopsImg.add(R.color.pallete_1); //index 0
imgShopsImg.add(R.color.pallete_2); //index 1
imgShopsImg.add(R.color.pallete_3); //index 2
imgShopsImg.add(R.color.pallete_4); //index 3

最佳答案

这正是它所说的:您的索引超出范围:

您的 imgShopsImg 有 4 个值:

imgShopsImg[0]
imgShopsImg[1]
imgShopsImg[2]
imgShopsImg[3]

您可能正在请求 imgShopsImg[4] 或其他不存在的值,因此请检查您的位置

关于android - 使用 Pull To Refresh 时出现 IndexOutOfBoundsException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34921351/

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