View the auction item program interface:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: gravity = "center" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <linearlayoutandroid: Orientation = "horizontal" Android: gravity = "center" Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: layout_margin = "@ dimen/sub_title_margin"> <textviewandroi D: Id = "@ + ID/view_titile" Android: text = "@ string/view_succ" Android: textsize = "@ dimen/label_font_size" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <! -- Define the return button --> <buttonandroid: Id = "@ + ID/bn_home" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_marginleft = "@ dimen/label_font_size" Android: Background = "@ drawable/home"/> </linearlayout> <! -- View the listview of the item list --> <listviewandroid: Id = "@ + ID/succlist" Android: layout_width = "match_parent" Android: layout_height = "match_parent"/> </linearlayout>
View the fragment code of a streaming object:
Public class viewitemfragment extends fragment {button bnhome; listview succlist; textview viewtitle; @ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {view rootview = Inflater. inflate (R. layout. view_item, container, false); // obtain the return button bnhome = (button) rootview on the interface. findviewbyid (R. id. bn_home); succlist = (listview) rootview. findviewbyid (R. id. succlist ); Viewtitle = (textview) rootview. findviewbyid (R. id. view_titile); // bind the event listener bnhome to the Click Event of the return button. setonclicklistener (New homelistener (getactivity (); string action = getarguments (). getstring ("action"); // defines the urlstring url = httputil. base_url + action; // if you are viewing a streaming object, modify the title if (action. equals ("viewfail. JSP ") {viewtitle. settext (R. string. view_fail);} Try {// send a request to the specified URL and convert the server response to the jsonarray object jsonarray = New jsonarray (httputil. getrequest (URL); // ① // wrap jsonarray into adapterjsonarrayadapter adapter = new jsonarrayadapter (getactivity (), jsonarray, "name", true); // ② succlist. setadapter (adapter);} catch (exception e) {dialogutil. showdialog (getactivity (), "Server Response exception. Please try again later! ", False); E. printstacktrace ();} succlist. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view, int position, long ID) {// view the details of a specified item. Viewitemdetail (position); // ③}); Return rootview;} private void viewitemdetail (INT position) {// load detail. view detailview represented by XML interface layout = getactivity (). getlayoutinflater (). inflate (R. layout. detail, null); // get detail. textview itemname = (textview) detailview in the XML interface layout. findviewbyid (R. id. itemname); textview itemkind = (textview) detailview. findviewbyid (R. id. itemkind); textview maxprice = (textview) detailview. findviewbyid (R. id. maxprice); textview itemremark = (textview) detailview. findviewbyid (R. id. itemremark); // get the clicked list item jsonobject jsonobj = (jsonobject) succlist. getadapter (). getitem (position); try {// display itemname in the text box. settext (jsonobj. getstring ("name"); itemkind. settext (jsonobj. getstring ("Kind"); maxprice. settext (jsonobj. getstring ("maxprice"); itemremark. settext (jsonobj. getstring ("DESC");} catch (jsonexception e) {e. printstacktrace ();} dialogutil. showdialog (getactivity (), detailview );}}
The above code interacts with the server through httputil. getrequest (URL) and encapsulates the returned results into a jsonarray object (essentially an array). The methods include:
Length ();
Optjsonarray ();
Optjsonobject ();
Optxxx ();
This program provides a jsonarrayadapter class, essentially an adapter, which is used to wrap the jsonarray and act as the adapter of the listview.
Public class jsonarrayadapter extends baseadapter {private context CTX; // defines the jsonarray object to be wrapped private jsonarray; // defines the attribute private string property of the jsonobject object in the list item; private Boolean hasicon; Public jsonarrayadapter (context CTX, jsonarray, string property, Boolean hasicon) {This. CTX = CTX; this. jsonarray = jsonarray; this. property = property; this. hasicon = hasicon;} @ overridepublic int getcount () {return jsonarray. length () ;}@ overridepublic object getitem (INT position) {return jsonarray. optjsonobject (position) ;}@ overridepublic long getitemid (INT position) {try {// return the idreturn (jsonobject) getitem (position) of the item )). getint ("ID");} catch (jsonexception e) {e. printstacktrace () ;}return 0 ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {// defines a linear layout manager linearlayout Linear = new linearlayout (CTX ); // set it to horizontal linear layout manager linear. setorientation (0); // create an imageviewimageview IV = new imageview (CTX); IV. setpadding (10, 0, 20, 0); IV. setimageresource (R. drawable. ITEM); // Add the image to linear in linearlayout. addview (IV); // create a textviewtextview TV = new textview (CTX); try {// obtain the property attribute string itemname = (jsonobject) of the jsonarray Element) getitem (position )). getstring (property); // set the content displayed in textview to TV. settext (itemname);} catch (jsonexception e) {e. printstacktrace ();} TV. settextsize (20); If (hasicon) {// Add textview to linearlayout. addview (TV); Return linear;} else {return TV ;}}}
:
View auction items