Friday, October 19, 2012

AutoCompleteTextView with dynamic suggested words

The post "Example of AutoCompleteTextView" demonstrate AutoCompleteTextView with pre-defined suggested words. It's modified to have dynamic suggested words in this exercise.

AutoCompleteTextView with dynamic suggested words

myAutoCompleteAdapter employ a List<string> instead of String[]. Such that we can modify the contain dynamically. When user finish enter by clicking on OK button, it will check if the words is contained in the List<string>. If not, it will be added, and the adapter will be updated.

package com.example.androidautocompletetextview;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements TextWatcher{

 AutoCompleteTextView myAutoComplete;
 String item[]={
   "January", "February", "March", "April",
   "May", "June", "July", "August",
   "September", "October", "November", "December"
 };
 
 List<String> myList;
 ArrayAdapter<String> myAutoCompleteAdapter;
 
 Button buttonOK;
 TextView autoList;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);
        
        prepareMyList();
        myAutoComplete.addTextChangedListener(this);
        
        myAutoCompleteAdapter = new ArrayAdapter<String>(
          MainActivity.this, 
          android.R.layout.simple_dropdown_item_1line, 
          myList);
        
        myAutoComplete.setAdapter(myAutoCompleteAdapter);
        
        buttonOK = (Button)findViewById(R.id.ok);
        buttonOK.setOnClickListener(OkOnClickListener);
        
        autoList = (TextView)findViewById(R.id.autolist);
    }
    
    private void prepareMyList(){
     //prepare your list of words for AutoComplete
     myList = new ArrayList<String>();
     for(int i = 0; i < item.length; i++){
      myList.add(item[i]);
     }
    }
    
    OnClickListener OkOnClickListener
    = new OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   
   String newAdd = myAutoComplete.getText().toString();
   
   if(!myList.contains(newAdd)){
    myList.add(newAdd);
    
    // I don't know why simple notifyDataSetChanged() 
    // cannot update the autocomplete words
    //myAutoCompleteAdapter.notifyDataSetChanged();
    
    //update the autocomplete words
    myAutoCompleteAdapter = new ArrayAdapter<String>(
      MainActivity.this, 
            android.R.layout.simple_dropdown_item_1line, 
            myList);
          
          myAutoComplete.setAdapter(myAutoCompleteAdapter);
   }
   
   //display the words in myList for your reference
   String s = "";
   for(int i = 0; i < myList.size(); i++){
    s += myList.get(i) + "\n";
   }
   autoList.setText(s);
  }};

 @Override
 public void afterTextChanged(Editable arg0) {
  // TODO Auto-generated method stub
  
 }

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

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

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <AutoCompleteTextView 
        android:id="@+id/myautocomplete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:completionThreshold="1"/>
    <Button 
        android:id="@+id/ok"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="OK"/>
    <TextView
        android:id="@+id/autolist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>


download filesDownload the files.

1 comment: