Tutorial Request ---> Putting XML to clickable List View

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Tutorial Request ---> Putting XML to clickable List View

Postby andyedward83 » Sun May 03, 2009 5:54 pm

Hi android experts,

I'm having trouble in grasping the concept on how to put data from parsed xml to a listview.

The current tutorial which is here http://www.anddev.org/parsing_xml_from_ ... -t353.html only shows how to make the data appear on a text view.

but how to put the parsed data to a list view? can anyone help??

maybe can extend the tutorial to be able to put into a list view??

I've tried using the custom ListAdapter but I don't know how to cast the parsed xml data to a data type that is acceptable by the custom ListAdapter.

I'm still very noobie of course, so excuse me on this noobish request of tutorial. If anyone can point me to a good article that really explains about this 'parsing xml data to a list view' thing, I would be very grateful too.

cheers,

Andy
andyedward83
Freshman
Freshman
 
Posts: 4
Joined: Sun May 03, 2009 5:00 pm

Top

Sorry for posting in the wrong section

Postby andyedward83 » Sun May 03, 2009 5:56 pm

I'm sorry for posting in the wrong section. I'll be really grateful if the moderators can put this subject to the correct category.

Thx,

Andy
andyedward83
Freshman
Freshman
 
Posts: 4
Joined: Sun May 03, 2009 5:00 pm

Postby arams » Wed May 20, 2009 6:23 am

Hello,
Try to use Hasttable or Vector to store the retriving collection of datas object....and then use Adapter................then set it to listview....
I hope this will help for u...
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby padde » Wed May 20, 2009 7:17 am

I would help if you could descripe what kind of data we are talking about..
with the current xml tut you parse the XML in an object.. what you do with this
object after that is up to you.

If you could give an example of a XML file you want to parse and descripe the data
you want to show in your list .. i could hack some stuff together for you


Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby blade » Wed May 20, 2009 9:45 am

I have the same issue ..
I parsed XML, by using same tutorial andyedward83 provided and created object, which I store into ArrayList.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayList<ParsedExampleDataSet> parsedDataSet;
  2.  
  3. The fields in ParsedExampleDataSet are and apropriate getters, are set:
  4.  
  5. - ID
  6.  
  7. - Title
  8.  
  9. - Body
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


The problem is how to display data into ListView. I tried SimpleAdapter, but cannot get it to work. So whats the basic idea when trying to display complex data (more than one in row) .. as far as i know Vector uses the same way of getting object's element as ArrayList of objects. I don't know how to use hasttable though.

This is clearly wrong code .. null should be some kind of Array, and parsedDataSet.get(0) selects only the first object and only then we can access the element. It's so simple with queries and cursors :).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ListAdapter notes = new SimpleAdapter(this,
  3.  
  4.         null, R.layout.list_all,
  5.  
  6.         new String[] { parsedDataSet.get(0).getID(),
  7.  
  8.         parsedDataSet.get(0).getTitle() },
  9.  
  10.         new int[] { R.id.id, R.id.title});
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Thx in advance.
blade
Freshman
Freshman
 
Posts: 4
Joined: Wed May 20, 2009 9:16 am

Postby arams » Wed May 20, 2009 1:20 pm

id=1
title=a
body=xxx
id=2
title=b
body=yyy
convert into string array(Whatever u need...)

like
array[0]=1+a+xxx;
array[1]=2+b+yyy;


and then
ListView lv=new Listview(this);
ArrayAdapter adap=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
lv.setAdapter(adap);



I hope this will help u....
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Top

Postby blade » Wed May 20, 2009 1:58 pm

Thx for your reply arams .. but you can't style individual elements in your solution .. for example, bigger title, smaller content.

Actually i figured it out .. u can populate adapter by using HashMap:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
  2.  
  3. HashMap<String,String> item = new HashMap<String,String>();
  4.  
  5.  
  6.  
  7. item.put("id", parsedDataSet.get(0).getD());
  8.  
  9. item.put("title", parsedPostsDataSet.get(0).getTitle());
  10.  
  11. item.put("body", parsedDataSet.get(0).getBody());
  12.  
  13. list.add(item);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

You can ad several items, or put several elements in an item ..

