有这样的json数组[\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"],传到第二个activity中,将streety对应的数据显示到组上面,将street和city对应的数据显示在相应的组下面。效果图:
布局文件:
activity_q.xml
<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" >
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查询"></Button>
</RelativeLayout>
activity_main.xml
<?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">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ExpandableListView android:id="@+id/list"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:drawSelectorOnTop="false" ></ExpandableListView>
</LinearLayout>
group.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation = "horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:id ="@+id/uselistGroup" android:layout_marginLeft="30dp" android:text="副编码" /> <TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content"
/> </LinearLayout> child.xmlandroid:id ="@+id/uselistGroup1"
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="委托号"/>" <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/childId" android:paddingLeft="80px" android:paddingTop="5px" android:paddingBottom="5px" android:text="TextView"></TextView> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="协助单位"/> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/childId00" android:paddingLeft="80px" android:paddingTop="25px" android:paddingBottom="5px" android:text="TextView"></TextView> </LinearLayout> </LinearLayout>
QActivity.java
MainActivity.javapackage com.example.m; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class QActivity extends Activity { private Button button; private List<Map<String, Object>> list; String result="[{\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"}]"; //String result="[{\"child\":\"changsha\"},{\"child\":\"beijing\"}]"; @SuppressWarnings("unchecked") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_q); this.button=(Button)findViewById(R.id.button); button.setOnClickListener(new buttonClick()); String result1="{\"address\":"+result+"}"; Log.d("result1",result1); try{ JSONObject json=new JSONObject(result1); JSONArray jsonlist=json.getJSONArray("address"); list = new ArrayList<Map<String, Object>>(); list=JsonHelper.toList(jsonlist); Log.d("list***",list.toString()); } catch(Exception e){ } } class buttonClick implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(QActivity.this,MainActivity.class); intent.putExtra("list", (Serializable)list); QActivity.this.startActivity(intent); Log.d("list",(Serializable)list+""); } } }
package com.example.m; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SimpleExpandableListAdapter; import android.widget.Toast; public class MainActivity extends Activity { ExpandableListView act; List<Map<String,String>> groups; List<List<Map<String,String>>> childs; SimpleExpandableListAdapter sla; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); act = (ExpandableListView)findViewById(R.id.list); Intent intent=getIntent(); groups = (List<Map<String, String>>)intent.getSerializableExtra("list"); // Map<String,String> group1 = new HashMap<String,String>(); // group1.put("group", "group1"); // Map<String,String> group2 = new HashMap<String,String>(); // group2.put("group", "group2"); // groups.add(group1); // groups.add(group2); List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>(); for(int i=0;i<groups.size();i++){ List<Map<String,String>> child1 = new ArrayList<Map<String,String>>(); Map<String,String> childdata = new HashMap<String,String>(); childdata=groups.get(i); child1.add(childdata); Log.d("child1",child1.toString()); childs.add(child1); } // List<Map<String,String>> child1 = (List<Map<String, String>>)intent.getSerializableExtra("list"); // Map<String,String> childData1 = new HashMap<String, String>(); // childData1.put("child", "child1Data1"); // Map<String,String> childData2 = new HashMap<String, String>(); // childData2.put("child", "child1Data2"); // child1.add(childData1); // child1.add(childData2); // List<Map<String,String>> child2 = (List<Map<String, String>>)intent.getSerializableExtra("list"); // Map<String,String> child2Data1 = new HashMap<String, String>(); // child2Data1.put("child", "child2Data1"); // Map<String,String> child2Data2 = new HashMap<String, String>(); // child2Data2.put("child", "child2Data2"); // child2.add(child2Data1); // child2.add(child2Data2); // childs = new ArrayList<List<Map<String,String>>>(); // childs.add(child1); // childs.add(child2); //生成对象 //1.context //2.一级条目数据 //3.用来设置一级条目样式的布局文件 //4.指定一级条目数据的Key //5.指定一级条目数据显示的group控件id //6.指定二级条目数据 //7.用来设置二级条目样式的布局文件 //8.指定二级条目数据的key //9.指定二级条目数据显示的控件的id sla = new SimpleExpandableListAdapter (this, groups, R.layout.group, new String[]{"street"}, new int[]{R.id.uselistGroup1}, childs, R.layout.child, new String[]{"city","street"}, new int[]{R.id.childId,R.id.childId00}); act.setAdapter(sla); System.out.println("act.getChildCount():"+act.getCount());; for (int i = 0; i < sla.getGroupCount(); i++) { System.out.println("i:"+i); act.expandGroup(i); } //设置为null则去掉箭头 // act.setGroupIndicator(null); //子节点的监听事件 act.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { System.out.println("i:"+i+" i1:"+i1+" l:"+l); HashMap m = (HashMap) sla.getChild(i, i1); System.out.println("m:"+m); Toast.makeText(MainActivity.this, (CharSequence) m.get("street"), Toast.LENGTH_SHORT).show(); Intent intent=new Intent(); intent.setClass(MainActivity.this,BillActivity.class); MainActivity.this.startActivity(intent); // String albumid = (String) m.get(NAME); // Toast.makeText(Main.this, albumid, Toast.LENGTH_SHORT).show(); return false; //todo } }); //父节点的监听事件 act.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) { System.out.println("v:"+v); return false; } }); } // public void expandClick(View v){ // Map<String,String> group1 = new HashMap<String,String>(); // group1.put("group", "group3"); // groups.add(group1); // List<Map<String,String>> child1 = new ArrayList<Map<String,String>>(); // Map<String,String> childData1 = new HashMap<String, String>(); // childData1.put("child", "child3Data1"); // Map<String,String> childData2 = new HashMap<String, String>(); // childData2.put("child", "child3Data2"); // child1.add(childData1); // child1.add(childData2); // childs.add(child1); // // Map<String,String> childData = new HashMap<String, String>(); // childData.put("child", "child1Data22"); // childs.get(1).add(childData); // sla.notifyDataSetChanged(); // System.out.println("===:"+sla.getGroupCount()); // } }
下面这段也可以用来写
childs = new ArrayList<List<Map<String, Object>>>(); for(int i=0;i<groups.size();i++){ List<Map<String, Object>> child1 = new ArrayList<Map<String, Object>>(); Map<String, Object> childdata = new HashMap<String, Object>(); childdata=groups.get(i); child1.add(childdata); Log.d("child1",child1.toString()); childs.add(child1);
去掉默认的展开收缩箭头 act.setGroupIndicator(null); 使用自己自定义的图片 在drawable中加入两张图片 在layout中新建一个xml文档expandlist.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawable/ic_arrow_down" /> <item android:drawable="@drawable/ic_arrow_right" /> </selector> 在java中填写一句:act.setGroupIndicator(this.getResources().getDrawable(R.layout.expandlist));