Java 字符串数组:显示结果
我想我可以接受从两个Java 字符串数组:显示结果,java,android,arrays,google-places-api,Java,Android,Arrays,Google Places Api,我想我可以接受从两个AutoCompleteTextView获取的用户输入,并将其显示为一个TableLayout的结果 AutoCompleteTextView接受的两个输入是来自用户的My Location和Destination。复杂性来自于AutoCompleteTextView生成的输入类型。AutoCompleteTextView使用谷歌的placesautomplete,因此我正在使用类似尼泊尔中部地区贾马尔(Jamal)、加德满都(Kathmandu)以及尼泊尔中部地区卡兰基(K
AutoCompleteTextView
获取的用户输入,并将其显示为一个TableLayout
的结果
AutoCompleteTextView
接受的两个输入是来自用户的My Location和Destination。复杂性来自于AutoCompleteTextView
生成的输入类型。AutoCompleteTextView
使用谷歌的placesautomplete
,因此我正在使用类似尼泊尔中部地区贾马尔(Jamal)、加德满都(Kathmandu)以及尼泊尔中部地区卡兰基(Kalanki)、加德满都(Kathmandu)的输入。我希望TableLayout
反映的是结果:来自。。。到…
我的问题是如何在尼泊尔中部地区加德满都的贾马尔(Jamal)输入字符串的第一部分,即贾马尔(Jamal),以便表格布局
显示结果,如结果:从贾马尔到卡兰基。作为android的新手,我有一个模糊的想法,我尝试的代码是这样的
String[] from;
String[] to;
//pulling the auto complete text view
from = location.getString().toText();
to = destination.getString().toText();
//referencing the text view inside the table layout
results.setText(new StringBuilder().append("Results from"+from[0]).append(" to"+to[0]));
此代码显然不起作用,因为它提示我将
从
更改为字符串
。我真的不知道发生了什么事。请提供帮助?您正在将字符串分配给字符串[]。那是行不通的
from = location.getString().toText();
将它们分配给字符串
,然后执行此操作
results.setText(new StringBuilder().append("Results from"+from).append(" to"+to));
这样做:
String[] from = new String[]{location.getString().toText()};
String[] to = new String[]{destination.getString().toText()};
results.setText(new StringBuilder().append("Results from"+from[0]).append(" to"+to[0]));
或
希望得到帮助。您不能将
字符串分配给字符串[]
(字符串和字符串数组不一样!)
将它们用作字符串:
String from = location.getString().toText();
results.setText(new StringBuilder().append("Results from"+from).append(" to"+to));
我想我找到了以我想要的方式显示结果的答案 我不确定这些数组声明的作用,但它们不会编译。它确实有效,但我得到了地址的完整部分。比如,如果输入是尼泊尔中部地区加德满都卡兰基Kalanki
,我只想在结果显示中获得Kalanki
。。。用代码。。。我得到了完整的地址我正在使用字符串数组,这样我就可以修改用户输入。。。比如,如果他们要输入一个地址,比如尼泊尔中部地区加德满都卡兰基
,或者可能是加德满都卡兰基
,我只想让他们看到第一个单词,在本例中是卡兰基
但是location.getString().toText();返回字符串,而不是字符串数组。您可以创建一个新的字符串,并根据需要进行修改,然后将其显示给用户。hmmm。。。我不明白你。。。很抱歉如果你能给我举个例子??
String from = location.getString().toText();
results.setText(new StringBuilder().append("Results from"+from).append(" to"+to));
String from_split = location.getText().toString();
String to_split = destination.getText().toString();
if(from_split.contains(","))
{
from = from_split.split(",");
}
else
{
from = from_split.split(" ");
}
if(to_split.contains(","))
{
to = to_split.split(",");
}
else
{
to = to_split.split(" ");
}
results.setText(new StringBuffer().append(from[0]).append(" to "+to[0]));