Reading xml and add item to listview

Put your problem here if it does not fit any of the other categories.

Reading xml and add item to listview

Postby moimael » Wed Oct 01, 2008 12:39 pm

Hi all !

I'm a beginner in Java and android platform(but i programm in vb .net) and i need your help to read an web xml file and extract each item to add them in a listview. I have created my listview and i fill it with an array of string (String[]).
I want to add this items to my listview : http://yp.shoutcast.com/sbin/newxml.phtml

Thanks

(scuse for my poor english, i'm french :p)
moimael
Freshman
Freshman
 
Posts: 3
Joined: Wed Oct 01, 2008 12:30 pm

Top

Postby moimael » Sat Oct 04, 2008 12:26 pm

nobody can help me ?
moimael
Freshman
Freshman
 
Posts: 3
Joined: Wed Oct 01, 2008 12:30 pm

Postby plusminus » Sat Oct 04, 2008 1:43 pm

Hello moimael,

sorry, of course we can help you :)

Simply declare your data like this :darrow: and save it to the "[font=Courier New].../res/raw/[/font]" folder of your project (filename should end with "[font=Courier New].xml[/font]").
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.         <array name="aboutcreditsnames">
  6.  
  7.                 <item>Nicolas Gramlich</item>
  8.  
  9.                 <item>The whole anddev.org community =)</item>
  10.  
  11.                 <item>Awesome Tango-Icons</item>
  12.  
  13.                 <item>Joy and Tom Gibara (British Voice)</item>
  14.  
  15.                 <item>Flag-Icons (icondrawer.com)</item>
  16.  
  17.         </array>
  18.  
  19. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Having done that, you load it as a String array to your javacode using the following snippet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.getResources().getStringArray(R.array.aboutcreditsnames);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby moimael » Sun Oct 05, 2008 10:12 am

First thanks for your answer, but i want to read the xml file from the web, in real time. That's why i cannot add them to my local ressource.

I want to add those items to my array : http://yp.shoutcast.com/sbin/newxml.phtml

Thanks ;)
moimael
Freshman
Freshman
 
Posts: 3
Joined: Wed Oct 01, 2008 12:30 pm

Postby coding_android » Sun Oct 05, 2008 10:36 am

You can use the SAX Parser in order to parse your XML file. An example for using the SAX parser in Android can be found here (this example is based on the old M3 SDK but it works with 0.9 and 1.0 as well):

http://www.helloandroid.com/node/110?page=0%2C2

This tutorial is covering all your needs:

http://www.anddev.org/parsing_xml_from_ ... -t353.html
If you got job opportunities for me. Just drop me a personal message. - Android developement - integrating social media into Android
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Postby Steph » Tue Nov 03, 2009 9:03 pm

Hi !
I want to get the string in my xml and create button with this text. But my code (below) doesn't work. I don't know why? there is an exception : String array resource id #0x7F0400000 android.content.resources$NotFoundException: String array

Morever, I have used this method to store my data. But i 'd like to know how save more data in this raw/files.

Thanks

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String[] myShowName = this.getResources().getStringArray(R.raw.seriesname);
  3.  
  4.                
  5.  
  6.                 Log.i("US Finder", "serie "+myShowName[0]);
  7.  
  8.                
  9.  
  10.                 //for each element in xml create button
  11.  
  12.                 for (int i=0; i<myShowName.length; i++)
  13.  
  14.                 {
  15.  
  16.                         Log.i("US Finder", "serie "+myShowName[i]);
  17.  
  18.                         _btn[i]= new Button(this);
  19.  
  20.                         _btn[i].setText(myShowName[i]);
  21.  
  22.                         _btn[i].setGravity(Gravity.CENTER_HORIZONTAL);
  23.  
  24.                         _btn[i].setOnClickListener(this);
  25.  
  26.                         myLayout.addView(_btn[i]);
  27.  
  28.                 }
  29.  
  30.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Postby android2 » Thu Nov 12, 2009 2:12 pm

this code make procude error in the listactivity
plusminus wrote:Hello moimael,

sorry, of course we can help you :)

Simply declare your data like this :darrow: and save it to the "[font=Courier New].../res/raw/[/font]" folder of your project (filename should end with "[font=Courier New].xml[/font]").
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.         <array name="aboutcreditsnames">
  4.                 <item>Nicolas Gramlich</item>
  5.                 <item>The whole anddev.org community =)</item>
  6.                 <item>Awesome Tango-Icons</item>
  7.                 <item>Joy and Tom Gibara (British Voice)</item>
  8.                 <item>Flag-Icons (icondrawer.com)</item>
  9.         </array>
  10. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Having done that, you load it as a String array to your javacode using the following snippet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.getResources().getStringArray(R.array.aboutcreditsnames);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Best Regards,
plusminus
android2
Developer
Developer
 
Posts: 48
Joined: Sun Oct 04, 2009 8:34 pm

Re: Reading xml and add item to listview

Postby arif-at-anddev » Fri Jul 02, 2010 3:31 am

nice post, but can we add an id for each item? because when we use the string array for drawing
a menu for instance, we need text or title and id for each menu item.

thx
arif-at-anddev
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jun 21, 2010 7:21 am

Re: Reading xml and add item to listview

Postby blundell » Fri Jul 02, 2010 8:59 am

You've just dragged up a post from 2008 :-p
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: Reading xml and add item to listview

Postby anarche » Sat Jul 03, 2010 1:11 am

and really arif, you can add ids whereever you want in your code.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 20 guests