views:

47

answers:

4

Hi, I have an alert dialog which lists around 100 cities. Is there a way to search within it? Or maybe autocomplete?

alt text

I want to add a textbox which will filter the list according to what I type.

EDIT: I'm very SORRY. What I meant was that I wanted to FILTER the above list based on what I type in a textbox. For example if I type 'D', I should get only Dhaka..

A: 

alert dialod which list around 100 cities? what this exactly means to?

yes for search we can use autocomplete AutoCompleteTextView Auto=new AutoCompleteTextView(this); ArrayAdapter arrAdapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,name); Auto.setAdapter(arrAdapter);

are you exactly meaning this?

Adhavan
With "alert dialog which list around 100 cities", I mean the above screenshot. How you get the idea. When i type in the text box, I want to filter the list accordingly.
rohith
s if u use autocomplete textbox u will be able to filter the items...which show the result in its own list view. but if u want your own listview to change dynamically,i think MGS answer will support you
Adhavan
+1  A: 

if suppose u have list of items in array you need to set it into list,and then use EditText for searching,you can implement it by by using EditText addTextChangedListener(this) action listener by implementing three method and do what u need

here the sample code of this implementaion has follows

@Override
    public void afterTextChanged(Editable s) {
        try{
        text = textView.getText().toString();
        string1 = text.substring(0,1).toUpperCase() + text.substring(1);
        }catch(Exception e)
        {
            Log.v("Text_Hereeeee++++++++++++++++++++++++++++e","NULL"+Integer.toString(a.length));


//          wv.setVisibility(View.GONE);    
            lv.setVisibility(View.GONE);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(Bru_Maps.this,R.layout.listitem, a); 
            lv.setAdapter(adapter);
            lv.setVisibility(View.VISIBLE);
            empty_text=2;
            temp_VALUE1=null;
            VALUE1=a;

        }
        if(empty_text==1){
        Log.v("Text from ",text);
        Set <String> has_set = new TreeSet<String>(search_List);
        if(string1!=null)
        {
            Log.v("string1_for New array",string1);
            for(int i=0;i<VALUE1.length;i++)
            {
                String a = string1;

            if(VALUE1[i].startsWith(a))
            {
//               Log.v("NoDuplicate New array","null");
                 has_set.add(VALUE1[i]);


              }
            else{
                continue;
            }
            }

        }
        Log.v("Text_Her********************e","NULL"+Integer.toString(VALUE1.length));
           carArray = new String[has_set.size()];

           has_set.toArray(carArray);

            temp_VALUE1=carArray;
            lv.setVisibility(View.GONE);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(Bru_Maps.this,R.layout.listitem, temp_VALUE1); 
            Log.v("Adapter_Get_count",Integer.toString(adapter.getCount()));
            lv.setAdapter(adapter);
            lv.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        Log.v("beforeTextChanged",s.toString());
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.v("onTextChanged",s.toString());
        empty_text=1;

    }

here i just managing two arrays, one is original from that is a and another one is sorted array temp_VALUE1 set in adapter.

MGS
I dont know, but Im not sure this is exactly what I want.. I dont think I made it clear enough :(
rohith
+1  A: 

These are just ideas, but may be you can use them.

  • What if you'll make an custom dialog with a custom design.
  • Then you can have your edit field for filtering.
  • Every time you type a new letter you should reset your list adapter.

Mur

Ps. If you have your cities in DB, you can also use cursor-adapter

UPD:

Here is an example for using usual layouts also in custom dialogs. Hence you can also put a listview. In your case it would be something like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"&gt;
 <EditText
    android:id="@+id/filter"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
 <ListView android:id="@+id/ListView01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />
</LinearLayout>
Mur Votema
Yes I was thinking of doing that. But Im relatively new to this and was trying to figure out how to put a list in the custom view :) any ideas?
rohith
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
Mur Votema