android 中使用ExpandableListView控件结合服务器json文件的下载
生活随笔
收集整理的這篇文章主要介紹了
android 中使用ExpandableListView控件结合服务器json文件的下载
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
布局文件代碼:
圖中的父節點和子節點的數據都是來自服務器的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文件的下载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 排序算法(1)—直接插入排序
- 下一篇: 李雷和韩梅梅的一次转账事务–事务系统概述