package sz.student.mu;import java.io.IOException;
import java.io.InputStream;import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;public class ChangeCity_mu extends Activity{ private ListView listcity=null;
private ExpandableListView expandlistview=null;
private static String[][] provincecityname;
private String[] provincename=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.changecity_xml_mu);
listcity=(ListView)findViewById(R.id.listview_zhixiacity);
expandlistview=(ExpandableListView)findViewById(R.id.expandablelistview01);
String[] cityname=parseProvincename("city");
provincename=parseProvincename("province");
this.parseCityname();//在ExpandablelistView适配前执行此方法,得到provincecityname。
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1
,cityname);
listcity.setAdapter(arrayAdapter);
ExpandableListAdapter adapter=new BaseExpandableListAdapter() {
private TextView getTextView(){
AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,45);
TextView textView=new TextView(ChangeCity_mu.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setTextSize(20);
textView.setPadding(36, 0, 0, 0);
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}

@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
Log.d("provincecityname", "provincecityname"+provincecityname[0].length);此处得到的长度为1;
LinearLayout ll=new LinearLayout(ChangeCity_mu.this);
ll.setOrientation(0);
TextView textView=getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}

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

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return provincename.length;
}

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

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

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=getTextView();
textView.setText(provincecityname[groupPosition][childPosition]);
return textView;
}

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

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

private String[] parseProvincename(String heartFlag){
AssetManager am=getAssets();
try {
InputStream stream=am.open("cityname.txt");
byte[] context = new byte[stream.available()];
stream.read(context);
String str = new String(context);
JSONObject jsonObject=new JSONObject(str);
JSONArray nameArray=jsonObject.getJSONArray(heartFlag);
String[] cityname=new String[nameArray.length()];
for(int i=0;i<nameArray.length();i++){
JSONObject citynameObject=nameArray.getJSONObject(i);
cityname[i]=citynameObject.getString("name");
}
return cityname;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

private void parseCityname(){
AssetManager am=getAssets();
String[][] cityname = null ;
try {
InputStream stream=am.open("cityname.txt");
byte[] context = new byte[stream.available()];
stream.read(context);
String str = new String(context);
JSONObject jsonObject=new JSONObject(str);
JSONArray nameArray=jsonObject.getJSONArray("province");
for(int i=0;i<nameArray.length();i++){
JSONObject citynameObject=nameArray.getJSONObject(i);
JSONArray citysname=citynameObject.getJSONArray("citys");
cityname=new String[nameArray.length()][citysname.length()];
for(int j=0;j<citysname.length();j++){
JSONObject citysnameObject=nameArray.getJSONObject(j);
cityname[i][j]=citysnameObject.getString("name");
provincecityname=new String[nameArray.length()][citysname.length()];
provincecityname[i][j]=cityname[i][j];
Log.d("provincecityname","provincecityname"+provincecityname[i][j]+"-"+i+"-"+j);此处的Logcat打印出了provincecityname得到的数据且数据正确。 }
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}问题叙述如上注释:
在provincecityname[][]通过parseCityname获得数据后,在用ExpandableListView适配时子列表得到的却是空数据,parseCityname获得的是子列表的数据,且Logcat正确的打印出了。
找了 很久没找到原因!

解决方案 »

  1.   

    http://topic.csdn.net/u/20111008/20/dbf499fa-99f9-4e8f-8861-864af1c81596.html
      

  2.   


    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <ListView
      android:id="@+id/listview_zhixiacity"
      android:layout_width="fill_parent"
      android:layout_height="200dp"/>
      <ExpandableListView
      android:id="@+id/expandablelistview01"
      android:layout_width="fill_parent"
      android:layout_height="280dp"/>
    </LinearLayout>这是我的XML
      

  3.   

    各位 这绝对是个 诡异的问题我追踪了下代码,问题在parseCityname这个方法,provincecityname这个数组在for循环里打印正确,出了for循环,就没数据了!
      

  4.   

    问题 解决 是cityname=new String[nameArray.length()][citysname.length()];
    的问题 谢谢各位 。
    散分喽!