过滤器返回一个空(NULL)数组列表 [英] Filter returns an empty (null) array list

查看:266
本文介绍了过滤器返回一个空(NULL)数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含微调,图像和一个TextView的ListView。在此之上的ListView我有一个EditText用户可以在其中输入他需要的项目名称和所有的相关项目已经在ListView被证明。

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.

我的code是工作到这里,但问题是,当我在搜索编辑框中它的输入字符串,当我开始删除字符列表视图只是消失,只存在一个空白屏幕。而在第一个包含纺纱厂已成功创建,但在搜索框中做过滤和删除所有字符不能创建视图后,在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.

下面是用getFilter在LazyAdapter我的code,从BaseAdapter扩展

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

            };

        }

这是LazyAdapter的构造

This is the constructor of LazyAdapter

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

这是我的getCount()方法

And here is my getCount() Method

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

和这里是code在活动

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 

            }
        }); 
}
}

下面是堆栈跟踪

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

下面的行号36是构造的公共LazyAdapter(上下文的背景下,ArrayList的&LT;项目&GT;清单) 41为 imagedownloader 在LazyAdapter

Here the line number 36 is of Constructor public LazyAdapter(Context context , ArrayList<items> list) and 41 is imagedownloader in LazyAdapter

推荐答案

在从code这下面的代码片段:

In this following snippet from your code:

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

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

you have are not setting Result.values. This could result in your adapter's getCount() being null.

这篇关于过滤器返回一个空(NULL)数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