【问题标题】:java.lang.IndexOutOfBoundsException: Invalid index 8, size is 0java.lang.IndexOutOfBoundsException:索引 8 无效,大小为 0
【发布时间】:2013-10-31 10:55:49
【问题描述】:

当我向上拖动列表并尝试显示所有列表项时,它会给我这样的错误。 这是我在后台工作的 AsyncTask。 请给出一些无错误的提示...... 服务会在几秒钟内调用此 asyncTask。

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... args) {
        String url = args[0];
        rssItems = rssParser.getRSSFeedItems(url);
        FeedDBHandler rssDb = new FeedDBHandler(getApplicationContext());
        // RSSItem rssItem;
        rssItems.size();
        Log.i("size", "size:" + rssItems.size());

        for (RSSItem item : rssItems) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // Truncating description
            String description = item.getDescription();
            if (description.length() > 100)
                description = description.substring(3, 97) + "..";

            // Store in database
            rssItem = new RSSItem(item.getTitle(), item.getLink(),
                    item.getCategory(), description, item.getPubdate());

            // check if not exist -notify and insert
            if (!rssDb.isExistItem(item.getLink())) {
                createNotification(item.getTitle());
                rssDb.addFeed(rssItem);
            }
            createNotification(item.getTitle());
            if (map != null) {
                map.put(TAG_TITLE, item.getTitle());
                map.put(TAG_LINK, item.getLink());
                map.put(TAG_CATEGORY, item.getCategory());
                map.put(TAG_DESRIPTION, description);
                map.put(TAG_PUB_DATE, item.getPubdate());

                rssItemList.add(map);
            }
        }

        return rssItemList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> rssItemList) {
        ListAdapter adapter = new SimpleAdapter(AndroidRSSReaderList.this,
                rssItemList, R.layout.rss_item_list_row,
                new String[] { TAG_LINK, TAG_TITLE, TAG_DESRIPTION,
                        TAG_PUB_DATE }, new int[] { R.id.page_url,
                        R.id.title, R.id.link, R.id.pub_date });

        // updating listview
        lv.setAdapter(adapter);
        lv.deferNotifyDataSetChanged();
        // lv.onDataSetChanged();

        // dismiss the dialog after getting all products
        // pDialog.dismiss();

    }
}

在此先感谢...

【问题讨论】:

  • 您知道为什么在您的 http 调用之后列表仍然包含 0 个元素吗?
  • 我没有使用http调用,请通过代码给我一些提示,这样我就可以很清楚地理解..谢谢
  • 首先,提供错误发生的位置非常有用。将问题缩小到一个不错的范围。
  • 实际上,当我将生成的列表向上移动时,不幸的是它停止了
  • 错误应该在`rssItemList.add(map);`行中。根据我的观点,它在向上移动时会变空...

标签: android android-listview android-asynctask indexoutofboundsexception


【解决方案1】:
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        rssItemList.clear();
    }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... args) {
        // updating UI from Background Thread
        FeedDBHandler rssDb = new FeedDBHandler(getApplicationContext());

        // listing all RSSItems from SQLite
        List<RSSItem> rssList = rssDb.getAllItems();

        // loop through each RSSItem
        for (int i = 0; i < rssList.size(); i++) {

            RSSItem s = rssList.get(i);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_TITLE, s.getTitle());
            map.put(TAG_LINK, s.getLink());
            map.put(TAG_CATEGORY, s.getCategory());
            map.put(TAG_DESRIPTION, s.getDescription());
            map.put(TAG_PUB_DATE, s.getPubdate());
            // adding HashList to ArrayList
            rssItemList.add(map);

        }
        return rssItemList;
    }

    @Override
    protected void onPostExecute(final ArrayList<HashMap<String, String>> rssItemList) {
        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        AndroidRSSReaderList.this, rssItemList,
                        R.layout.rss_item_list_row, new String[] {
                                TAG_LINK, TAG_TITLE, TAG_PUB_DATE,
                                TAG_DESRIPTION }, new int[] {
                                R.id.page_url, R.id.title, R.id.pub_date,
                                R.id.link });

                // updating listview
                setListAdapter(adapter);
            }
        });
    }

我已经通过一些改变解决了这个问题......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多