View Item types

Source: Internet
Author: User

To view the type of an item in Android, first send a request to the server through httputil, convert the response string from the server to a jsonarray object, then use the adapter to wrap the jsonarray object, and use listview to display it.

View the layout page of the item type:
<? 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"> <linearlayoutan Droid: Orientation = "vertical" Android: gravity = "center" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> <textviewandroid: TEXT = "@ string/manage_kind" Android: textsize = "@ dimen/label_font_size" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> <! -- Add a category button --> <buttonandroid: Id = "@ + ID/bnadd" Android: layout_width = "85dp" Android: layout_height = "30dp" Android: background = "@ drawable/add_kind"/> </linearlayout> <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> <! -- Display the listview of the category list --> <listviewandroid: Id = "@ + ID/kindlist" Android: layout_width = "match_parent" Android: layout_height = "match_parent"/> </linearlayout>

Display the fragment code of the item type:

Public class managekindfragment extends Fragment
{Public static final int add_kind = 0x1007; button bnhome, bnadd; listview kindlist; callbacks mcallbacks; @ overridepublic view oncreateview (layoutinflater Inflater, viewgroup container, bundle savedinstancestate) {view rootview = Inflater. inflate (R. layout. manage_kind, container, false); // obtain the two buttons bnhome = (button) rootview in the interface layout. findviewbyid (R. id. bn_home); bnadd = (button) rootview. findviewbyid (R. id. BNA Dd); kindlist = (listview) rootview. findviewbyid (R. id. kindlist); // bind event listener bnhome to the Click Event of the return button. setonclicklistener (New homelistener (getactivity (); // bind the event listener bnadd to the Click Event of the Add button. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (View Source) {// when the Add button is clicked, call the onitemselected method mcallbacks of the activity where the fragment is located. onitemselected (add_kind, null) ;}}); // defines the urlstring url = httputil. base_url + "View Kind. JSP "; try {// send a request to the specified URL and encapsulate the response into a jsonarray object final jsonarray = new jsonarray (httputil. getrequest (URL); // wrap the jsonarray object into adapterkindlist. setadapter (New kindarrayadapter (jsonarray, getactivity ();} catch (exception e) {dialogutil. showdialog (getactivity (), "Server Response exception. Please try again later! ", False); E. printstacktrace ();} return rootview;} // when the fragment is added and displayed to the activity, the method @ overridepublic void onattach (activity) {super. onattach (activity); // If activity does not implement the callbacks interface, an exception if (! (Activity instanceof callbacks) {Throw new illegalstateexception ("the activity where managekindfragment is located must implement the callbacks interface! ");} // Treat the activity as the callbacks object mcallbacks = (callbacks) activity ;} // when the fragment is deleted from the activity to which it belongs, the method @ overridepublic void ondetach () {super. ondetach (); // assign mcallbacks to null. Mcallbacks = NULL ;}}

  

  

View Item types

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.