Use the ExpandableListView control in android to download server json files.

Source: Internet
Author: User

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1053514219-0.jpg "title =" qqpicture 20130825115215.jpg "width =" 250 "height =" 352 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" width: 250px; height: 352px; "/>



Layout file code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".AllsectionActivity" >    <ExpandableListView        android:id="@+id/expandableListView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true" >    </ExpandableListView>                                                                                                   </RelativeLayout>


The data of the parent and child nodes in the figure are both JSON files from the server.

Download and parse the json file.

Set the adapter, select BaseExpandableListAdapter (), and rewrite several methods. The key is to get the objects of the parent node and child node layout in getGroupView () and getChildView (), and set the data for them.

The following code details:

Public class AllsectionActivity extends Activity {// initialize the parent node and child node type Object [] [] mChilds = null; // The child node is a two-dimensional array String [] mGroups = null; private ExpandableListView listView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_allbankuai); AllformsAsyncTask task = new AllformsAsyncTask (); task.exe cute (Contents. URL_ALLFORMS); listView = (ExpandableListView) findViewById (R. id. expandableListView1); // ExpandableListView inherits the listview. setOnChildClickListener (new OnChildClickListener () {// set the Click Event @ Override public boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id) for the subnode) {startActivity (new Intent (AllsectionActivity. this, BankuaitieziliebiaoActivity. class); return false; // jump to the forum post list});} class AllformsAsyncTask extends AsyncTask <String, Void, String >{@ Override protected String doInString... params) {String json = HTTPutil. getJson (params [0]); // download the json file. The data of the parent and child nodes in the view is the return JSON file from the server ;} @ Override protected void onPostExecute (String result) {if (result = null | "". equals (result) {return;} try {JSONArray groupArr = new JSONArray (result); mGroups = new String [groupArr. length ()]; // create an array and create it statically. You need to define the length of the parent node array mChilds = new Object [groupArr. length ()] []; // create a two-dimensional array and create a static node. You need to define the one-dimensional length of the sub-node array. The length of the sub-node must be defined twice) for (int I = 0; I <groupArr. length (); I ++) {JSONObject jsonObject = groupArr. getJSONObject (I); String group_name = jsonObject. getString ("group_name"); mGroups [I] = group_name; JSONArray childArr = jsonObject. getJSONArray ("child"); mChilds [I] = new Object [childArr. length ()]; // defines the two-dimensional length of each child node array for (int j = 0; j <childArr. length (); j ++) {JSONObject childObject = childArr. getJSONObject (j); String title = childObject. getString ("title"); String today = childObject. getString ("today"); String post = childObject. getString ("post"); String all = childObject. getString ("all"); ChildItemdata childItemdata = new ChildItemdata (title, today, post, all); mChilds [I] [j] = childItemdata; // single row data of the subnode }}catch (JSONException e) {// TODO Auto-generated catch block e. printStackTrace ();} setadapter (mGroups, mChilds);} private void setadapter (final String [] mGroups, final Object [] [] mChilds) {listView. setAdapter (new BaseExpandableListAdapter () {// use BaseExpandableListAdapter, Override several methods @ Override public boolean isChildSelectable (int groupPosition, int childPosition) {// can a subnode click return true ;} @ Override public boolean hasStableIds () {// TODO Auto-generated method stub return false;} @ Override public View getGroupView (int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {// get the view object TextView textview_group = (TextView) getLayoutInflater () of the group (). inflate (R. layout. textview_group, null); textview_group.setText (mGroups [groupPosition]); return textview_group ;}@ Override public long getGroupId (int groupPosition) {// TODO Auto-generated method stub return 0 ;} @ Override public int getGroupCount () {return mGroups. length ;}@ Override public Object getGroup (int groupPosition) {// TODO Auto-generated method stub return null;} @ Override public int getChildrenCount (int groupPosition) {// Number of subnodes under each parent node return mChilds [groupPosition]. length ;}@ Override public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {// obtain the child view object RelativeLayout child; ViewHolder holder; if (convertView = null) {holder = new ViewHolder (); child = (RelativeLayout) getLayoutInflater (). inflate (R. layout. listitem_allforms, null); holder. allforms_iv_shoucang = (ImageView) child. findViewById (R. id. allforms_iv_shoucang); holder. allforms_ TV _title = (TextView) child. findViewById (R. id. allforms_ TV _title); holder. allforms_ TV _today = (TextView) child. findViewById (R. id. allforms_ TV _today); holder. allforms_ TV _post = (TextView) child. findViewById (R. id. allforms_ TV _post); holder. allforms_ TV _all = (TextView) child. findViewById (R. id. allforms_ TV _all); child. setTag (holder);} else {child = (RelativeLayout) convertView; holder = (ViewHolder) child. getTag ();} ChildItemdata childItemdata = (ChildItemdata) mChilds [groupPosition] [childPosition]; // obtain the single row data holder of the subnode. allforms_ TV _title.setText (childItemdata. title); holder. allforms_ TV _today.setText (childItemdata. today); holder. allforms_ TV _post.setText (childItemdata. post); holder. allforms_ TV _all.setText (childItemdata. all); return child;} @ Override public long getChildId (int groupPosition, int childPosition) {// TODO Auto-generated method stub return 0 ;} @ Override public Object getChild (int groupPosition, int childPosition) {// TODO Auto-generated method stub return null ;}});} class ViewHolder {ImageView allforms_iv_shoucang; TextView allforms_ TV _title; textView allforms_ TV _today; TextView allforms_ TV _post; TextView allforms_ TV _all;} class ChildItemdata {String title; String today; String post; String all; public ChildItemdata (String title, String today, String post, all) {super (); this. title = title; this. today = today; this. post = post; this. all = all ;}@ Override public String toString () {return "ChildItemdata [title =" + title + ", today =" + today + ", post = "+ post +", all = "+ all +"] ";}}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. allbankuai, menu); return true ;}}


This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1282322

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.