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

答えて

0

あなたのコードは、アダプタ内でgetCount()を間違って計算しているようです。 エレメントの保管場所とアダプターコードを確実に投稿してください。

+0

実際には、 'getCount'は正確な配列サイズを返します。私はそれが操作によって増減していることを意味します、私はログでそれをテストしました。しかし、私は 'getViewTypeCount()'の値を1だけ増やすと、2回追加すると1つのアイテムが追加されることに気付きました。だから問題は 'getViewTypeCount'メソッドであると思います。そして、それは 'getCount()'を返しています...しかし、動的に変化していません... – madProgrammer

+0

@madProgrammerはあなたのコードを共有します – Vyacheslav

+0

私は2日前にコードを共有しました@Vyacheslav – madProgrammer

関連する問題