Siddhpura Amit
Siddhpura Amit

Reputation: 15078

Filter data in listview

HI i am filtering data in listview there is a one editbox with search....

Now if i enter la and enter search button the filter will be done....

below is the code

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

Now when i click on SearchImageView the following code will be executed...

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

Now when i click on List view the following code will be executed..

    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

        }
    });

Here what should i have to write code so that i can get filterable latitude, longitude, zipcode.....

Upvotes: 1

Views: 155

Answers (3)

Nguyen  Minh Binh
Nguyen Minh Binh

Reputation: 24423

The best way is customize the Adapter class. Write a method to filter data array by yourself. After that fill the filtered data to your listview via getView() method

Upvotes: 0

Siddhpura Amit
Siddhpura Amit

Reputation: 15078

I got the answer....

Although i know that it is not efficient way but for me it's solved the problem

Here in adapter if i print adapter's value i got an answer like below

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

Upvotes: 0

Jamshid
Jamshid

Reputation: 340

you can add this function your custom adapter:

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

and you can bind this filter search to your 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) {

            }
        });

use this link: https://stackoverflow.com/questions/12930403/how-to-search-name-by-searchview/12930564#12930564

Upvotes: 1

Related Questions