2013-04-11 74 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])); 

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

相關問題