在搜索中获取索引绑定异常的数组

时间:2014-08-11 05:25:57

标签: android arrays listview

我有一个应用程序,其中我将数据从json显示到listview,现在想要在listview上应用搜索功能。但是每当我在编辑框中键入内容时,都会获得错误数组索引超出限制。请帮助我把它搞定。

HomeActivity

public class HomeActivity extends Fragment {


    String countryCode;
    ArrayList<ProfileBean> catagery;
    EditText search;
    AdaptorClass adapter;
    public static String Bussinessurl;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.homeactivity);

         View rootView = inflater.inflate(R.layout.homeactivity, container, false);
        //Log.i("homeurl", "http://chabu.agicent.com/api/v1/get_business?token=" +Login.USERTOKEN.toString());
         search=(EditText)rootView.findViewById(R.id.search);

        catagery = new ArrayList<ProfileBean>();
        new JSONAsyncTask()
                .execute(Login.AllChabu);

        ListView listview = (ListView)rootView. findViewById(R.id.list);
        adapter = new AdaptorClass(getActivity(), R.layout.row,
                catagery);
        listview.setItemsCanFocus(false);
        //listview.setOnItemClickListener(this);
        listview.setTextFilterEnabled(true);
        listview.setAdapter(adapter);
         countryCode = GetCountryZipCode();
            Log.i("country code", countryCode);
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {
                // TODO Auto-generated method stub
                //Toast.makeText(getActivity(),
                    //  catagery.get(position).getcategory_name(), Toast.LENGTH_LONG)
                        //.show();
                position = position+1;


                Log.i("url", Bussinessurl);

                Intent i= new Intent( getActivity(), BusinessActivity.class);
                startActivity(i);
            }
        });

         search.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    search.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void afterTextChanged(Editable arg0) {

                        }

                        @Override
                        public void beforeTextChanged(CharSequence s, int start,
                                int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start,
                                int before, int count) {
                            adapter.getFilter().filter(s.toString());

                        }
                    });
                }
            });

        return rootView;
    }

AdaptorClass

public class AdaptorClass extends ArrayAdapter<ProfileBean> implements Filterable {
    private List<ProfileBean> originalData = null;
    private List<ProfileBean> filteredData = null;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;
    private ItemFilter mFilter = new ItemFilter();

    public AdaptorClass(Context context, int resource,
            ArrayList<ProfileBean> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        //actorList = objects;
        this.filteredData = objects;
        this.originalData = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);

            holder.tvDescription = (TextView) v
                    .findViewById(R.id.tvDescriptionn);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.tvDescription
                .setText(filteredData.get(position).getcategory_name());

        return v;

    }

    static class ViewHolder {

        public TextView tvDescription;


    }

    public android.widget.Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends android.widget.Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<ProfileBean> list = originalData;

            int count = list.size();
            Log.i("count", String.valueOf(count));
            final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count);

            String filterableString;

            for (int i = 0; i <count; i++) {

                ProfileBean bean = list.get(i);
                filterableString = bean.getcategory_name();
                if (filterableString.toLowerCase().contains(
                        filterString.toString().toLowerCase())) {
                    nlist.add(bean);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }


        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            filteredData  = (ArrayList<ProfileBean>) results.values;
            notifyDataSetChanged();
        }
    }

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

Home Fragment

public class HomeActivity extends Fragment {


    String countryCode;
    ArrayList<ProfileBean> catagery;
    EditText search;
    AdaptorClass adapter;
    public static String Bussinessurl;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View rootView = inflater.inflate(R.layout.homeactivity, container, false);
        //Log.i("homeurl", "http://chabu.agicent.com/api/v1/get_business?token=" +Login.USERTOKEN.toString());
        search = (EditText) rootView.findViewById(R.id.search);

        catagery = new ArrayList<ProfileBean>();
        new JSONAsyncTask()
                .execute(Login.AllChabu);

        ListView listview = (ListView) rootView.findViewById(R.id.list);
        adapter = new AdaptorClass(getActivity(), catagery);
        listview.setItemsCanFocus(false);
        listview.setTextFilterEnabled(true);
        listview.setAdapter(adapter);
        countryCode = GetCountryZipCode();
        Log.i("country code", countryCode);
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long id) {

                //Toast.makeText(getActivity(),
                //  catagery.get(position).getcategory_name(), Toast.LENGTH_LONG)
                //.show();
                position = position + 1;
                Log.i("url", Bussinessurl);
                Intent i = new Intent(getActivity(), BusinessActivity.class);
                startActivity(i);
            }
        });

        search.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                String text = search.getText().toString();
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
            }
        });

        return rootView;
    }
}

适配器类

public class AdaptorClass extends BaseAdapter {
    private List<ProfileBean> originalData;
    private List<ProfileBean> filteredData;
    private Context context;

    public AdaptorClass(Context context,ArrayList<ProfileBean> originalData) {
        this.context=context;
        this.originalData = originalData;
        filteredData =new List<ProfileBean>();
        filteredData.addAll(this.originalData);
    }

    @Override
    public int getCount() {
        return filteredData.size();
    }

    @Override
    public Object getItem(int position) {
        return filteredData.get(position);
    }

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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
            holder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescriptionn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvDescription.setText(filteredData.get(position).getcategory_name());
        return convertView;
    }

    class ViewHolder {
        public TextView tvDescription;
    }

    public void filter(String charText) {
        filteredData.clear();
        if (charText.length() == 0) {
            filteredData.addAll(originalData);
        } else {
            for (ProfileBean bean : originalData) {
                if (bean.getcategory_name().toLowerCase().contains(charText.toLowerCase())) {
                    filteredData.add(bean);
                }
            }
        }
        notifyDataSetChanged();
    }
}

答案 1 :(得分:0)

为什么在这里应用toString方法:

adapter.getFilter().filter(s.toString());

当过滤方法似乎需要字符时:

performFiltering(CharSequence constraint)