日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 中使用ExpandableListView控件结合服务器json文件的下载

發布時間:2025/4/5 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 中使用ExpandableListView控件结合服务器json文件的下载 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



布局文件代碼:

<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" ><ExpandableListViewandroid:id="@+id/expandableListView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ></ExpandableListView></RelativeLayout>


圖中的父節點和子節點的數據都是來自服務器的JSON文件。

下載json文件并解析。

設置適配器,選擇BaseExpandableListAdapter(),重寫幾個方法,關鍵是在 getGroupView(),getChildView()得到父節點、子節點布局的對象,并為他們設置數據上去。

以下是詳細代碼:

public class AllsectionActivity extends Activity {//初始化父節點和子節點類型Object[][] mChilds = null; //子節點為二維數組String[] mGroups = null;private ExpandableListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_allbankuai);AllformsAsyncTask task = new AllformsAsyncTask();task.execute(Contents.URL_ALLFORMS);listView = (ExpandableListView) findViewById(R.id.expandableListView1);//ExpandableListView繼承listviewlistView.setOnChildClickListener(new OnChildClickListener(){//為子節點設置點擊事件@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id){startActivity(new Intent(AllsectionActivity.this,BankuaitieziliebiaoActivity.class ));return false;//跳轉到版塊帖子列表}});}class AllformsAsyncTask extends AsyncTask<String, Void, String>{@Overrideprotected String doInString... params){String json = HTTPutil.getJson(params[0]);//下載json文件,這里視圖中的父節點和子節點的數據都是來自服務器給的JSON文件return json;}@Overrideprotected void onPostExecute(String result){if(result == null || "".equals(result)){return;}try{JSONArray groupArr = new JSONArray(result);mGroups = new String[groupArr.length()];//新建數組,靜態創建,需要定義父節點數組長度mChilds = new Object[groupArr.length()][];//新建二維數組,靜態創建,需要定義子節點的數組的一維長度(子節點的長度需要定義兩次)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()];//定義每個子節點的數組的二維長度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;//子節點的單行數據}}}catch (JSONException e){// TODO Auto-generated catch blocke.printStackTrace();}setadapter(mGroups,mChilds);}}private void setadapter(final String[] mGroups, final Object[][] mChilds){listView.setAdapter(new BaseExpandableListAdapter(){//使用BaseExpandableListAdapter,重寫幾個方法@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition){//子節點是否可以點擊return true;}@Overridepublic boolean hasStableIds(){// TODO Auto-generated method stubreturn false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){//得到group的view對象TextView textview_group = (TextView) getLayoutInflater().inflate(R.layout.textview_group, null);textview_group.setText(mGroups[groupPosition]);return textview_group;}@Overridepublic long getGroupId(int groupPosition){// TODO Auto-generated method stubreturn 0;}@Overridepublic int getGroupCount(){return mGroups.length;}@Overridepublic Object getGroup(int groupPosition){// TODO Auto-generated method stubreturn null;}@Overridepublic int getChildrenCount(int groupPosition){//每個父節點下的子節點數量return mChilds[groupPosition].length;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){//得到child的view對象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];//得到子節點的單行數據holder.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;}@Overridepublic long getChildId(int groupPosition, int childPosition){// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getChild(int groupPosition, int childPosition){// TODO Auto-generated method stubreturn 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, String all){super();this.title = title;this.today = today;this.post = post;this.all = all;}@Overridepublic String toString(){return "ChildItemdata [title=" + title + ", today=" + today+ ", post=" + post + ", all=" + all + "]";}}@Overridepublic 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;} }


轉載于:https://blog.51cto.com/wangcuijing/1282322

總結

以上是生活随笔為你收集整理的android 中使用ExpandableListView控件结合服务器json文件的下载的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。