2013-04-11 58 views
0

我在想,我可以从两个AutoCompleteTextView中获取用户输入,并将其显示为TableLayout字符串数组:显示结果

两个输入该AutoCompleteTextView需要的是我的位置目标从用户。复杂性来自AutoCompleteTextView生成的输入类型。 AutoCompleteTextView使用Google的PlacesAutoComplete,因此我正在使用像Jamal,加德满都,中部地区,尼泊尔和也许Kalanki,加德满都,中部地区,尼泊尔。我想在TableLayout反映是结果:从...到...

我的问题是如何才能采取字符串的第一部分中贾马尔,加德满都,中部地区,尼泊尔这只是JamalTableLayout显示结果如结果:从Jamal到Kalanki。对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])); 

此代码明显,因为它促使我​​改变fromString不起作用。我真的不知道发生了什么事。请帮忙 ?

回答

1

这样做:

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; 
String to; 
from = location.getString().toText(); 
to = destination.getString().toText(); 
results.setText(new StringBuilder().append("Results from"+from).append(" to"+to)); 

希望它的帮助。

+1

不知道这些数组声明是做什么的,但它们不会编译。 – Keppil 2013-04-11 06:50:31

+0

谢谢,我编辑了我的答案 – jimpanzer 2013-04-11 06:51:55

+0

@Keppil,感谢您指出了错误。 – jimpanzer 2013-04-11 06:58:22

0

您正在为字符串[]分配一个字符串。这是行不通的。

from = location.getString().toText(); 

将它们分配到String并执行此操作。

results.setText(new StringBuilder().append("Results from"+from).append(" to"+to)); 
0

您不能分配StringString[]字符串和字符串数组是不一样的!)

使用它们作为一个字符串:

String from = location.getString().toText(); 

results.setText(new StringBuilder().append("Results from"+from).append(" to"+to)); 
+0

我正在使用String数组我可以修改用户输入...就像他们要输入像Kalanki,加德满都,中部地区,尼泊尔或者Kalanki Kathmandu一样的地址,我只想向他们展示第一个单词,在这个例子中是'Kalanki' – 2013-04-11 07:56:39

+0

但是location.getString ()。到文本();返回一个String,而不是String数组。您可以根据需要创建一个新的字符串并将其显示给用户。 – BobTheBuilder 2013-04-11 07:59:05

+0

嗯...我没有得到你...对不起...如果你能给我一个例子? – 2013-04-11 08:04:05

0
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])); 

我想我找到了答案,以我想要的方式显示结果。