標籤:應景的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 指標