ExpandableListView

有这样的json数组[\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"],传到第二个activity中,将streety对应的数据显示到组上面,将street和city对应的数据显示在相应的组下面。效果图:


布局文件:

activity_q.xml



 


activity_main.xml



	
	
group.xml



    android:id ="@+id/uselistGroup1"
/>
 
  

 child.xml 
  



	
	"
	 
	 
	 
	 	
     
     
    

QActivity.java


package 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> 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>();
     	    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+"");
			
		}
    	
    }

}
MainActivity.java

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> groups;
	List>> 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>)intent.getSerializableExtra("list");
//        Map group1 = new HashMap();  
//        group1.put("group", "group1");  
//        Map group2 = new HashMap();  
//        group2.put("group", "group2");  
//        groups.add(group1);  
//        groups.add(group2);  
        List>> childs = new ArrayList>>();
        for(int i=0;i> child1 = new ArrayList>();
      	  Map childdata = new HashMap();
      	childdata=groups.get(i);
      	child1.add(childdata);
      	Log.d("child1",child1.toString());
          childs.add(child1);
        }   
       // List> child1 = (List>)intent.getSerializableExtra("list"); 
//        Map childData1 = new HashMap();  
//        childData1.put("child", "child1Data1");  
//        Map childData2 = new HashMap();  
//        childData2.put("child", "child1Data2");  
//        child1.add(childData1);  
//        child1.add(childData2);  
          
      //  List> child2 = (List>)intent.getSerializableExtra("list"); 
//        Map child2Data1 = new HashMap();  
//        child2Data1.put("child", "child2Data1");  
//        Map child2Data2 = new HashMap();  
//        child2Data2.put("child", "child2Data2");  
//        child2.add(child2Data1);  
//        child2.add(child2Data2);  
          
//        childs = new ArrayList>>();  
//        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 group1 = new HashMap();  
//        group1.put("group", "group3");
//        groups.add(group1);  
//        List> child1 = new ArrayList>();  
//        Map childData1 = new HashMap();  
//        childData1.put("child", "child3Data1");  
//        Map childData2 = new HashMap();  
//        childData2.put("child", "child3Data2");  
//        child1.add(childData1);  
//        child1.add(childData2);  
//        childs.add(child1);  
//        
//        Map childData = new HashMap();  
//        childData.put("child", "child1Data22");  
//        childs.get(1).add(childData);
//        sla.notifyDataSetChanged();
//        System.out.println("===:"+sla.getGroupCount());
//    }
}



下面这段也可以用来写

 

childs = new ArrayList>>();
        for(int i=0;i> child1 = new ArrayList>();
       Map childdata = new HashMap();
      childdata=groups.get(i);
      child1.add(childdata);
      Log.d("child1",child1.toString());
          childs.add(child1);



 
  

去掉默认的展开收缩箭头 act.setGroupIndicator(null); 使用自己自定义的图片 在drawable中加入两张图片 在layout中新建一个xml文档expandlist.xml 在java中填写一句:act.setGroupIndicator(this.getResources().getDrawable(R.layout.expandlist));

 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(android学习笔记)