我有一个通知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);
}
post“init()”方法和适配器类 –
我贴了2天前你问我什么@an_droid_dev – madProgrammer
这里你有很多问题。首先,你从来没有将适配器设置为listview。其次,“runOnUiThread”是不需要的,因为你只是在mainThread中,第三我不明白为什么你再次在“runOnUiThread”里面调用“notifyDataSetChanged”而没有从不设置适配器到列表视图@madProgrammer –