1

嗨,我正在列表视图中过滤数据,有一个带有搜索功能的编辑框....

现在,如果我输入 la 并输入搜索按钮,过滤器将完成....

下面是代码

    double lat[] = new double[] { 32.711857, 32.80964, 32.751261, 32.753932, 32.751622, 32.769955, 32.770677, 34.304875, 32.854782, 32.761829 };
double lon[] = new double[] { -117.159901, -117.238223, -117.247137, -117.16355, -117.165782, -117.162005, -117.16561, -118.461662, -117.27464,
        -117.172343 };

double latitude, longitude;

String places[] = new String[] { "Gaslamp", "Pacific Beach", "Ocean Beach", "Uptown", "Hillcrest", "Mission Valley", "Fashion Valley",
        "La Jolla", "San Diego","North Park" };

int zipCode[] = new int[] { 92101, 92109, 92107, 92103, 92103, 92123, 92008, 92037, 92115, 92104 };

现在,当我单击 SearchImageView 时,将执行以下代码...

  ImageView imgSearch = (ImageView) findViewById(R.id.ivIconSearch);
    imgSearch.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (adapter != null) {
                adapter.getFilter().filter(edtSearch.getText().toString());
                adapter.notifyDataSetChanged();
            }
            return true;
        }
    });

现在,当我单击列表视图时,将执行以下代码..

    adapter = new SimpleAdapter(PredefinedLocation.this, fillMaps, R.layout.list_item_deal, new String[]{"name"}, new int[]{R.id.name});
    // Adding data into listview
    lv.setAdapter(adapter);
    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            // TODO Auto-generated method stu

        }
    });

在这里,我应该编写什么代码才能获得可过滤的纬度,经度,邮政编码.....

4

3 回答 3

1

您可以将此功能添加到您的自定义适配器:

 @Override public Filter getFilter() {

            Filter myFilter = new Filter() {

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    // TODO Auto-generated method stub
                    if (!m_orders.isEmpty()){
                        clear();
                    }

                    for (int i = 0; i < tempList.size(); i++) {
                        if(tempList.get(i).getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                            add(tempList.get(i));
                        }
                    }

                    notifyDataSetChanged();
                }

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    // TODO Auto-generated method stub
                    return null;
                }
            };  

            return myFilter;
        }

您可以将此过滤器搜索绑定到您的editText:

et_search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

                m_adapter.getFilter().filter(s, new FilterListener() {

                    @Override
                    public void onFilterComplete(int count) {
                        // TODO Auto-generated method stub
                        m_adapter.notifyDataSetChanged();
                    }
                });
            }

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

            }

            @Override
            public void afterTextChanged(Editable str) {

            }
        });

使用此链接:https ://stackoverflow.com/questions/12930403/how-to-search-name-by-searchview/12930564#12930564

于 2012-10-20T11:41:23.400 回答
0

我得到了答案......

虽然我知道这不是有效的方法,但对我来说它解决了问题

在适配器中,如果我打印适配器的值,我会得到如下答案

     System.out.println(adapter);

  I got value like below ....

    "{name=SanDeigo}"


  based on that....


    Intent intent = new Intent(PredefinedLocation.this, MainActivity.class);
            int length = "{name=".length();
            String str = adapter.getItem(position).toString();
            if (str != null)
                if (str.length() > 0)
                    str = str.substring(length, str.length() - 1);

            for (int i = 0; i < places.length; i++) {
                if (places[i].equals(str)) {
                    intent.putExtra("nearLocation", places[i]);
                    //System.out.println("Near Location = " + places[i]);
                    intent.putExtra("latitude", lat[i]);
                    //System.out.println("LATITUDE = " + lat[i]);
                    intent.putExtra("longitude", lon[i]);
                    //System.out.println("LONGITUDE = " + lon[i]);
                    intent.putExtra("zipcode", zipCode[i]);
                    //System.out.println("ZIP CODE = " + zipCode[i]);
                    PredefinedLocation.this.startActivity(intent);
                    break;
                }
            }
于 2012-10-22T05:41:41.850 回答
0

最好的方法是自定义 Adapter 类。自己写一个过滤数据数组的方法。getView()之后通过方法将过滤后的数据填充到您的列表视图中

于 2012-10-22T05:56:37.760 回答