【问题标题】:Custom adapter notifyDataSetChanged() causes ArrayIndexOutOfBoundsException自定义适配器 notifyDataSetChanged() 导致 ArrayIndexOutOfBoundsException
【发布时间】:2017-07-14 13:46:57
【问题描述】:

我有一个通知列表视图。我需要在短时间内刷新列表视图内容。但是当我调用 notifyDataSetChanged() 时,直到滚动底部都没有例外。滚动底部后,它会抛出 ArrayIndexOutOfBoundsException。有我的代码:

@Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
       setContentView(R.layout.activity_notification);
       init();
       runOnUiThread(new Runnable() {
           public void run() {
               adapter.notifyDataSetChanged();
               notificationsListView.postDelayed(this, 1000);
               Log.i("Checking", "...");
           }
       });
   }

Adapter.java

public class NotificationsListViewAdapter extends BaseAdapter {
   RealmResults<myNotification> notifications;
   Context context;
   public NotificationsListViewAdapter(Context context, RealmResults<myNotification> notifications){
       this.context = context;
       this.notifications = notifications;
   }
   @Override
   public int getCount() {
       Log.e("NOT SIZE ADAPTER COUNT", String.valueOf(notifications.size()));
       return notifications.size();
   }

   @Override
   public myNotification getItem(int position) {
       return notifications.get(position);
   }

   @Override
   public int getViewTypeCount() {
       Log.e("NOT SZE ADPTR VT COUNT", String.valueOf(notifications.size()));
       return notifications.size();
   }

   @Override
   public int getItemViewType(int position) {

       return position;
   }

   @Override
   public long getItemId(int position) {
       return 0;
   }

   @Override
   public View getView(int position, final View convertView, ViewGroup parent) {
       View notificationsRowView = convertView;

       // i am not posting getView() it is 400 row code and im sure error is not coming from getView()

       return notificationsRowView;
   }
}

顺便说一句初始化函数;

void init(){
     realm = Realm.getDefaultInstance();
     notifications = realm.where(myNotification.class).findAllSorted("messageID",Sort.DESCENDING);
     notificationsListView = (ListView) findViewById(R.id.notificationActivity_notificationsListView);
     adapter = new NotificationsListViewAdapter(NotificationActivity.this,notifications);
     notificationsListView.setAdapter(adapter); 
}

【问题讨论】:

  • 发布“init()”方法和适配器类
  • 我在两天前发布了你问我什么@an_droid_dev
  • 这里有很多问题。首先,您永远不会将适配器设置为 listview。其次,不需要“runOnUiThread”,因为您只是在 mainThread 中,第三,我不明白为什么您在“runOnUiThread”中再次调用“notifyDataSetChanged”而从未将适配器设置为 listview @madProgrammer
  • 我的错误,我把错误的行复制到这里...。我正在设置适配器。否则,当我指向我的问题时,我怎么能向下滚动:) 我正在使用线程来检查适配器列表视图中拍摄间隔的任何变化。但是您的解决方案仍然没有回答“ArrayIndexOutOfBoundsException”问题...@an_droid_dev
  • 你确定在你的 getView 中没有一些奇怪的位置增量吗? @madProgrammer

标签: android listview custom-adapter notifydatasetchanged


【解决方案1】:

看来,您的代码错误地计算了适配器内部的getCount()。 发布您的元素的存储和适配器代码以便确定。

【讨论】:

  • 实际上getCount 返回精确的数组大小。我的意思是它通过操作增加和减少,我用日志测试过它。但是我刚刚注意到,如果我将 getViewTypeCount() 返回值增加 1 ,它允许我添加 1 项,如果我添加 2nd 它会崩溃。所以我认为问题出在getViewTypeCount 方法上。它正在返回getCount() ...但没有动态变化...
  • @madProgrammer.ok 我会尽快检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多