java.lang.IllegalStateException: Observer AdapterDataSetObserver was not register setadapternull 指標

來源:互聯網
上載者:User

標籤:應景的adapter   百變的adapter   illegalsta   notregiste   

如果你和我描述的以下幾點情況一樣,那麼這個我檢查出來的bug解決方案可能能解決你的問題

1.setadapter放在onclick事件裡面
2.適配器的寫法是標準的最佳化過後的
3.資料有時為空白,有時可以適配,只要為空白的時候一次,再有資料就不能適配了

解決方案:

思路:在適配器中判斷資料是否為空白,如果為空白就new 一個數組給它當值,
如果不為空白就等於傳入的值

做法:一開始就給adapter的資料來源為null,自己寫一個updatedata方法
點擊之後直接更新,setadapter的是null
具體作業碼:

**adapter構造方法**public GovernmentInstitutionAdapter(Context mContext,            List<GovernmentInstitutionBean> institutionBeansList) {        this.mContext = mContext;        //判斷是否為空白        if(institutionBeansList == null){            institutionBeansList = new ArrayList<GovernmentInstitutionBean>();        }else{        this.institutionBeansList = institutionBeansList;        }    }    //自己寫的updatedata方法    public void updateAdapter(List<GovernmentInstitutionBean> institutionBeansList) {        if (institutionBeansList == null)             this.institutionBeansList = new ArrayList<GovernmentInstitutionBean>();        else            this.institutionBeansList = institutionBeansList;        this.notifyDataSetChanged();    }

activity中的具體步驟:

    searchButton.setOnClickListener(new OnClickListener() {    //1.adapter的初始化,注意資料為null            GovernmentInstitutionAdapter adapter = new GovernmentInstitutionAdapter(                                getApplicationContext(), null);            @Override            public void onClick(View v) {    .......    ....    //2.調用updateadapter方法就相當於適配了一次        adapter.updateAdapter(動態要適配的資料來源);    ...    //3.最後setadapter        listview.setAdapter(adapter);}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java.lang.IllegalStateException: Observer AdapterDataSetObserver was not register setadapternull 指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.