Sax-based XML parsing with Source UI Class (II)

Source: Internet
Author: User

This section is part of the UI, incidentally, by simply using the Thread+handler

Layout class: Only two XML files

Activity_main: main layout, only one ListView is not given.

Item.xml uses a relative layout to display the items in the ListView.

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/         Android "Android:layout_width=" Match_parent "android:layout_height=" 20DP "android:gravity=" Center "> <textview android:id= "@+id/name_tv" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:layout_alignbottom=" @+id/id_tv "android:layout_marginleft=" 60DP "android:layout _torightof= "@+id/id_tv" android:text= "TextView"/> <textview android:id= "@+id/age_tv" Androi D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbaseline= "@+id/name_t V "android:layout_alignbottom=" @+id/name_tv "android:layout_alignparentright=" true "Android:layout_ma rginright= "19DP" android:text= "TextView"/> <textview android:id= "@+id/id_tv" android:layout _width= "Wrap_content" Androidoid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" android:layout_margintop= "76DP" android:text= "TextView"/></relativelayout>
Main class

public class Mainactivity extends Activity{private ListView listview=null;private static final String tag= "Parserxml"; private static final int msg_success = 1;//Request succeeded//private static final int msg_failure = 0;//Get failed identity private Handler Mhandl ER = new Handler () {public void Handlemessage (Message msg) {//This method runs on the UI thread switch (msg.what) {case msg_success://gets the data passed over Li st<person> list = (list<person>) msg.obj;//print for (person person:list) {log.d (TAG, person.tostring ());} Myadapter adapter=new myadapter (list); adapter.notifydatasetchanged (); Listview.setadapter (adapter); Break;default: break;}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (    R.layout.activity_main); listview= (ListView) This.findviewbyid (r.id.listview1);d oparserxml ();}        public class Myadapter extends Baseadapter {private list<person>list;         Pass a List in public myadapter (list<person>list) {this.list=list;} Return number @overridepublic int GetCount () {return list.size ();}        Gets the data at position @overridepublic Object getItem (int position) {return list.get (position);}         Returns the item id@overridepublic long getitemid (int position) {return position;} Return view@overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Layoutinflater inflater= Layoutinflater.from (Mainactivity.this); Viewholder viewholder=null;if (convertview==null) {//is similar to the reuse mechanism of iOS and optimizes the loading of XML viewholder=new Viewholder (); Convertview=inflater.inflate (R.layout.item,null); viewholder.id_tv= (TextView) Convertview.findviewbyid (R.id.id_ TV); viewholder.name_tv= (TextView) Convertview.findviewbyid (R.ID.NAME_TV); viewholder.age_tv= (TextView) Convertview.findviewbyid (R.ID.AGE_TV);//Use tag to identify Convertview.settag (Viewholder);} else {//use tag to remove viewholder= (Viewholder) Convertview.gettag ();} Person Person=list.get (position); ViewHolder.id_tv.setText (Person.getid () + ""); ViewHolder.age_tv.setText ( Person.getage () + ""); ViewHolder.name_tv.setText (Person.getnaMe () + ""); return Convertview;} }protected void Doparserxml () {New Thread (new Runnable () {@Overridepublic void run () {LOG.D (TAG, "Start parsing xml"); InputStream Inputstream=httputils.getxml (); List<person>list=parserxmlutils.xmlparser (InputStream, "person");//Use Obtainmessage () to get a message messages =mhandler.obtainmessage (); message.what=msg_success;message.obj=list;mhandler.sendmessage (message);}}).    Start ();}    public class Viewholder {public TextView id_tv;    Public TextView Name_tv;    Public TextView Age_tv; } @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is P Resent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
Process---"

1. Data processing is performed first:

Doparserxml ()
2. Passing the returned data to handler

3. Handler in the ListView binding adapter.

The use of Ps:adapter, Baseadapter and iOS UITableView is simply the likeness. (The meaning of the delegate method and the interface of the adapter is basically a meaning)

The list is also optimized for TableView: cell reuse.

The source code is as follows:

XML parsing of Andriod





Sax-based XML parsing with Source UI Class (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.