尝试从Android中的arrayList中删除项时IndexOutOfBounds异常

时间:2014-11-20 11:39:29

标签: java android arraylist indexoutofboundsexception

我有一个Android活动,我将listItem的位置添加到arrayList,并在需要时删除(选择/删除)。所以,当我要删除最后一个索引时,它说:

logcat的

11-20 06:31:29.819: W/System.err(19963): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
11-20 06:31:29.839: W/System.err(19963):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
11-20 06:31:29.839: W/System.err(19963):    at java.util.ArrayList.remove(ArrayList.java:403)
11-20 06:31:29.839: W/System.err(19963):    at com.amar.travelonwards.HotelListActivity$RssAdapter$1.onClick(HotelListActivity.java:949)
11-20 06:31:29.839: W/System.err(19963):    at android.view.View.performClick(View.java:4424)
11-20 06:31:29.839: W/System.err(19963):    at android.view.View$PerformClick.run(View.java:18383)
11-20 06:31:29.849: W/System.err(19963):    at android.os.Handler.handleCallback(Handler.java:733)
11-20 06:31:29.849: W/System.err(19963):    at android.os.Handler.dispatchMessage(Handler.java:95)
11-20 06:31:29.849: W/System.err(19963):    at android.os.Looper.loop(Looper.java:137)
11-20 06:31:29.849: W/System.err(19963):    at android.app.ActivityThread.main(ActivityThread.java:4998)
11-20 06:31:29.849: W/System.err(19963):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 06:31:29.849: W/System.err(19963):    at java.lang.reflect.Method.invoke(Method.java:515)
11-20 06:31:29.849: W/System.err(19963):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-20 06:31:29.849: W/System.err(19963):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-20 06:31:29.849: W/System.err(19963):    at dalvik.system.NativeStart.main(Native Method)

。我试过没有运气我的代码如下,请帮我把它缩短。谢谢提前..!

private class RssAdapter extends ArrayAdapter<RSSFeed_SelectedHotelResult> {
        private List<RSSFeed_SelectedHotelResult> rssFeedLst;

        public RssAdapter(Context context, int textViewResourceId,
                List<RSSFeed_SelectedHotelResult> rssFeedLst) {
            super(context, textViewResourceId, rssFeedLst);
            this.rssFeedLst = rssFeedLst;
            Boolean addtoShotlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {

            View view = convertView;

            if (convertView == null) {
                view = View.inflate(HotelListActivity.this, R.layout.list_row,
                        null);

                rssHolder = new RssHolder();
                rssHolder.iv_add = (ImageView) view.findViewById(R.id.iv_add);
                rssHolder.rssTitleView = (TextView) view
                        .findViewById(R.id.title);
                rssHolder.rssImagHotel = (ImageView) view
                        .findViewById(R.id.hotelImage);
                rssHolder.rssImageHotelRate = (ImageView) view
                        .findViewById(R.id.rateHotel2);
                rssHolder.rssHotelPrice = (TextView) view
                        .findViewById(R.id.textHotelRate);
                rssHolder.rssHotelAddress = (TextView) view
                        .findViewById(R.id.textHotelDesc);
                // rssHolder.rssHotelRating = (TextView)
                // view.findViewById(R.id.textHotelRating);
                rssHolder.rating_hotel = (RatingBar) view
                        .findViewById(R.id.rateHotelImage);

                view.setTag(rssHolder);

            } else {
                rssHolder = (RssHolder) view.getTag();

            }
            final RSSFeed_SelectedHotelResult rssFeed = rssFeedLst
                    .get(position);

            rssHolder.rssTitleView.setText(rssFeed.getName());
            imageLoader.DisplayImage(rssFeed.getHotel_image(),
                    rssHolder.rssImagHotel);
            imageLoader.DisplayImage_rating(rssFeed.getHote_rate_image(),
                    rssHolder.rssImageHotelRate);

            rssHolder.rssHotelPrice.setText("Avg. $" + rssFeed.getHotel_price()
                    + "/Night");

            rssHolder.rssHotelAddress.setText(rssFeed.getHotel_desc());
            rssHolder.rating_hotel.setRating(Float.valueOf(rssFeed
                    .getHotel_rate()));

            rssHolder.iv_add.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    try {
                        if (!mypos.contains(position + "")) {
                            mypos.add(position + "");
                            Toast.makeText(HotelListActivity.this,
                                    "Added Position::" + position, 1).show();
                            mypos.add(position + "");
                            HashMap<String, String> detailHash = new HashMap<String, String>();
                            detailHash.put("position", position + "");
                            detailHash.put("city", rssFeed.getCity());
                            detailHash.put("name", rssFeed.getName());
                            detailHash.put("dest_id", rssFeed.getDest_id());
                            detailHash.put("rate_img",
                                    rssFeed.getHote_rate_image());
                            detailHash.put("desc", rssFeed.getHotel_desc());
                            detailHash.put("id", rssFeed.getHotel_id());
                            detailHash.put("img", rssFeed.getHotel_image());
                            detailHash.put("price", rssFeed.getHotel_price());
                            detailHash.put("rate", rssFeed.getHotel_rate());
                            Consts.shortListedHotels.add(detailHash);
                            rssHolder.iv_add
                                    .setBackgroundResource(R.drawable.fill);

                        } else {
                            rssHolder.iv_add
                                    .setBackgroundResource(R.drawable.plus12);
                            Toast.makeText(HotelListActivity.this,
                                    "Removed postion::" + position, 1).show();
                            mypos.remove(position + "");

                            Consts.shortListedHotels.remove(position - 1);

                        }
                    } catch (IndexOutOfBoundsException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out
                            .println("::::::::::::My data in side hotel List activity:::::::::;"
                                    + position
                                    + ""
                                    + rssFeed.getName()
                                    + "\n"
                                    + rssFeed.getHotel_price());

                    /*
                     * RSSFeed_SelectedHotelResult rssFeed1 = rssFeedLst
                     * .get(position);
                     */

                }
            });

            return view;
        }
    }

    class RssHolder {
        TextView rssTitleView;
        ImageView rssImagHotel;
        ImageView rssImageHotelRate;
        ImageView iv_add;
        // TextView rssHotelRating;
        RatingBar rating_hotel;
        TextView rssHotelPrice;
        TextView rssHotelAddress;
        TextView rssMultiple_resultCity;

    }

1 个答案:

答案 0 :(得分:0)

尝试这样希望它能帮到你,

if( Consts.shortListedHotels != null && Consts.shortListedHotels.size() > 0){
  Consts.shortListedHotels.remove(position);
}