当前位置: 首页 > 面试题库 >

在搜索中使数组脱离索引绑定异常

束建章
2023-03-14
问题内容

我有一个应用程序,其中将json中的数据显示到listview中,现在想在listview上应用搜索功能。但是每当我在editbox中输入内容时,错误数组索引就会超出界限。请帮助我进行排序。

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

适配器类

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

问题答案:

尝试这种方式,希望这将帮助您解决问题。

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


 类似资料:
  • 在测试此文本时,我在要在文本中搜索的列上创建了lucene(3.0.1)索引: 如果我按关键字搜索,它会给出结果,但按 我正在使用standardAnalyzer: 因为已设置,它应创建令牌,并且应存在$GLD。Analyzer将从文本中删除停止字,因为word也将在该过程中删除。

  • 我有大量相同类型的实体,每个实体都有大量属性,并且我只有以下两种选择来存储它们: 将每个项存储在索引中并执行多索引搜索 将所有enties存储在单个索引中,并且只搜索1个索引。 一般而言,我想要一个时间复杂度之间的比较搜索“N”实体与“M”特征在上述每一种情况!

  • 我有一个jTable,它由使用bean绑定的数据库中的数据填充。我想为表实现一个搜索函数。我遇到了以下教程,它使用bean绑定实现了这一点: https://weblogs.java.net/blog/pkeegan/archive/2008/06/using_beans_bin.html 我无法使它工作。当我运行我创建的示例应用程序时,我看到搜索文本字段上出现了一个意外的值,如“javax.sw

  • 本文向大家介绍在C#中搜索Collection中指定对象的索引,包括了在C#中搜索Collection中指定对象的索引的使用技巧和注意事项,需要的朋友参考一下 要在Collection中搜索指定对象的索引,代码如下- 示例 输出结果 这将产生以下输出- 示例 现在让我们来看另一个示例- 输出结果 这将产生以下输出-

  • 问题内容: 还有其他方法可以在Postgres的列中搜索某个值吗? 我目前安装的Postgres版本并 没有 让下面的语句: 数组示例: 该语句应返回数组包含的每一行。 问题答案: 对于相等性检查,您可以简单地: 在手册中阅读有关ANY / SOME的信息 。

  • 有没有一个函数可以在字符串的特定索引处替换一个字符串内的一个字符串一次?示例: 结果输出将是“我的文本是我的文本,你的大文本”