Then you use SimpleAdapter to display it:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SimpleAdapter notes = new SimpleAdapter(
  2.  
  3.         this,
  4.  
  5.         list,
  6.  
  7.         R.layout.list_all,
  8.  
  9.         new String[] {"id", "title", "body"},
  10.  
  11.         new int[] { R.id.id, R.id.text, R.id.body });
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thx for introducing HashTable/HashMap to me ;).
It's possible there is a better solution, but for the time being this works for me .. actually i could store data in hashMap in the first place .. hmmm.
blade
Freshman
Freshman
 
Posts: 4
Joined: Wed May 20, 2009 9:16 am

Postby arams » Thu May 21, 2009 7:56 am

I did not try this....


separating_listview_into_sections_using_headers-t2710.html


if it works fine then post the sample.....
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby nitemovz » Sun Dec 20, 2009 6:12 pm

Thanks for the help on putting the XML into a ListView! My whole application is based on ListViews. It is a reference type application. The only issue I am still having with this, is how to get the name/value of the list item that the user has clicked.

I need to then take this name/value and pull new information based on that. Any help that you all can provide would be wonderful! I am willing to give credits in my app info/credits page to anyone that posts to help me out.
nitemovz
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat Dec 19, 2009 6:44 pm

Postby Mazinkaiser » Sat Jan 02, 2010 11:35 am

hi all, i'm into this me too... if someone could make a new tutorial (just for an happy 2010 :D) could be great.
Mazinkaiser
Freshman
Freshman
 
Posts: 9
Joined: Fri Oct 16, 2009 12:21 pm

Postby xaire » Sat Jan 30, 2010 8:13 pm

I am trying to follow this to create the ListView that I want, but I am having some difficulty. Mainly, I understand the code and how it works, but I'm unsure as to where to put the code that is being discussed.

I'm used to programming in php, so switching to Java is confusing me a little.

What I am trying to do is connect to my server, run a php script, which outputs the data into the form of an XML, then have android display the data in a list. I have already done everything except for displaying the data in a list.

Also, after having clicked on an item in the ListView, is it possible to send another value? In other words, lets say my XML file looked like this.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <Employees>
  3.  
  4.    <Employee>
  5.  
  6.       <EmployeeID>12</EmployeeID>
  7.  
  8.       <EmployeeName>John Smith</EmployeeName>
  9.  
  10.    </Employee>
  11.  
  12.    <Employee>
  13.  
  14.       <EmployeeID>14</EmployeeID>
  15.  
  16.       <EmployeeName>Jane Doe</EmployeeName>
  17.  
  18.    </Employee>
  19.  
  20. </Employees>
  21.  
  22.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


I have followed plusminus' tutorial on parsing the XML, which then displays "Jane Doe" and not John Smith.

I'm having difficulty converting this to a ListView to only display the names. And, after having read many discussions on ListView...and am still unsure....can I have it send the EmployeeID back to the server, rather than the name (as the name could occur on more than one occasion).
xaire
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Jan 10, 2010 1:56 am
Location: Texas

Postby Buts » Mon May 03, 2010 3:42 pm

The problem I'm facing is quite similar.

I know how this works:
Code: Select all
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item = new HashMap<String,String>();

item.put("id", parsedDataSet.get(0).getD());
item.put("title", parsedPostsDataSet.get(0).getTitle());
item.put("body", parsedDataSet.get(0).getBody());
list.add(item);


But I want (for instance) "id" to be an image. So "id" is an ImageView and I have a URL to an image in my XML. I create a bitmap from that image and after that I want to show it in "id".
That doesn't work. I've tried changing <String,String> to <String,?>, but that didn't solve the problem either.
Buts
Freshman
Freshman
 
Posts: 2
Joined: Mon May 03, 2010 3:38 pm

Postby padde » Mon May 03, 2010 4:49 pm

Look at my code there -> http://www.anddev.org/png_image_--und-gt_sqlite_db_--und-gt_imageview-t7188-s15.html

It uses data backed-up by a database and therefore uses simplecursor adapter.. but the key is the same.
You have to implement your own ViewBinder to bind your images to a imageview.

Greets Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests