package com.example.demo.activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.demo.ActivityCollector;
import com.example.demo.R;
import com.example.demo.entity.Province;
import com.example.demo.entity.MapAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MapPageActivity extends Activity implements AdapterView.OnItemSelectedListener{
private Button btnCity,btnCounty;
private Spinner spin_Province;
private Context context;
private List<Province> provinceData;
private MapAdapter mapAdapter = null;
//判断是否为刚进去时触发onItemSelected的标志
private boolean one_selected = false;
private boolean two_selected = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.map_page);
provinceData = new ArrayList<>();
ActivityCollector.addActivity(this);
spin_Province = (Spinner)findViewById(R.id.spin_province);
btnCity = (Button)findViewById(R.id.btncity);
btnCounty = (Button)findViewById(R.id.btncounty);
getProvince();
}
private void init(){
}
private void getProvince() {
//第一步获取okHttpClient对象
OkHttpClient client = new OkHttpClient.Builder()
.build();
//第二步构建Request对象
String url = "http://192.168.236.1:8181/sewagePlant/province";
Request request = new Request.Builder()
.url(url)
.get()
.build();
//第三步构建Call对象
Call call = client.newCall(request);
//第四步:异步get请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("ttit", e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//得到的子线程
String result = response.body().string();
Gson gson = new Gson();
// Type type = new TypeToken<List<Province>>(){}.getType();
List<Province> provinces = gson.fromJson(result, new TypeToken<List<Province>>() {}.getType());
for (int i = 0; i <provinces.size() ; i++) {
Province p = provinces.get(i);
provinceData.add(p);
Log.i("ttit",p.getProvince());
}
myHandler.sendEmptyMessage(0);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
switch (parent.getId()){
case R.id.spin_province:
if (one_selected){
Toast.makeText(MapPageActivity.this,"选择了"+parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
}else {
one_selected = true;
}
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
private Handler myHandler = new Handler() {
@Override
//重写handleMessage方法,根据msg中what的值判断是否执行后续操作
public void handleMessage(Message msg) {
if (msg.what == 0) {
mapAdapter = new MapAdapter(provinceData,context);
spin_Province.setAdapter(mapAdapter);
spin_Province.setOnItemSelectedListener(MapPageActivity.this);
}
}
};
}