android端获取网络数据添加到二级列表ExpandableListView

//模拟数据,转换为二维数组格式

public class ExUtils {

    /**
     * jsonarray格式转成list格式,若为空返回null
     * @param array2
     * @return
     * @throws JSONException
     */
        private static ArrayList<String> getListValue()
                throws JSONException {
            ArrayList<String> list = new ArrayList<String>();

            for (int i = 0; i < 5; i++) {
                String s="xigua"+i;
                list.add(s);
            }
            return list;
        }

        // ArrayList转成一维数组
        private static String[] getArrayValue(ArrayList<String> list2) {
            String str[] = new String[0];
            if (list2 != null) {
                str = list2.toArray(new String[list2.size()]);
            }
            return str;
        }



        /**
         * 获取子项目方法
         * 
         * @return
         */
        public static String[][] getItemChild() {
            String[] arr1, arr2, arr3;
            String[][] arrTwo = null;
//          // 从网页获取数据
            ArrayList<String> list1, list2, list3;
//
//          HttpClient client = new DefaultHttpClient();
//          HttpPost post = new HttpPost(urlPath);
            try {
//              InputStream responseStream = client.execute(post).getEntity()
//                      .getContent();
//              // 记得转换成gbk编码
//              BufferedReader bufferedReader = new BufferedReader(
//                      new InputStreamReader(responseStream, "UTF-8"));
//              String webContentString = bufferedReader.readLine();
//              // json解析**************************
//
//              JSONObject object = new JSONObject(webContentString);
//
//              JSONArray array1 = object.getJSONArray("item1");
//              JSONArray array2 = object.getJSONArray("item2");
//              JSONArray array3 = object.getJSONArray("item3");

                arrTwo = new String[3][];

                // json转成list
                list1 = getListValue();
                list2 = getListValue();
                list3 = getListValue();

                // 转成一维数组
                arr1 = getArrayValue(list1);
                arr2 = getArrayValue(list2);
                arr3 = getArrayValue(list3);

                // 用arraylis存儲得到的非空list
                ArrayList<String[]> list = new ArrayList<String[]>();
                if (arr1 != null && arr1.length != 0) {
                    list.add(arr1);
                }
                if (arr2 != null && arr2.length != 0) {
                    list.add(arr2);
                }
                if (arr3 != null && arr3.length != 0) {
                    list.add(arr3);
                }
                // 动态生成二维数组
                int count = 0;
                for (int i = 0; i < list.size(); i++) {
                    arrTwo[count++] = list.get(i);
//                  count++;
                }
                // 测试打印二维数组的每一行
//              printOne(arrTwo[0]);
//              printOne(arrTwo[1]);
//              printOne(arrTwo[2]);


//              printTwo(arrTwo);

            } catch (Exception e) {
                System.out.println("解析子项目出错!");
            }
            return arrTwo;
        }
}

//MainActivity中调用,实现二级列表ExpandableListview

package com.example.aa;

