Android XML parsing listview -
i using tutorial http://www.androidbegin.com/tutorial/android-xml-parse-images-and-texts-tutorial/ reaching xml parsing,if want change url 1 http://gis.taiwan.net.tw/xmlreleaseall_public/activity_c_f.xml ,how that? because xml format different.if there similar question of please inform me.thanks.
there tag in each listitem url 'infos' @ begin includes tags.
mainactivity.java
package eason.xml; import android.app.activity; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.widget.listview; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.hashmap; import java.util.map; public class mainactivity extends activity { // declare variables listview listview; listviewadapter adapter; progressdialog mprogressdialog; arraylist<hashmap<string, string>> arraylist; static string id = "id"; static string name = "name"; static string website = "website"; static string picture1 = "picture"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // view listview_main.xml setcontentview(r.layout.listview_main); // execute downloadjson asynctask new downloadxml().execute(); } // downloadjson asynctask private class downloadxml extends asynctask<void, void, void> { private void parserequestresultxml(inputstream stream) { arraylist=new arraylist<hashmap<string,string>>(); try { xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); xmlpullparser parser = factory.newpullparser(); parser.setinput(stream, null); hashmap<string, string> map = null; int eventtype = parser.geteventtype(); while (eventtype != xmlpullparser.end_document) { switch (parser.geteventtype()) { case xmlpullparser.start_document: //log.i("tag", " start document " + parser.getname()); break; case xmlpullparser.start_tag: //log.i("tag", " start tag " + parser.getname()); string tag=parser.getname(); if(tag.equals("info")){ //read values info tag log.i("tag","reading info"); id=parser.getattributevalue(null, "id"); name=parser.getattributevalue(null, "name"); website=parser.getattributevalue(null, "website"); picture1=parser.getattributevalue(null,"picture1"); //do same other values map = new hashmap<string, string>(); map.put("id", id); map.put("name", name); map.put("website", website); arraylist.add(map); } break; case xmlpullparser.end_tag: //log.i("tag", " end tag " + parser.getname()); break; default: break; } eventtype=parser.next(); //get next event type } //reading values list (map<string,string> e : arraylist) { log.d("tag"," row id "+e.get("id")+" name "+e.get("name")+" website "+e.get("website")+" picture1 "+e.get("picture1")); } } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e1) { e1.printstacktrace(); } } @override protected void onpreexecute() { super.onpreexecute(); // create progressdialog mprogressdialog = new progressdialog(mainactivity.this); // set progressdialog title mprogressdialog.settitle("android xml parse tutorial"); // set progressdialog message mprogressdialog.setmessage("loading..."); mprogressdialog.setindeterminate(false); // show progressdialog mprogressdialog.show(); } @override protected void doinbackground(void... params) { // create array arraylist = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); // retrieve nodes given url address inputstream stream = parser.getinputstreamfromurl("http://gis.taiwan.net.tw/xmlreleaseall_public/activity_c_f.xml"); if (stream != null) { try {parserequestresultxml(stream); stream.close();}catch(ioexception e1) { e1.printstacktrace(); } }return null; } @override protected void onpostexecute(void args) { // locate listview in listview_main.xml listview = (listview) findviewbyid(r.id.listview); // pass results listviewadapter.java adapter = new listviewadapter(mainactivity.this, arraylist); // binds adapter listview listview.setadapter(adapter); // close progressdialog mprogressdialog.dismiss(); } }}
listviewadapter
package eason.xml; import android.content.context; import android.content.intent; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import java.util.arraylist; import java.util.hashmap; public class listviewadapter extends baseadapter { // declare variables context context; layoutinflater inflater; arraylist<hashmap<string, string>> data; imageloader imageloader; hashmap<string, string> resultp = new hashmap<string, string>(); public listviewadapter(context context, arraylist<hashmap<string, string>> arraylist) { this.context = context; data = arraylist; imageloader = new imageloader(context); } @override public int getcount() { return data.size(); } @override public object getitem(int position) { return null; } @override public long getitemid(int position) { return 0; } public view getview(final int position, view convertview, viewgroup parent) { // declare variables textview name; textview start; textview website; imageview picture1; inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.listview_item, parent, false); // position resultp = data.get(position); // locate textviews in listview_item.xml name = (textview) itemview.findviewbyid(r.id.rank); start = (textview) itemview.findviewbyid(r.id.country); website = (textview) itemview.findviewbyid(r.id.population); // locate imageview in listview_item.xml picture1 = (imageview) itemview.findviewbyid(r.id.flag); // capture position , set results textviews name.settext(resultp.get(mainactivity.id)); start.settext(resultp.get(mainactivity.name)); website.settext(resultp.get(mainactivity.website)); // capture position , set results imageview // passes flag images url imageloader.class imageloader.displayimage(resultp.get(mainactivity.picture1), picture1); // capture listview item click itemview.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // position resultp = data.get(position); intent intent = new intent(context, singleitemview.class); // pass data rank intent.putextra("id", resultp.get(mainactivity.id)); // pass data country intent.putextra("name", resultp.get(mainactivity.name)); // pass data population intent.putextra("website", resultp.get(mainactivity.website)); // pass data flag intent.putextra("picture1", resultp.get(mainactivity.picture1)); // start singleitemview class context.startactivity(intent); } }); return itemview; } }
xml parser
package eason.xml; import android.util.log; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.w3c.dom.document; import org.w3c.dom.node; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import java.io.ioexception; import java.io.inputstream; import java.io.stringreader; import java.io.unsupportedencodingexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; public class xmlparser { public xmlparser() { } // retrive xml url public string getxmlfromurl(string url) { string xml = null; try { defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return xml; } // retrive dom element public document getdomelement(string xml) { document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } // retrive node element public final string getelementvalue(node elem) { node child; if (elem != null) { if (elem.haschildnodes()) { (child = elem.getfirstchild(); child != null; child = child .getnextsibling()) { if (child.getnodetype() == node.text_node) { return child.getnodevalue(); } } } } return ""; } // retrive node value public inputstream getinputstreamfromurl(string url) { string xml = null; try { defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); return httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } }
change url in getxmlfromurl()
, change keys in parser.getvalue(e, **rank**)
. whatever format is, need specify keys data , save accordingly.
Comments
Post a Comment