如何从AutoCompleteTextView得到字符串文本? [英] How to get string text from AutoCompleteTextView?

查看:99
本文介绍了如何从AutoCompleteTextView得到字符串文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


}

我只是想存储autocompletetextview源和autocompletetextviewDEST值字符串变量源和字符串变量'目的地'。我将使用进行进一步的处理都字符串变量在我的项目,所以请帮助我。

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.

推荐答案

只需使用AutoCompleteTextView方法的getText()并调用的toString()就可以了。

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

您应该记住,用户可以输入他们想要的一切到您的AutoCompleteTextView。如果您想在用户选择建议的项目之一执行的操作,添加 OnItemSelectedListener dest.setOnItemSelectedListener()

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().

还有一个在你的code你叫 dest.setAdapter(sadapter)而不是 source.setAdapter(sadapter)错误

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

这篇关于如何从AutoCompleteTextView得到字符串文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