2017-07-14 69 views
-1

我有一个通知listview.And我需要刷新列表视图内容与很短的时间间隔。但是当我调用notifyDataSetChanged()时,直到滚动底部才会有异常。在滚动底部之后,它抛出ArrayIndexOutOfBoundsException。这里是我的代码:自定义适配器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); 
} 
+0

post“init()”方法和适配器类 –

+0

我贴了2天前你问我什么@an_droid_dev – madProgrammer

+0

这里你有很多问题。首先,你从来没有将适配器设置为listview。其次,“runOnUiThread”是不需要的,因为你只是在mainThread中,第三我不明白为什么你再次在“runOnUiThread”里面调用“notifyDataSetChanged”而没有从不设置适配器到列表视图@madProgrammer –

回答

0

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

+0

实际上'getCount'返回确切的数组大小。我的意思是它正在增加和减少的操作,我已经测试与日志。但是我刚刚注意到,如果我将getViewTypeCount()的返回值增加1,它允许我添加1个项目,如果我添加第二个项目它会崩溃。所以我认为问题在于'getViewTypeCount'方法。它会返回'getCount()'...但不会动态变化... – madProgrammer

+0

@madProgrammer分享我的代码 – Vyacheslav

+0

我分享了我的代码2天前@Vyacheslav – madProgrammer

相关问题