使用Pull To Refresh时出现IndexOutOfBoundsException错误

时间:2016-01-21 10:36:53

标签: android

我正在使用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打印位置时,我得到以下的POSITION值...

position0
position1
position2
position3
position4

但我的ArrayList中只有4个值imgShopsImg ...索引为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

1 个答案:

答案 0 :(得分:1)

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

您的imgShopsImg有4个值:

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

您可能要求imgShopsImg[4]或其他一些不存在的值,请检查position