NullPointerException异常在LazyAdapter [英] NullPointerException at LazyAdapter

查看:134
本文介绍了NullPointerException异常在LazyAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过创建一个自定义过滤器上执行LazyAdapter类的搜索。但是,当我试图通过TextWatcher执行搜索,应用程序迫使近。
在code为LazyAdapter类如下:
    包com.demo.directory;
    公共类LazyAdapter延伸BaseAdapter {

I am trying to perform a search on LazyAdapter class by creating a custom Filter. But when I am trying to perform the search by using the TextWatcher, the application is forcing close. The code for the LazyAdapter class is as follows: package com.demo.directory; public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public View getFilter(CharSequence seq, View convertView) {

    String name_org;
    View vi = convertView;
    convertView = null;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView name = (TextView) vi.findViewById(R.id.name_org); // title
    TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
                                                                        // name
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                            // image

    for (int i = 0; i < data.size(); i++) {
        HashMap<String, String> org = new HashMap<String, String>();
        org = data.get(i);
        name_org = org.get(OrganizationActivity.KEY_NAME);

        if (name_org != null && seq != null) {
            if (name_org.contains(seq)) {
                name.setText(org.get(OrganizationActivity.KEY_NAME));
                address.setText(org.get(OrganizationActivity.KEY_CITY)
                        + ", " + org.get(OrganizationActivity.KEY_STATE));
                imageLoader.DisplayImage(
                        org.get(OrganizationActivity.KEY_IMAGE_URL),
                        thumb_image);
                notifyDataSetChanged();
            } else {
                org.remove(OrganizationActivity.KEY_NAME);
            }
        }
    }
    return vi;
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView name = (TextView) vi.findViewById(R.id.name_org); // title
    TextView address = (TextView) vi.findViewById(R.id.address_org); // artist
                                                                        // name
    ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb
                                                                            // image

    HashMap<String, String> org = new HashMap<String, String>();
    org = data.get(position);

    // Setting all values in listview
    name.setText(org.get(OrganizationActivity.KEY_NAME));
    address.setText(org.get(OrganizationActivity.KEY_CITY) + ", "
            + org.get(OrganizationActivity.KEY_STATE));
    imageLoader.DisplayImage(org.get(OrganizationActivity.KEY_IMAGE_URL),
            thumb_image);
    return vi;
}
}

以上code现在工作,但如果数据的EditText正在搜索后清零回到它不显示任何东西,如果我删除搜索数据,也就是说,不返回的ListView的原始数据..

The above code is now working, but it doesn't show anything if I delete the search data, i.e., the original data of the ListView is not returned if the EditText data is cleared back after searching..

推荐答案

要保留数据的项目,你清除搜索的EditText后,低于code是有用的。希望这可以帮助你。

To retain data items after you clear search edittext, below code is useful. Hope this helps you.

public Filter getFilter() 
{
    Filter filter = new Filter() 
    {           
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) 
        {
            System.out.println(" --- constraint in publishResults --- "+constraint+" - results - "+results);
            itemDetailList = (List<Dish>)results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 
        {
            FilterResults filterResults = new FilterResults();
            List<Dish> filtered_list = new ArrayList<Dish>();
            if(originalItemsDetails == null)
            {
                System.out.println(" --- originalItemsDetails IS NULL --- ");
                originalItemsDetails = new ArrayList<Dish>(itemDetailList);
            }
            else
            {
                System.out.println(" --- originalItemsDetails NOT NULL --- ");
            }

            if(constraint == null || constraint.length() == 0) 
            {
                filterResults.count = originalItemsDetails.size();
                filterResults.values = originalItemsDetails;
            } 
            else 
            {
                constraint = constraint.toString().toLowerCase();
                for(int i=0; i<originalItemsDetails.size(); i++) 
                {
                    String dataNames = originalItemsDetails.get(i).getDishName().toLowerCase();
                    if(dataNames.startsWith(constraint.toString()))  
                    {
                        filtered_list.add(originalItemsDetails.get(i));
                    }
                }

                filterResults.count = filtered_list.size();
                System.out.println(" --- filterResults.count --- "+filterResults.count);

                filterResults.values = filtered_list;
                System.out.println(" --- filterResults.values --- "+filterResults.values);
            }

            System.out.println(" --- constraint in performFiltering --- "+constraint);

            return filterResults;
        }
    };

    return filter;
}

这篇关于NullPointerException异常在LazyAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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