Search AlertDialog

I have a warning dialog that lists about 100 cities. Is there a way to search in it? Or maybe autocomplete?

alt text

I want to add a text box that will filter the list according to what I'm typing.

EDIT: I very much ASK. I meant that I would like to FILTER the above list based on what I type in the text box. For example, if I type 'D', I should only get Dhaka ..

+3
source share
5 answers

Well, finally, I managed to do this through a special dialogue.

citylistview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="200dip" 
    android:orientation="vertical"
    android:gravity="center">
    <EditText android:id="@+id/EditBox" 
        android:maxLines="1"
        android:layout_height="wrap_content"
        android:layout_width="180dip"
        android:gravity="left">
    </EditText> 
    <ListView android:id="@+id/List"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice">
    </ListView>
</LinearLayout>

CityListDialog.java

public class CityListDialog extends Dialog implements OnClickListener {

private ListView list;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
private static final String TAG = "CityList";

public CityListDialog(Context context, String[] cityList) {
    super(context);

    /** Design the dialog in main.xml file */
    setContentView(R.layout.citylistview);
    this.setTitle("Select City");
    filterText = (EditText) findViewById(R.id.EditBox);
    filterText.addTextChangedListener(filterTextWatcher);
    list = (ListView) findViewById(R.id.List);
    adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, cityList);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Log.d(TAG, "Selected Item is = "+list.getItemAtPosition(position));
        }
    });
}
@Override
public void onClick(View v) {

}
private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

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

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        adapter.getFilter().filter(s);
    }
};
@Override
public void onStop(){
    filterText.removeTextChangedListener(filterTextWatcher);
}}

, , . . alt text

+6

, u , , EditText , EditText addTextChangedListener (this) action listenener, ,

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

    }

, , - temp_VALUE1, .

+1

, , , .

  • , .
  • .
  • , , reset .

Ps. , -

UPD:

. , listview. :

<?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">
 <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>
+1

@rohith , , , , !

, , , .

:

 public class SearchableActivity extends Dialog implements OnClickListener 
 {

    CursorAdapter mAdapter;

    ListView countryListView;
    private EditText filterText = null;
    Context context;

    public SearchableActivity(Context context) 
    {
        super(context);
        setContentView(R.layout.countrysearch);

        String[] from;
        int[] to;           

            /*
             *dbConnector and cursor 
             *are close() before entering class
             */

            dbConnector.open();

        this.setTitle("Pick a Country");

        filterText = (EditText) findViewById(R.id.EditBox);
        filterText.addTextChangedListener(filterTextWatcher);
        filterText.requestFocus();

        countryListView = (ListView) findViewById(R.id.List);

        cursor = dbConnector.getAllCountries("simpletable");

        from = new String[] { "country" };
        to = new int[] { android.R.id.text1 };

        mAdapter = new SimpleCursorAdapter(context,
                android.R.layout.simple_list_item_1, cursor, from, to);

        countryListView.setAdapter(mAdapter);

        countryListView.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3)
            {
                cursor.close();
                dbConnector.close();

                //send the data out
                finishSearch(arg3);

            }

        });
    }


    @Override
    public void onStop(){
        filterText.removeTextChangedListener(filterTextWatcher);
        super.onStop();
    }

    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub

    }

    public void doMySearch(String s)
    {
            // this is a LIKE search on DB
        cursor = dbConnector.getSearch(s);
        mAdapter.changeCursor(cursor);

    }


    private TextWatcher filterTextWatcher = new TextWatcher()
    {

        public void afterTextChanged(Editable s)
        {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count)
        {
            doMySearch(s.toString());
        }
    };

}

!

+1

, 100 ? ?

AutoCompleteTextView Auto=new AutoCompleteTextView(this);
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,name);
Auto.setAdapter(arrAdapter);

?

0

Source: https://habr.com/ru/post/1771886/


All Articles