import com.example.aa.R.string;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//      requestWindowFeature(Window.FEATURE_NO_TITLE);

      final ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
          //设置组视图的图片
          int[] logos = new int[] { R.drawable.ic_launcher, R.drawable.ic_launcher,R.drawable.ic_launcher};
          //设置组视图的显示文字
          private String[] generalsTypes = new String[] { "宝一", "宝二", "宝三" };
          //子视图显示文字
          /*private String[][] generals = new String[][] {
                  { "宝一孩儿1", "宝一孩儿2", "宝一孩儿3", "宝一孩儿4", "宝一孩儿5", "宝一孩儿6" },
                  { "宝二孩儿1", "宝二孩儿2", "宝二孩儿3", "宝二孩儿4", "宝二孩儿5", "宝二孩儿6" },
                  { "宝三孩儿1", "宝三孩儿2", "宝三孩儿3", "宝三孩儿4", "宝三孩儿5" }

          };*/

          private String[][] generals=ExUtils.getItemChild();
          //子视图图片
          public int[][] generallogos = new int[][] {
                  { R.drawable.ic_launcher, R.drawable.ic_launcher,
                          R.drawable.ic_launcher, R.drawable.ic_launcher,
                          R.drawable.ic_launcher, R.drawable.ic_launcher },
                  { R.drawable.ic_launcher, R.drawable.ic_launcher,
                          R.drawable.ic_launcher, R.drawable.ic_launcher,
                          R.drawable.ic_launcher, R.drawable.ic_launcher },
                  { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
                          R.drawable.ic_launcher, R.drawable.ic_launcher } };

          //自己定义一个获得文字信息的方法
          TextView getTextView() {
              AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                      ViewGroup.LayoutParams.FILL_PARENT, 64);
              TextView textView = new TextView(
                      MainActivity.this);
              textView.setLayoutParams(lp);
              textView.setGravity(Gravity.CENTER_VERTICAL);
              textView.setPadding(36, 0, 0, 0);
              textView.setTextSize(20);
              textView.setTextColor(Color.BLACK);
              return textView;
          }


          //重写ExpandableListAdapter中的各个方法
          @Override
          public int getGroupCount() {
              // TODO Auto-generated method stub
              return generalsTypes.length;
          }

          @Override
          public Object getGroup(int groupPosition) {
              // TODO Auto-generated method stub
              return generalsTypes[groupPosition];
          }

          @Override
          public long getGroupId(int groupPosition) {
              // TODO Auto-generated method stub
              return groupPosition;
          }

          @Override
          public int getChildrenCount(int groupPosition) {
              // TODO Auto-generated method stub
              return generals[groupPosition].length;
          }

          @Override
          public Object getChild(int groupPosition, int childPosition) {
              // TODO Auto-generated method stub
              return generals[groupPosition][childPosition];
          }

          @Override
          public long getChildId(int groupPosition, int childPosition) {
              // TODO Auto-generated method stub
              return childPosition;
          }

          @Override
          public boolean hasStableIds() {
              // TODO Auto-generated method stub
              return true;
          }

          @Override
          public View getGroupView(int groupPosition, boolean isExpanded,
                                   View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
              LinearLayout ll = new LinearLayout(
                      MainActivity.this);
//              ll.setOrientation(0);
              ImageView logo = new ImageView(MainActivity.this);
              logo.setImageResource(logos[groupPosition]);
              logo.setPadding(50, 0, 0, 0);
              ll.addView(logo);
              TextView textView = getTextView();
              textView.setTextColor(Color.BLACK);
              textView.setText(getGroup(groupPosition).toString());
              ll.addView(textView);

              return ll;
          }

          @Override
          public View getChildView(int groupPosition, int childPosition,
                                   boolean isLastChild, View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
              LinearLayout ll = new LinearLayout(
                      MainActivity.this);
//              ll.setOrientation(0);
              ImageView generallogo = new ImageView(
                      MainActivity.this);
              generallogo
                      .setImageResource(generallogos[groupPosition][childPosition]);
              ll.addView(generallogo);
              TextView textView = getTextView();
              textView.setText(getChild(groupPosition, childPosition)
                      .toString());
              ll.addView(textView);
              return ll;
          }

          @Override
          public boolean isChildSelectable(int groupPosition,
                                           int childPosition) {
              // TODO Auto-generated method stub
              return true;
          }

      };

      ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list);
      expandableListView.setAdapter(adapter);


      //设置item点击的监听器
      expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

          @Override
          public boolean onChildClick(ExpandableListView parent, View v,
                                      int groupPosition, int childPosition, long id) {

              Toast.makeText(
                      MainActivity.this,
                      "你点击了" + adapter.getChild(groupPosition, childPosition),
                      Toast.LENGTH_SHORT).show();

              return false;
          }
      });
  }
}

感谢博主:http://blog.csdn.net/yhm2046/article/details/8601948



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值