8
public class FareActivity extends Activity {


int fareid;
String Source;
String Dest;
AutoCompleteTextView source;
AutoCompleteTextView dest;


static final String[] SOURCE = new String[] {
      "Delhi", "Mumbai", "Agra", "Jaipur};


static final String[] DEST = new String[] {
      "Delhi", "Mumbai", "Agra", "Jaipur};




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fare);




    dest = (AutoCompleteTextView) findViewById(R.id.acdest);
    ArrayAdapter<String> dadapter = new ArrayAdapter<String>(this, R.layout.list_item, DEST);
    dest.setAdapter(dadapter);



source = (AutoCompleteTextView) findViewById(R.id.acsource);
ArrayAdapter<String> sadapter = new ArrayAdapter<String>(this, R.layout.list_item, SOURCE);
    dest.setAdapter(sadapter);




 // Fare id calculation

     if(Source=="Delhi" && Dest=="Jaipur")
     {
         fareid=1;
     }
     else  if(Source=="Delhi" && Dest=="Agra")
     {
         fareid=2;
     }
     else  if(Source=="Delhi" && Dest=="Mumbai")
     {
         fareid=3;
     }


}

I just want to store autocompletetextview 'source' and autocompletetextview 'dest' values to String variable 'Source' and String Variable 'Dest'. I will use both string variables for further processing in my project, so please help me out.

josh3736
  • 139,160
  • 33
  • 216
  • 263
  • Do you want to get text of autocomlete view? - by using `String myDest = dest.getText().toString()` ? – pleerock Mar 24 '12 at 19:43
  • http://stackoverflow.com/questions/4819813/how-to-get-text-from-autocomplete-textview-android – AliSh Mar 25 '12 at 10:44

2 Answers2

18

Just use the AutoCompleteTextView method getText() and call toString() on it.

// Fare id calculation
Source = source.getText().toString();
Dest = dest.getText().toString();

if (Source.equals("Delhi") && Dest.equals("Jaipur")) {
    fareid=1;
}
else if (Source.equals("Delhi") && Dest.equals("Agra")) {
    fareid=2;
}
else if (Source.equals("Delhi") && Dest.equals("Mumbai")) {
    fareid=3;
}

You should keep in mind that users can enter everything they want into your AutoCompleteTextView. If you want to perform an action when the user chooses one of the suggested items, add an OnItemSelectedListener with dest.setOnItemSelectedListener().

There is also an error in your code you call dest.setAdapter(sadapter) instead of source.setAdapter(sadapter).

Gabriel Ittner
  • 1,162
  • 9
  • 22
4
AutoCompleteTextView source = (AutoCompleteTextView) findViewById(R.id.acsource);
String Source = source.getText().toString();
Draken
  • 3,134
  • 13
  • 34
  • 54
Amrit Bains
  • 111
  • 1
  • 3