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'source'和autocompletetextview'dest'值存储到String变量'Source'和String Variable'Dest'。我将在我的项目中使用两个字符串变量进行进一步处理,所以请帮助我。
答案 0 :(得分:16)
只需使用AutoCompleteTextView方法getText()
并在其上调用toString()
即可。
// 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()
。
您拨打dest.setAdapter(sadapter)
而不是source.setAdapter(sadapter)
的代码中也有错误。
答案 1 :(得分:3)
AutoCompleteTextView source = (AutoCompleteTextView) findViewById(R.id.acsource);
String Source = source.getText().toString();