[英]Filter returns an empty (null) array list
I have ListView that contains spinner, an image and a TextView. 我有一个包含微调器,图像和TextView的ListView。 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. 在此ListView上方,我有一个EditText,用户可以在其中键入所需的项目名称,并且所有相关项目已显示在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. 最初,包含微调框的listView是成功创建的,但是在执行过滤并删除搜索框中的所有字符后,无法创建该视图。
Here is my code of getFilter in LazyAdapter that extends from BaseAdapter 这是我从BaseAdapter扩展的LazyAdapter中的getFilter代码
@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 这是LazyAdapter的构造函数
public LazyAdapte(Context context , ArrayList<items> list)
{
this.context = context;
listDetails = list;
downloader = new ImageDownloader();
}
And here is my getCount() Method 这是我的getCount()方法
@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 在这里,行号36是构造函数public LazyAdapter(Context context , ArrayList<items> list)
而行号41是imagedownloader
中的imagedownloader
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. 这可能会导致适配器的getCount()为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.