I have an AutoCompleteTextView in which I have to show suggestions from data in an XML file. Is it possible to do so, or will I have to convert this XML into JSON format?
Structure of my XML file-
<?xml version="1.0" encoding="utf-8"?>
<elements>
<elem>
<name>Name 1</name>
<code>2001</code>
<dat>1001</dat>
</elem>
<elem>
<name>Name 2</name>
<code>2002</code>
<dat>1002</dat>
</elem>
.
.
.
<elem>
<name>Name 3</name>
<code>2003</code>
<dat>1003</dat>
</elem>
</elements>
The program has to look for matches in all three fields- name, code and dat.
AutoCompleteTextView needs an adapter that will supply the data and the views. You should parse the XML file (by using an XML parser like SAXParser, etc) and convert it into an ArrayList of objects and then use the ArrayList as the backing data for the adapter.
XML or JSON depends on whether or not the data will be modified. XML is good if the data will later be modified by the user. JSON is good for keeping snapshot data that will not change.
Related
Want to populate the spinner from the online XML data which i am getting from link for example-http://example.com/abc.svc/APIGetalllocationAgentID=1234&KeyCode=123456789012345677544
From the above link assume I am getting the below pattern of XML
<a:Cityvalues>
<a:FromCityId>123</a:FromCityId>
<a:FromCityCameIdName>Cityname</a:FromCityCameIdName>
<a:ToCityId>321</a:ToCityId>
<a:ToCityNameidName>Cityname</a:ToCityNameidName>
</a:CityValues>
From the above XML value format which i am getting from the link..want to fill the spinner of android of tag <a:FromCityCameIdName>Cityname</a:FromCityCameIdName> i.e the spinner should display Cityname
Before set it in spinner you require to parse it. Following is the url which help you how to parse xml data ?
http://www.tutorialspoint.com/android/android_xml_parsers.htm
https://developer.android.com/training/basics/network-ops/xml.html
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
I want to make something that searches through an XML file, to see if a string, entered by the user, exists. My XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="dict_nl_array" ><item>ADHD</item><item>ADSL</item><item>AMvB</item><item>AOV</item><item>AOW</item><item>uniteit</item></string-array></resources>
The only difference is that my XML is about 500 times as long as this.
I tried loading the arraylist (xml) directly, but then I get this error:
Failed adding to JNI local ref table (has 512 entries)
Which seems logical, because its a gigantic list of items.
So, I was thinking, can I search through that XML file, without having to load it completely?
Ofcourse, other suggestions to do this are welcome! (If possible, with an example, I'd greatly appreciate that!)
Late answer, but it might help anyone else with this problem!
Based on this question, you can avoid this error by encoding the resource as a string, rather than a string-array.
<string name="array">ADHD#ADSL#AOV#AOW</string>
The # character separates the entries. To speed up the reformatting, just Find/Replace </item><item> with #. Then you can parse the string into an array like so:
myStringArray = getResources().getString(R.id.array).split("#");
Hope this helps!
I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?
Strings.xml isn't the right place. You COULD define the URLs, one at a time, in strings.xml, like this:
<string name="url1">http://foo1.com</string>
<string name="url2">http://foo2.com</string>
// etc
And then create an array at runtime, dumping all these values in one-by-one. But that would suck.
Instead, create an xml file in res/values/, name it whatever you want (for instance, urlvals.xml), and populate it like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="urls">
<item>http://foo1.com</item>
<item>http://foo2.com</item>
</array>
</resources>
Then, in code, reference it in the following way:
String[] myUrls = getResources().getStringArray(R.array.urls);
If you want an array of string resources, you can either:
1) add the strings in strings.xml (as per #Alexander's answer), and reference them by name in an array in values/yourresourcename.xml as per here.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myLinks">
<item>#string/link1</item>
<item>#string/link2</item>
</array>
2) add them directly the string array (verbatim) like #Alexander suggests
How you associate them with the elements in the ListView is up to you. I would suggest (if the content is static) creating a matching string array for (e.g.) the string you want to actually display (as opposed to the link itself).
I'm new to Android Development, I have to parse my XML files through Http request, i'm using this example for parsing
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
which works fine for me in all the the XML file through HTTPRequest except the one which having CADATA like:-
<title> <![CDATA[ Timer for work ]]> </title>
When i'm trying to parse this file which having this type of data i'm not getting any thing on the ListView.
This file having 10 new after parsing it shows only 10 cells but not showing any data but when i parse other XML file which not having this kind of CADATA type data that works fine showing text .
Can any one plz suggest me how to rectify this problem of CADATA in parsing XML.
Thanks In Advance
You probably need to know that LexicalHandlers exist. You get callbacks of startCDATA() endCDATA()
in my app i am sending when i click a button it goes on to a url and it returns back an xml file. The xml file is as follows
<search>
<data>
<userid>1</userid>
<name>Jean</name>
<address>dbvsvksn</address>
<dob>Mar 3</dob>
<country>us</country>
</data>
<data>
<userid>2</userid>
<name>Anne</name>
<address>dbvsvksn ,</address>
<dob>Jun 2<dob>
<country>us</country>
</data>
<data>
<userid>3</userid>
<name>J</name>
<address>dbvsvksn ,</address>
<dob>Dec 6<dob>
<country>us</country>
</data>
</search>
From this xml file i am listing out the name's in a list view in an activity. In the list view when a name is clicked it moves over to a new activity where i want to display the correct address, country and dob.
I am able to get the xml file and list out the names in a list view, but i don't know how to display the exact tag related to the name selected , please help me. How to do this....
You say you can get the xml file and list out the names, so you apparently are parsing the xml. Are you using one of the XML parser classes (DOM or SAX)? If so, presumably you are building some sort of data structure based on the parsing (probably an array, judging from the XML fragment you posted). Use the index of the selected name to index into the array and retrieve the rest of the data you need.
Perhaps if you posted some code showing what you've tried, we can provide more specific help.
you can use textview. The number of userid's is there same number of textview.One for each userid,name,address,dob & country. Just add these in tablerow & just check which row is clicked just pass all information of that click in intent & you are able to display all info.