Saturday, 11 August 2012

Android AutoCompleteTextView data binding based on another AutoCompleteTextView selection

Android - Bind data to an AutoCompleteTextView based on another AutoCompleteTextView selection

Hi friends, here I am going to explain how to load an AutoCompleteTextView based on the selection of another AutoCompleteTextView in Android.

Create screen layout xml file

Add following in layout xml file as station_input.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ station_input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp" >

        <AutoCompleteTextView
            android:id="@+id/fromStation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:imeOptions="actionNext"
            android:inputType="text"
            android:singleLine="true"
            android:text="@string/From" >
            <requestFocus />
        </AutoCompleteTextView>

         <AutoCompleteTextView
             android:id="@+id/toStation"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:ems="10"
             android:imeOptions="actionDone"
            android:inputType="text"
            android:singleLine="true"
             android:text="@string/To" >
            <requestFocus />
        </AutoCompleteTextView>
    </LinearLayout>

Add list_station.xml to the project layout folder

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="14sp"
    android:textColor="#000">
</TextView>

Bind data to first AutoCompleteTextView

 // Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id. station_input);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View layout = layoutInflater.inflate(R.layout. station_input, viewGroup);

    
    // bind all stations to station auto complete
  AutoCompleteTextView fromStation = (AutoCompleteTextView) layout.findViewById(R.id.fromStation) ;
                   // Helper class for getting Stations list from Databse. 
  DatabaseHelper helper = new  DatabaseHelper();
  List<String> stationsList =helper.GetStations(context); 
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.list_station, stationsList);
  fromStation.setAdapter(adapter);
  fromStation.setText("");

  
  
  
  toStation = (AutoCompleteTextView) layout.findViewById(R.id.toStation) ;
  toStation.setText("");

setOnItemClickListener for first AutoCompleteTextView

//  add item click listener for fromStation AutocompleteTextView 
  fromStation.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3){
                                // get the clicked item in fromStation
String fromStationSelected = arg0.getItemAtPosition(arg2).toString().split("-")[1];

                                 // Helper class for getting Stations list from Databse.
DatabaseHelper  helper = new  DatabaseHelper();
  List<String> stationsList = helper.GetToStation(context,  fromStationSelected ); 
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.list_station, stationsList);
  toStation.setAdapter(adapter);
  toStation.setText("");
 
}
});
    
  toStation.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3){
  
  // to do what ever you want in item click of second AutoCompleteTextView (toStation)
          
}
});



If any better way, please feel free to comment.

Enjoy Coding. 

No comments:

Post a Comment