私は通知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に設定しないでください。第2に、mainThreadに入っているために3番目に "runOnUiThread"が必要ではなく、3番目に "notifyDataSetChanged"を "runOnUiThread"の中に入れ直した理由がわからないのですが、アダプタをlistview @madProgrammerに設定していない場合。 –