Android Action Bar SearchView as Autocomplete?

前端 未结 7 1370
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 04:00

I am using a SearchView in the Action Bar. I want to use autocomplete feature on the search view to get results from the database.

Is this possible? Or do I need to

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 04:29

    I have use custom AutoCompleteTextView and add it in ActionBar.

    enter image description here

    public class MainActivity extends Activity {
    
        private static final String[] COUNTRIES = new String[] { "Belgium",
                "France", "France_", "Italy", "Germany", "Spain" };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
            // actionBar.setDisplayShowTitleEnabled(false);
            // actionBar.setIcon(R.drawable.ic_action_search);
    
            LayoutInflater inflator = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.actionbar, null);
    
            actionBar.setCustomView(v);
    
            ArrayAdapter adapter = new ArrayAdapter(this,
                    android.R.layout.simple_dropdown_item_1line, COUNTRIES);
            AutoCompleteTextView textView = (AutoCompleteTextView) v
                    .findViewById(R.id.editText1);
            textView.setAdapter(adapter);
    
        }
    
    }
    

    and Your Layout:

    
    
        
    
        
    
            
        
    
    
    

    Edited:

    Please check this and this link it may help you. code is here.

提交回复
热议问题