package com.activity.chaoran;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import com.activity.chaoran.R;
import com.sys.SysData;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class MenuActivity extends Activity {
	private ListView listView; 
	private List mData; 
	SimpleAdapter adapter;
	Runnable downloadRun = new Runnable() {
		public void run() {
			purview();
		}
	};
	private Handler handler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {  
			adapter.notifyDataSetChanged();
		}

	};
	public void purview(){
		try {
			String url = SysData.url+"/ChaoRanBI/webservice/ServiceInterface?wsdl";
			System.out.println(url);
			String method = "selectAndroidPurview";
			SoapObject rpc = new SoapObject("", method);
			rpc.addProperty("userid",SysData.userid);
			HttpTransportSE ht = new HttpTransportSE(url);
			ht.debug = true;
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);
			ht.call("", envelope);
			if (envelope.getResponse() != null) {// 判断是否返回结果，因为我这个是一个查询操作，是带有返回值的。
				byte[] bb = org.kobjects.base64.Base64.decode(envelope
						.getResponse().toString());
				ByteArrayInputStream bin = new ByteArrayInputStream(bb);
				ObjectInputStream oin = new ObjectInputStream(bin);
				List list= (List) oin.readObject();
//				for(int i=0;i<list.size();i++){
//					mData.add(list.get(i));
//				}
				mData.addAll(list);
				System.out.println(mData.size()+"----------");
				Message message = new Message();
				handler.sendMessage(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread(downloadRun).start();
        System.out.println("--------------------------------------");
//        for(int i=0;i<9;i++){
//        	Map m=new HashMap();
//        	m.put("mname","第"+i+"个选项");
//        
//        }
        mData=new ArrayList();
        adapter = new SimpleAdapter(this,mData,R.layout.activity_menu, 
                new String[]{"mname"}, 
                new int[]{R.id.title}); 
        listView=new ListView(this);
        
        listView.setAdapter(adapter);
		OnItemClickListener lis1 = new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Map map=(Map)mData.get(arg2);
//				System.out.println(map.get("gn_no"));
				Intent intent = new Intent();
				intent.putExtra("formlx",  map.get("gn_no").toString());
				intent.setClass(getApplicationContext(), DjActivity.class);
				startActivity(intent);
			}
		};
		listView.setOnItemClickListener(lis1);
        setContentView(listView); 
    }
  

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }
}
