大马资讯论坛 - 马来西亚中文资讯平台

 找回密码
 注册
搜索

[教学] Android PinnedSectionListView ArrayIndexOutOfBoundsException

[复制链接]
发表于 2015-1-11 00:41:48 | 显示全部楼层 |阅读模式
超简单教学Tutorial extends BaseAdapter implements PinnedSectionListAdapter
java.lang.ArrayIndexOutOfBoundsException
  at android.widget.AbsListView$RecycleBin.addScrapView

1. Does your adapter handle at least two types of views in getViewTypeCount() method: items and sections?
Remember add in getItemViewType getViewTypeCount 记得添加 getItemViewType getViewTypeCount

2. java.lang.ArrayIndexOutOfBoundsException at android.widget.AbsListView$RecycleBin.addScrapView
getItemViewType return either 0 or 1,只能return 0 或 1

  1. public class NewsListAdapter extends BaseAdapter implements PinnedSectionListAdapter{
  2.        
  3.     private static final int[] COLORS = new int[] {
  4.             android.R.color.holo_purple, android.R.color.holo_green_light,
  5.                 android.R.color.holo_orange_light, android.R.color.holo_blue_light,
  6.                 android.R.color.holo_purple, android.R.color.holo_green_light,
  7.                 android.R.color.holo_orange_light, android.R.color.holo_blue_light,
  8.                 android.R.color.holo_purple, android.R.color.holo_green_light,
  9.                 android.R.color.holo_orange_light, android.R.color.holo_blue_light,
  10.                 android.R.color.holo_purple, android.R.color.holo_green_light,
  11.                 android.R.color.holo_orange_light, android.R.color.holo_blue_light};
  12.    
  13.         public NewsListAdapter(Context c) {
  14.         //mContext = c;
  15.     }
  16.    
  17.     public int getCount() {
  18.         return oNewsItems.size();
  19.     }

  20.     public NewsItem getItem(int position) {
  21.             return oNewsItems.get(position);
  22.     }

  23.     public long getItemId(int position) {
  24.         return position;
  25.     }

  26.         @Override
  27.     public View getView(int position, View convertView, ViewGroup parent) {
  28.                
  29.         View view;
  30.         LayoutInflater inflater = (LayoutInflater) activity
  31.           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  32.         
  33.         if(oNews.id.equals("0"))
  34.         {
  35.                 TextView txtTitle = (TextView)view.findViewById(R.id.txtTitle);
  36.             TextView.setBackgroundResource(COLORS[position % COLORS.length]);

  37.         
  38.         return view;
  39.     }
  40.    

  41.         @Override
  42.         public boolean isItemViewTypePinned(int viewType) {
  43.                 // TODO Auto-generated method stub
  44.                 return viewType == 0;
  45.         }

  46.         @Override
  47.         public int getItemViewType(int position) {
  48.                 if(Integer.parseInt(getItem(position).id) == 0)
  49.                         return 0;
  50.                 else
  51.                         return 1;
  52.         }

  53.         @Override
  54.         public int getViewTypeCount() {
  55.             return 2;
  56.         }
  57.        
  58. }
复制代码


https://github.com/beworker/pinned-section-listview

手机版|大马资讯论坛  

GMT+8, 2024-3-29 21:31 , Processed in 0.347731 second(s), 13 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表