先要建议三张表:province、city、county,分别用于存放省市县的数据信息。
Province.java
package com.example.lenovo.coolweather.db;
import org.litepal.crud.DataSupport;
/**
* 代表省的数据库表
*/
public class Province extends DataSupport {
private int id; //id号
private String provinceName; //省得名字
private int provinceCode; //省得代号
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public int getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(int provinceCode) {
this.provinceCode = provinceCode;
}
}
City.java
package com.example.lenovo.coolweather.db;
import org.litepal.crud.DataSupport;
/**
* 代表市的数据库表
*/
public class City extends DataSupport {
private int id;
private String cityName; //城市名字
private int cityCode; //城市代号
private int provinceId; //当前城市所属省得id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public int getCityCode() {
return cityCode;
}
public void setCityCode(int cityCode) {
this.cityCode = cityCode;
}
public int getProvinceId() {
return provinceId;
}
public void setProvinceId(int provinceId) {
this.provinceId = provinceId;
}
}
County.java
package com.example.lenovo.coolweather.db;
import org.litepal.crud.DataSupport;
/**
* 代表县的数据库表
*/
public class County extends DataSupport {
private int id;
private String countName; //县的名字
private String weatherId; //天气id
private int cityId; //当前县所属城市的id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCountName() {
return countName;
}
public void setCountName(String countName) {
this.countName = countName;
}
public String getWeatherId() {
return weatherId;
}
public void setWeatherId(String weatherId) {
this.weatherId = weatherId;
}
public int getCityId() {
return cityId;
}
public void setCityId(int cityId) {
this.cityId = cityId;
}
}
遍历省市县数据的碎片
ChooseAreaFragment.java
package com.example.lenovo.coolweather;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.lenovo.coolweather.db.City;
import com.example.lenovo.coolweather.db.County;
import com.example.lenovo.coolweather.db.Province;
import com.example.lenovo.coolweather.util.HttpUtil;
import com.example.lenovo.coolweather.util.Utility;
import org.litepal.crud.DataSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* 遍历省市县数据的碎片
*/
public class ChooseAreaFragment extends Fragment {
public static final int LEVEL_PROVINCE = 0; //省标记
public static final int LEVEL_CITY = 1; //市标记
public static final int LEVEL_COUNTY = 2; //县标记
private ProgressDialog progressDialog; //进度条
private TextView titleText; //标题
private Button backButton; //返回按钮
private ListView listView;
private ArrayAdapter<String> adapter;
private List<String> dataList = new ArrayList<>();
private List<Province> provinceList; //省列表
private List<City> cityList; //市列表
private List<County> countyList; //县列表
private Province selectProvince; //选中的省份
private City selectCity; //选中的城市
private int currentLevel; //当前选中的级别
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container