here is what I'm trying to do. I have a list of stock symbols located in the string.xml file in an android project. The list looks something like this...
ACE - ACE Limited ABT - Abbott Laboratories ANF - Abercrombie and Fitch Company etc...etc.
I have this list set up in the android Main as an AutoComplete array. The problem is that when the user selects one of the dropdown stocks, the box fills in the STOCK SYMBOL + the COMPANY NAME. I need to "trim" off the "company name" when the user selects it so only the stock "sym开发者_Go百科bol" appears in the box. Is there a simple function or command to do this? I get confused trying to convert the array back to a string and then back again. Any help would be appreciated!
Try separating them within your XML file. For example, instead of something like
<company>ACE - ACE Limited</company>
Use
<company><symbol>ACE</symbol><name>ACE Limited</company>
or
<company symbol="ACE" name="ACE Limited" />
Then you can read the individual properties or sub-tags with your reading engine. For a simple tutorial on this, check out this link.
EDIT: If this would require too much work, you could try simply splitting the strings (assuming they all have a common delimiter of -
.
String symbol;
String name;
String xmlData = "ACE - ACE Limited";
String[] splitData = xmlData.split(" - ");
symbol = splitData[0];
// Set the name to the remaining items
for (int i=1; i<splitData.length; i++) {
name += splitData[i] + " - ";
}
This will set the symbol to the first part of xmlData
(or the whole string if " - "
is not found) and the name to the rest of it, including all occurrences of " - "
.
(Of course, you'll only want to do either of these once the user selects the item. I'm assuming your question is about the parsing of the String rather than the click event.)
精彩评论