【发布时间】: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