PullToRefresh + EndlessAdapter给出了IndexOutofBoundException

时间:2013-01-28 13:58:32

标签: android cwac-endless pull-to-refresh

我正在尝试使用PulltoRefresh和EndlessAdapter,第一次运行应用程序时,列表加载得非常好,但是当它最终加载更多数据时,它会崩溃。

Library EndLessAdapter:https://github.com/commonsguy/cwac-endless

Library PulltoRefresh:https://github.com/chrisbanes/Android-PullToRefresh

没有PullToRefresh就可以了。

该应用首次运行时,会调用:

  1. getPendingView
  2. MyAsyncTask
  3. return hasmoredata
  4. 致电postOnExecute - listener
  5. 致电OnItemsReady
  6. 设置DemoAdapter.notydatasetchanged
  7. 当它到达列表的末尾时,它会调用:

    1. getPendingView
    2. MyAsyncTask
    3. return hasmoredata
    4. 提供IndexOutofBoundException
    5. 堆栈跟踪:

      01-28 11:12:15.273: E/AndroidRuntime(12735): FATAL EXCEPTION: main
      01-28 11:12:15.273: E/AndroidRuntime(12735): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at java.util.ArrayList.get(ArrayList.java:311)
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at android.widget.AbsListView.obtainView(AbsListView.java:1592)
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at android.widget.ListView.makeAndAddView(ListView.java:1782)
      01-28 11:12:15.273: E/AndroidRuntime(12735):    at android.widget.ListView.fillDown(ListView.java:705)
      

      在PullToRefreshListinViewPager示例中:

      public View instantiateItem(ViewGroup container, int position) {
          PullToRefreshListView plv = null;
          Context context = container.getContext();
      
          plv = (PullToRefreshListView) LayoutInflater.from(context).inflate(
                              R.layout.layout_listview_in_viewpager, container, false);
          endlessList = new ArrayList<HashMap<String, String>>(); 
          demoAdapter = new DemoAdapter(context, endlessList); 
          demoAdapter.setRunInBackground(false);
          plv.setAdapter(demoAdapter);
      
          plv.setOnRefreshListener(BaseSampleActivity.this);
      
          // Now just add ListView to ViewPager and return it
          container.addView(plv, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
          return plv;
      }
      

      EndLessAdapter:

      private class DemoAdapter extends EndlessAdapter implements IItemsReadyListener{
      
            private RotateAnimation rotate=null;
            private View pendingView=null;
            private ArrayList<HashMap<String, String>> data;
            private List<HashMap<String, String>> mOriginalNames;
            private boolean hasMoreData=true;
            private Context mContext;
      
            DemoAdapter(Context context, ArrayList<HashMap<String, String>> d) {
      
              super(new LazyAdapter(context, d));
              this.data = d;
              this.mContext = context;
              rotate=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF,
                        0.5f, Animation.RELATIVE_TO_SELF,
                        0.5f);
      
              rotate.setRepeatMode(Animation.RESTART);
              rotate.setRepeatCount(Animation.INFINITE);
            }
      
            @Override
            protected View getPendingView(ViewGroup parent) {
                View row = LayoutInflater.from(mContext).inflate(R.layout.row, null);
      
                    View child=row.findViewById(android.R.id.text1);
      
                    child.setVisibility(View.GONE);
      
                    child=row.findViewById(R.id.throbber);
                    child.setVisibility(View.VISIBLE);
                    child.startAnimation(rotate);
                  return(row);
            }
      
      
      
            @Override
            protected boolean cacheInBackground() {
              if(isNetworkOnline() == true){
                new MyAsyncTask(this, data.size()).execute();
              }
              return hasMoreData;
            }
      
            public void onItemsReady(ArrayList<HashMap<String, String>> data) {
                endlessList.addAll(data);
                demoAdapter.onDataReady();  // Tell the EndlessAdapter to remove it's pending
                                        // view and call notifyDataSetChanged()
                hasMoreData = endlessList.isEmpty();
              }
      
            @Override
            protected void appendCachedData() {
              if (getWrappedAdapter().getCount()<75) {
                @SuppressWarnings("unchecked")
                LazyAdapter a=(LazyAdapter)getWrappedAdapter();
                for (int i = 0; i < data.size(); i++) {
                    a.add(data.get(i));
                }
              }
            }
          }
      

      的AsyncTask:

      private static class MyAsyncTask extends AsyncTask<Void,Void,ArrayList<HashMap<String, String>>>{
              IItemsReadyListener listener;
      
              int startPoint;
      
              public MyAsyncTask(IItemsReadyListener listener, int startPoint) {
                  // TODO Auto-generated constructor stub
                  this.listener = listener;
                  this.startPoint = startPoint;
              }
      
              @Override
              protected ArrayList<HashMap<String, String>> doInBackground(Void... params) {
      
                  XMLParser parser = new XMLParser();
                  String xml = parser.getXmlFromUrl(URL); // getting XML from URL
                  Document doc = parser.getDomElement(xml); // getting DOM element
      
                  NodeList nl = doc.getElementsByTagName(KEY_SONG);
                  // looping through all song nodes <song>
                  for (int i = 0; i < 10; i++) {
                      // creating new HashMap
                      HashMap<String, String> map = new HashMap<String, String>();
                      Element e = (Element) nl.item(i);
                      // adding each child node to HashMap key => value
                      map.put(KEY_ID, parser.getValue(e, KEY_ID));
                      map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                      map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                      map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                      map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                      // adding HashList to ArrayList
                      endlessList.add(map);
                  }
                  return (endlessList);
              }
      
      
      
      
              @Override
              protected void onPostExecute(ArrayList<HashMap<String, String>> result)
              {
                  listener.onItemsReady(endlessList);
                   // Getting adapter by passing xml data ArrayList
                  if (demoAdapter != null) 
                  {
                      demoAdapter.notifyDataSetChanged();
                  }
      
              }
      }
      

      更新

      我只是改变:

            @Override
            protected boolean cacheInBackground() throws Exception {
              if(isNetworkOnline() == true){
                new MyAsyncTask(this).execute();
              }
              if (getWrappedAdapter().getCount()<75) {
                  return(true);
                }
              throw new Exception("Gadzooks!");
      
              }
      

      现在它加载更多内容,但是在加载3次并进入final后,它会抛出Exception Gadzooks并在它显示相同的IndexOutofBoundException之后。

      我只知道每次cacheinBackground返回false时,它都会崩溃。

0 个答案:

没有答案