新手问题,比较二逼,不好意思问出来,但是本人已经尝试太多次了。这样的,这个是使用ExpandableListAdapte 来实现通讯录功能,本地化完全没问题,private void initializeData() {这里是本地的数据}。
现在需要取网络数据,所以采用了handler,然后就在handler里面用了 addInfo 不知道为什么不能无法加载了。求高手科普,666
现在需要取网络数据,所以采用了handler,然后就在handler里面用了 addInfo 不知道为什么不能无法加载了。求高手科普,666
package com.dailyreport.main; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ExpandableListActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import com.example.dailyreport.R; public class Organization extends ExpandableListActivity { List<String> group; // 组列表 List<List<String>> child; // 子列表 ContactsInfoAdapter adapter; // 数据适配器 /** * 初始化组、子列表数据 */ private void initializeData() { group = new ArrayList<String>(); child = new ArrayList<List<String>>(); addInfo("Andy", new String[] { "male", "138123***", "GuangZhou" }); addInfo("Fairy", new String[] { "female", "138123***", "GuangZhou" }); addInfo("Jerry", new String[] { "male", "138123***", "ShenZhen" }); addInfo("Tom", new String[] { "female", "138123***", "ShangHai" }); addInfo("Bill", new String[] { "male", "138231***", "ZhanJiang" }); } //handle取网络线程的数据 protected static final int SET_ORGANIZATION = 1; private Handler myhandler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == SET_ORGANIZATION) { String depart_json_array = (String) msg.obj; JSONArray depart_json_arr = null; try { depart_json_arr = new JSONArray(depart_json_array); } catch (JSONException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } try { JSONObject depart_info = (JSONObject) depart_json_arr .get(0); String depart_info_s = depart_info.get( "contact_depart_json").toString(); String[] armtypes = depart_info_s.split(","); JSONObject departdetail_info = (JSONObject) depart_json_arr .get(1); String contact_departdetail_s = departdetail_info.get( "contact_departdetail_json").toString(); String[] departdetail_arr = contact_departdetail_s .split("#"); initializeData(); for (int i = 0; i < armtypes.length; i++) { String[] arrarr = new String[]{}; arrarr = departdetail_arr[i].split(","); addInfo(armtypes[i], arrarr); System.out.println(armtypes[i]);//检查数组! for (int j = 0; j < arrarr.length; j++) { System.out.println(arrarr[j]); // 检查二维数组! } } // addInfo(armtypes[0], departdetail_arr[0].split(",")); // addInfo("Andy", new String[] { "male", "138123***", "GuangZhou" }); // addInfo("Fairy", new String[] { "female", "138123***", "GuangZhou" }); // addInfo("Jerry", new String[] { "male", "138123***", "ShenZhen" }); // addInfo("Tom", new String[] { "female", "138123***", "ShangHai" }); // addInfo("Bill", new String[] { "male", "138231***", "ZhanJiang" }); } catch (JSONException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } Log.v("depart_json_array", depart_json_array); } }; }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为无标题 setContentView(R.layout.organization); getExpandableListView().setBackgroundResource( R.drawable.contact_background); new URLConnectionThread().start(); initializeData(); getExpandableListView().setAdapter(new ContactsInfoAdapter()); getExpandableListView().setCacheColorHint(0); // 设置拖动列表的时候防止出现黑色背景 // getExpandableListView().setGroupIndicator(null);//去箭头 } /** * 模拟给组、子列表添加数据 * * @param g * -group * @param c * -child */ private void addInfo(String g, String[] c) { group.add(g); List<String> childitem = new ArrayList<String>(); for (int i = 0; i < c.length; i++) { childitem.add(c[i]); } child.add(childitem); } //列表 class ContactsInfoAdapter extends BaseExpandableListAdapter { // --Child--// @Override public Object getChild(int groupPosition, int childPosition) { return child.get(groupPosition).get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return child.get(groupPosition).size(); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String string = child.get(groupPosition).get(childPosition); return getGenericView(string); } // --Group--// @Override public Object getGroup(int groupPosition) { return group.get(groupPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return group.size(); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String string = group.get(groupPosition); return getGenericView(string); } // 创建组/子视图 public TextView getGenericView(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); // 框架行间距 TextView text = new TextView(Organization.this); text.setLayoutParams(lp); // Center the text vertically text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); // Set the text starting position text.setPadding(36, 0, 0, 0); text.setTextSize(20); text.setText(s); return text; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } } //网络请求数据 class URLConnectionThread extends Thread { @Override public void run() { // TODO Auto-generated method stub super.run(); String contact_json = null; URL url; try { url = new URL( "http://42.51.5.89:9380/DailyReport_contact/servlet/DailyReportInfoServlet_contact"); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader( is, "UTF-8")); String line; while ((line = br.readLine()) != null) { contact_json = new String(line);// 读取页面字符串转换为json } System.out.println("get depart ok is:" + contact_json); br.close(); } catch (MalformedURLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } Message msg = new Message(); msg.what = SET_ORGANIZATION; msg.obj = contact_json; myhandler.sendMessage(msg); Log.v("contact_json", contact_json); } } }
解决方案
6
handler是是异步执行,不是你按照先后顺序写,就先后执行的,你把setAdapter放在handler里面试试
60
在handler里面的最后一句加上 getExpandableListView().setAdapter(new ContactsInfoAdapter());