问题
I have ListView that contains spinner, an image and a TextView. Above this ListView I have an EditText where a user can type his required item name and all the related items has been shown in the ListView.
My code is working till here but the problem is when i enter string in the search edit box it does that and when i start to remove the characters the list view is just disappears and there is only a blank screen. while at first the listView that contains the spinners was created successfully but after doing filter and removing all characters in the search box the view cannot be created.
Here is my code of getFilter in LazyAdapter that extends from 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();
}
};
}
This is the constructor of LazyAdapter
public LazyAdapte(Context context , ArrayList<items> list)
{
this.context = context;
listDetails = list;
downloader = new ImageDownloader();
}
And here is my getCount() Method
@Override
public int getCount() {
// TODO Auto-generated method stub
return listDetails == null ? 0 : listDetails.size();
}
and here is the code in the Activity
public class ItemsActivity extends Activity{
EditText inputsearch;
LazyAdapter adapter;
ArrayList<items> list;
Controller control;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.items_activity);
RelativeLayout layout = (RelativeLayout) findViewById(R.id._view);
control = (Controller) getApplicationContext();
list = (ArrayList<items>) control.Table_items.GetData();
final ListView listview = (ListView)findViewById(R.id._data);
final LazyAdapter adapter = new LazyAdapter(this, list);
adapter.setLayout(this, layout);
listview.setAdapter(adapter);
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
}
});
}
}
Here is the stack trace
Android Runtime E:com.example.itemspac.LazyAdapter.<init>(LazyAdapter.java:36)
at com.example.itemspac.LazyAdapter.<init>(LazyAdapter.java:41)
Here the line number 36 is of Constructor public LazyAdapter(Context context , ArrayList<items> list)
and 41 is imagedownloader
in LazyAdapter
回答1:
In this following snippet from your code:
if (TextUtils.isEmpty(constraint)){
//Result.values = storedata;
Result.count = listDetails.size();
return Result;
}
you have are not setting Result.values. This could result in your adapter's getCount() being null.
来源:https://stackoverflow.com/questions/21854534/filter-returns-an-empty-null-array-list