我有ListView包含微調,圖像和TextView。在這個ListView上方,我有一個EditText,用戶可以在其中鍵入他所需的項目名稱,並且所有相關項目都顯示在ListView中。我的代碼在這裏工作,但問題是,當我嘗試刪除應用程序剛剛崩潰的最後一個字符。我認爲listview沒有正確刷新。我已經搜索了很多答案,但沒有發現任何與我的問題有關的問題,所以請幫我解決這個問題。應用程序崩潰notifyDataSetChanged()使用篩選器
這裏是LazyAdapter我用getFilter的代碼從BaseAdapter
延伸@SuppressLint("DefaultLocale")
public android.widget.Filter getFilter() {
return new android.widget.Filter() {
@SuppressLint("DefaultLocale")
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if (TextUtils.isEmpty(constraint)){
//Result.values = storedata;
Result.count = listDetails.size();
return Result;
}
ArrayList<items> Filtered_Names = new ArrayList<items>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<listDetails.size(); i++){
items searchdata = listDetails.get(i);
String title = searchdata.title();
String id = searchdata.id();
String imageUrl = searchdata.imageUrl();
String pprice = searchdata.pprice();
String shippingPrice = searchdata.shippingPrice();
String stock = searchdata.stock();
filterableString = title+" "+id+" "+imageUrl+" "+pprice+" "+shippingPrice+" "+status+" "+stitle+" "+stock+"
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(listDetails.get(i));
//Log.e("Added", String.valueOf(Filtered_Names.size()));
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
//Log.e("Results", Result.values.toString() + String.valueOf(Result.count));
return Result;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
@SuppressWarnings("unchecked")
ArrayList<items> resultList =(ArrayList<items>) results.values;
LazyAdapter.this.listDetails = resultList;
LazyAdapter.this.notifyDataSetChanged();
}
};
}
這裏是代碼在活動
inputSearch = (EditText) findViewById(R.id.inputsearch);
if(adapter!=null){
}
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
try{
ItemsActivity.this.adapter.getFilter().filter(cs.toString());
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
而且這裏是logcat的說
**
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.itemspac.LazyAdapter.getCount(LazyAdapter.java:48)
at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:778)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
at com.example.itemspac.LazyAdapter$3.publishResults(LazyAdapterGifts.java:305)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
**
這裏是我的getCount將()方法
@Override
public int getCount() {
// TODO Auto-generated method stub
return listDetails.size();
}
listDetails在調用getCount()方法時顯然爲空。也許改成'''返回listDetails == null? 0:listDetails.size();''' – peshkira
@peshkira你的意思是在getFilter調用listDetails後null?而起初視圖是成功創建的。 –
我不能確定,但我假設在你的'''publishResults''中,你設置爲LazyAdapter的ArrayList爲null。也許在那裏添加一個檢查來驗證。 – peshkira