package com.qian.tourism.activity;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qian.tourism.Data.cityname;
import com.qian.tourism.Entity.Result;
public class CityActivity extends Activity {
@ViewInject(R.id.spinner2)
private Spinner spinProvince;
@ViewInject(R.id.spinner1)
private Spinner spinCity;
private String city="";
// 声明数组适配器,用来填充城市列表
private ArrayAdapter<CharSequence> adapterCity = null;
StringBuffer sb = new StringBuffer();
private Map<String, String> cityData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city);
ViewUtils.inject(this);
cityData = cityname.getCityData(this);
sb.delete(0, sb.length());
for (Entry<String, String> sa : cityData.entrySet()) {
sb.append(",").append(sa.getKey());
}
// 为spinProvince控件注册OnItemSelected监听器
spinProvince.setAdapter(new ArrayAdapter<CharSequence>(
CityActivity.this, android.R.layout.simple_spinner_item, sb
.toString().substring(1).split(",")));
spinProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view,
int position, long id) {
// Toast.makeText(getApplicationContext(),
// ((TextView)view).getText().toString(),
// Toast.LENGTH_LONG).show();
String a = "";
for (Entry<String, String> sa : cityData.entrySet()) {
if (sa.getKey().equals(
((TextView) view).getText().toString())) {
a = sa.getValue();
}
}
CityActivity.this.adapterCity = new ArrayAdapter<CharSequence>(
CityActivity.this,
android.R.layout.simple_spinner_item, a.split(","));
// 使用adapterCity数据适配器填充spinCity
CityActivity.this.spinCity
.setAdapter(CityActivity.this.adapterCity);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
spinCity.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int arg2, long arg3) {
// TODO Auto-generated method stub
city = ((TextView) view).getText().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void jinru(View v) {
Intent it=new Intent(CityActivity.this, MainActivity.class);
Bundle bundle = new Bundle();
List<Result> allData = cityname.getAllData(this);
for (Result result : allData) {
if(result.getCityName().equals(city)){
bundle.putString("cityId", result.getCityId());
bundle.putString("provinceId", result.getProvinceId());
bundle.putString("cityname", result.getCityName());
break;
}
}
it.putExtras(bundle);
startActivity(it);
}
}
和下面的读取文件类
package com.qian.tourism.Data;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import android.content.Context;
import com.alibaba.fastjson.JSON;
import com.qian.tourism.Entity.CityEntity;
import com.qian.tourism.Entity.Result;
public class cityname {
private final static String URL = "http://apis.haoservice.com/lifeservice/travel/cityList?key=6e04704c185145dca3684e2b0f7110e7";
public static Map<String, String> getCityData(Context context) {
StringBuffer sb = new StringBuffer();
try {
InputStream open = context.getAssets().open("a.txt");
String encoding = "GBK";
InputStreamReader read = new InputStreamReader(open, encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
sb.append(lineTxt);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
//
CityEntity parseObject = JSON.parseObject(sb.toString(),
CityEntity.class);
final List<Result> result = parseObject.getResult();
Set<String> set = new TreeSet<String>();
//
for (Result result2 : result) {
set.add(result2.getProvinceId());
}
Map<String, String> map = new HashMap<String, String>();
String b = "";
for (String string : set) {
for (int i = 0; i < result.size(); i++) {
Result result2 = result.get(i);
if (string.equals(result2.getProvinceId())) {
String a = "," + result2.getCityName();
b += a;
// System.out.print(a);
map.put(string, b);
} else {
b = "";
}
}
// System.out.println();
}
for (Entry<String, String> string : map.entrySet()) {
string.setValue(string.getValue().substring(1));
}
Map<String, String> map2 = new HashMap<String, String>();
for (Entry<String, String> string : map.entrySet()) {
String[] split = string.getValue().split(",");
String a = "";
for (int i = 0; i < split.length; i++) {
if (i != 0) {
a += "," + split[i];
}
map2.put(split[0], a);
}
}
for (Entry<String, String> string : map2.entrySet()) {
string.setValue(string.getValue().substring(1));
//System.out.println(string);
}
return map2;
}
public static List<Result> getAllData(Context context) {
StringBuffer sb = new StringBuffer();
try {
InputStream open = context.getAssets().open("a.txt");
String encoding = "GBK";
InputStreamReader read = new InputStreamReader(open, encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
sb.append(lineTxt);
}
read.close();
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
//
CityEntity parseObject = JSON.parseObject(sb.toString(),
CityEntity.class);
return parseObject.getResult();
}
}