Ich habe eine Liste von Benachrichtigungen. Und ich muss Listview Inhalt mit kurzen Intervallen aktualisieren. Aber wenn ich notifyDataSetChanged() aufrufe, gibt es keine Ausnahme bis zum Ende des Bildlaufs. Nach dem unteren Bildlauf wird ArrayIndexOutOfBoundsException ausgelöst. Es ist mein Code:Benutzerdefinierter Adapter notifyDataSetChanged() verursacht 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;
}
}
Btw init-Funktion;
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()" Methode und Adapter Klasse –
ich gepostet, was hast du mich gefragt, vor 2 Tagen @an_droid_dev – madProgrammer
hier haben Sie eine Menge Probleme. Erstens, du hast den Adapter nie auf listview gesetzt. Zweitens wird "runOnUiThread" nicht benötigt, weil Sie gerade in mainThread sind und drittens verstehe ich nicht, warum Sie "notifyDataSetChanged" innerhalb von "runOnUiThread" erneut aufrufen, ohne den Adapter auf listview @madProgrammer –