我正在使用Pull To Refresh方法,但尝试刷新时却遇到了

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



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

这是我的AdapterClass。

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]或其他不存在的值,因此请检查您的position

07-28 12:45