Filter返回一个空(null)数组列表

时间:2014-02-18 12:56:15

标签: java android android-filter

我有ListView包含微调器,图像和TextView。在这个ListView上面,我有一个EditText,用户可以在其中输入所需的项目名称,所有相关项目都显示在ListView中。

我的代码工作到这里,但问题是当我在搜索编辑框中输入字符串时它会这样做,当我开始删除字符时,列表视图就会消失,并且只有一个空白屏幕。虽然起初包含微调器的listView已成功创建,但在进行过滤并删除搜索框中的所有字符后,无法创建视图。

这是LazyAdapter中从BaseAdapter

扩展的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();
                }

            };

        }

这是LazyAdapter的构造函数

public  LazyAdapte(Context context , ArrayList<items> list) 
    {
        this.context = context;
        listDetails = list;
         downloader = new ImageDownloader();
    }

这是我的getCount()方法

 @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return listDetails == null ? 0 : listDetails.size();
        }

这是活动

中的代码
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 

            }
        }); 
}
}

这是堆栈跟踪

 Android Runtime E:com.example.itemspac.LazyAdapter.<init>(LazyAdapter.java:36) 
at com.example.itemspac.LazyAdapter.<init>(LazyAdapter.java:41)

此处第36行是Constructor public LazyAdapter(Context context , ArrayList<items> list),41是LazyAdapter中的imagedownloader

1 个答案:

答案 0 :(得分:0)

在您的代码中的以下代码段中:

if (TextUtils.isEmpty(constraint)){
    //Result.values = storedata;
    Result.count = listDetails.size();
    return Result;
 }

您没有设置结果。。这可能导致您的适配器的getCount()为空。