package com. Example. Administrator. Coolweather;
The import android. App. ProgressDialog;
The import android. OS. Bundle;
The import android. Support. The v4. App. Fragments;
The import android. View. LayoutInflater;
The import android. View. The view;
The import android. View. ViewGroup;
The import android. Widget. AdapterView;
The import android. Widget. ArrayAdapter;
The import android. Widget. The Button;
The import android. Widget. ListView.
The import android. Widget. TextView;
The import android. Widget. Toast;
The import com. Example. Administrator. Coolweather. The City;
The import com. Example. Administrator. Coolweather. The County;
The import com. Example. Administrator. Coolweather. The Province;
The import com. Example. Administrator. Coolweather. Util. HttpUtil;
The import com. Example. Administrator. Coolweather. Util. The Utility;
The import org. Litepal. Crud. DataSupport;
Import the Java. IO. IOException;
Import the Java. Util. ArrayList;
import java.util.List;
The import okhttp3. Call;
The import okhttp3. Callback;
The import okhttp3. The Response;
/* *
* Created by Administrator on 2018/4/24.
*/
Public class ChooseAreaFragment extends fragments {
Public static final ints LEVEL_PROVINCE=0;
Public static final ints LEVEL_CITY=1;
Public static final ints LEVEL_COUNTY=2;
Private ProgressDialog ProgressDialog.
Private TextView titleText;
Private Button backButton;
Private ListView ListView.
Private ArrayAdapter Adapter.
Private List The dataList=new ArrayList<> (a);
Private List ProvinceList;
Private List CityList;
Private List CountyList;
Private Province selectedProvince;
Private City selectedCity;
//the currently selected level
Private int currentLevel;
@ Override
Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View the View=inflater. Inflate (R.l ayout. Choose_area, container, false);
TitleText=(TextView) view. The findViewById (R.i which itle_text);
BackButton=(Button) view. The findViewById (R.i db ack_button);
ListView=(listView) view. The findViewById (R.i d.l ist_view);
Adapter=new ArrayAdapter<> (getContext (), the android R.l ayout. Simple_list_item_1, dataList);
ListView. SetAdapter (adapter);
Return the view;
}
@ Override
Public void onActivityCreated (Bundle savedInstanceState) {
Super. OnActivityCreated (savedInstanceState);
ListView. SetOnItemClickListener (new AdapterView. OnItemClickListener () {
@ Override
Public void onItemClick (AdapterView<?> The parent, View View, int position, long id) {
If (currentLevel==LEVEL_PROVINCE) {
SelectedProvince=provinceList. Get (position);
QueryCities ();
{} else if (currentLevel==LEVEL_CITY)
SelectedCity=cityList. Get (position);
QueryCounties ();
}
}
});
BackButton. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
If (currentLevel==LEVEL_COUNTY) {
QueryCities ();
{} else if (currentLevel==LEVEL_CITY)
QueryProvinces ();
}
}
});
QueryProvinces ();
}
/*
Query all across the country, province, first from the database query, if it weren't for the query to query on the server
In a ListView in
*/
Private void queryProvinces () {
TitleText. SetText (" China ");
BackButton. SetVisibility (View. GONE);
ProvinceList=DataSupport. The.findall (Province. Class);
If (provinceList. The size () & gt; 0 {
DataList. The clear ();
For (Province Province: provinceList) {
DataList. Add (province. GetProvinceName () + "" + province. GetProvinceCode () +" "+ province. The getId ());
}
Adapter. NotifyDataSetChanged ();
ListView. SetSelection (0);
CurrentLevel=LEVEL_PROVINCE;
} else {
String address="http://guolin.tech/api/china";
QueryFromServer (address, "the province");
}
}
/*
Query to select all of the city in the province, the first from the database query, if there is no query to query to the server
In a ListView in
*/
Private void queryCities () {
TitleText. SetText (selectedProvince getProvinceName ());
BackButton. SetVisibility (the VISIBLE);
CityList=DataSupport. Where (" provinceid=?" String. The valueOf (selectedProvince getId ())). The find (City. Class);
If (cityList. The size () & gt; 0 {
DataList. The clear ();
For (City City: cityList) {
DataList. Add (city. GetCityName () + "" + city. GetCityCode () +" "+ city. GetProvinceId ());
}
Adapter. NotifyDataSetChanged ();
ListView. SetSelection (0);
CurrentLevel=LEVEL_CITY;
} else {
Int provinceCode=selectedProvince. GetProvinceCode ();
String address="http://guolin.tech/api/china/" + provinceCode;
QueryFromServer (address, city, "");
}
}
/*
Query of all counties in the city, the first from a database query, if it weren't for the query to query on the server
In a ListView in
*/
Private void queryCounties () {
TitleText. SetText (selectedCity getCityName ());
BackButton. SetVisibility (the VISIBLE);
CountyList=DataSupport. Where (" cityid=?" nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull