酷欧天气2.0(二)——添加城市ChooseAreaActivity

代码

先是获取数据和解析数据的数据库和代码,这些书上已经有了就不赘言
了。

ChooseAreaActivity:

一、在ChooseAreaActivity开头要先进行两个判断:

1、判断这个Activity从何而来:当数据库里没有城市信息,即用户第一次用这个APP时会先进入这个Activity,而通过WeatherAddActivity添加城市时也会跳入这个Activity。

SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
flag=prefs.getBoolean("Where",false);

2、判断数据库里有多少行数据:

List countyAdditions=DataSupport.findAll(County_Addition.class);
for(County_Addition countyAddition:countyAdditions){
            number++;
}

二、接着根据这两个判断执行不同代码:

1、(flag==false)&&(number>0):数据库中已有城市数据,说明用户非第一次使用这个APP,直接跳过该Activity。

if((flag==false)&&(number>0)){
            Intent intent1=new Intent(ChooseAreaActivity.this,WeatherActivity.class);
            startActivity(intent1);
            finish();
}

除了上面这个情况外,接下来都会进入ListView的点击事件。

2、number==0:说明数据库为空,先存储你点击的城市信息。
2.1、flag==true:说明是从WeatherAddActivity进入该Activity的,直接finish();
2.2、flag==false:说明用户第一次使用该APP。

if(number==0){
            countySave.setDef(true);
            countySave.setCountyWeatherId(countyList.get(position).getWeatherId());
            countySave.setWeatherName(countyList.get(position).getCountyName());
            countySave.setCountyWeather("");
            countySave.save();
            if(flag==true)
                         finish();
            else if(flag==false) {
                         Intent intent3 = new Intent(ChooseAreaActivity.this, WeatherActivity.class);
                         startActivity(intent3);
                         finish();
            }
}

3、number!=0:是从WeatherAddActivity跳到该Activity的。
3.1、点击的城市已在数据库中:

Boolean flag2=true;
List countyAdditions=DataSupport.findAll(County_Addition.class);
for(County_Addition countyAddition:countyAdditions){
if(countyAddition.getWeatherName().equals(countyList.get(position).getCountyName())){
            Toast.makeText(ChooseAreaActivity.this,"已添加该城市",Toast.LENGTH_SHORT).show();
            flag2=false;
            }
}

3.2、点击的城市未在数据库中:存储点击的城市数据,并退回到WeatherAddActivity。

if(flag2){
            countySave.setDef(false);
            countySave.setCountyWeatherId(countyList.get(position).getWeatherId());
            countySave.setWeatherName(countyList.get(position).getCountyName());
            countySave.setCountyWeather("");
            countySave.save();
            finish();
}

4、已被添加的城市在ListView中的显示方式:

countyList=DataSupport.where("cityId=?",String.valueOf(selectedCity.getId())).find(County.class);
if(countyList.size()>0){
            dataList.clear();
            Boolean flag=false;
            for(County county:countyList){
                List countyAdditions=DataSupport.findAll(County_Addition.class);
                for(County_Addition countyAddition:countyAdditions){
                    if(countyAddition.getWeatherName().equals(county.getCountyName())){
                        flag=true;
                        break;
                    }
                }
                if(flag){
                    dataList.add("√|"+county.getCountyName());
                    flag=false;
                } else
                    dataList.add(county.getCountyName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
}

前面只写了修改的部分,最后献上ChooseAreaActivity的所有代码,有些乱。

public class ChooseAreaActivity extends AppCompatActivity {
    public static final int LEVEL_PROVINCE=0;
    public static final int LEVEL_CITY=1;
    public static final int LEVEL_COUNTY=2;
    private int number=0;
    private ProgressDialog progressDialog;
    private TextView title;
    private Button btn_back;
    private ListView listView;
    private ArrayAdapter adapter;
    private List dataList=new ArrayList<>();
    private List provinceList;
    private List cityList;
    private List countyList;
    private Province selectedProvince;
    private City selectedCity;
    private int currentLevel;
    private County_Addition county_addition;
    private Boolean flag=false;

    @Nullable
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_area);
        inited();
        //判断是否是从WeatherAddActivity跳过来的
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        flag=prefs.getBoolean("Where",false);

        //判断城市添加表里有多少组数据
        List countyAdditions=DataSupport.findAll(County_Addition.class);
        Log.d("TAG","number:"+number+"");
        for(County_Addition countyAddition:countyAdditions){
            number++;
        }
        Log.d("ChooseAreaActivity",number+"");
        Log.d("ChooseAreaActivity",flag+"");

        if((flag==false)&&(number>0)){
            Intent intent1=new Intent(ChooseAreaActivity.this,WeatherActivity.class);
            startActivity(intent1);
            finish();
        }
        SharedPreferences.Editor editor= PreferenceManager.
                getDefaultSharedPreferences(ChooseAreaActivity.this).edit();
        editor.putBoolean("Where",false);
        editor.apply();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView 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();
                }else if(currentLevel==LEVEL_COUNTY){
                    County_Addition countySave=new County_Addition();
                    Log.d("TAG","number:"+number+"");
                    if(number==0){
                        countySave.setDef(true);
                        countySave.setCountyWeatherId(countyList.get(position).getWeatherId());
                        countySave.setWeatherName(countyList.get(position).getCountyName());
                        countySave.setCountyWeather("");
                        countySave.save();
                        if(flag==true)
                            finish();
                        else if(flag==false) {
                            Intent intent3 = new Intent(ChooseAreaActivity.this, WeatherActivity.class);
                            startActivity(intent3);
                            finish();
                        }
                    }else{
                        Boolean flag2=true;
                        List countyAdditions=DataSupport.findAll(County_Addition.class);
                        for(County_Addition countyAddition:countyAdditions){
                            Log.d("Choose",countyList.get(position).getCountyName());
                            Log.d("Choose",countyAddition.getWeatherName());
                            if(countyAddition.getWeatherName().equals(countyList.get(position).getCountyName())){
                                Toast.makeText(ChooseAreaActivity.this,"已添加该城市",Toast.LENGTH_SHORT).show();
                                flag2=false;
                            }
                            Log.d("Choose",flag2+"");
                        }
                        if(flag2){
                            countySave.setDef(false);
                            countySave.setCountyWeatherId(countyList.get(position).getWeatherId());
                            countySave.setWeatherName(countyList.get(position).getCountyName());
                            countySave.setCountyWeather("");
                            countySave.save();
                            finish();
                        }
                    }

                }
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                if(currentLevel==LEVEL_COUNTY){
                    queryCities();
                }
                if(currentLevel==LEVEL_CITY){
                    queryProvinces();
                }
            }
        });
        queryProvinces();
    }
    private void inited(){
        title=(TextView)findViewById(R.id.text_title);
        btn_back=(Button)findViewById(R.id.btn_back);
        listView=(ListView)findViewById(R.id.listView);
        adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,dataList);
        listView.setAdapter(adapter);
        county_addition=new County_Addition();
    }
    private void queryProvinces(){
        title.setText("中国");
        btn_back.setVisibility(View.GONE);
        provinceList= DataSupport.findAll(Province.class);
        if(provinceList.size()>0){
            dataList.clear();
            for(Province province:provinceList){
                dataList.add(province.getProvinceName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            currentLevel=LEVEL_PROVINCE;
        }else{
            String address="http://guolin.tech/api/china";
            queryFromServer(address,"province");
        }
    }
    private void queryCities(){
        title.setText(selectedProvince.getProvinceName());
        btn_back.setVisibility(View.VISIBLE);
        cityList=DataSupport.where("provinceId=?",String.valueOf(selectedProvince.getId())).find(City.class);
        if(cityList.size()>0){
            dataList.clear();
            for(City city:cityList){
                dataList.add(city.getCityName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            currentLevel=LEVEL_CITY;
        }else{
            int provinceCode=selectedProvince.getProvinceCode();
            String address="http://guolin.tech/api/china/"+provinceCode;
            queryFromServer(address,"city");
        }
    }
    private void queryCounties(){
        title.setText(selectedCity.getCityName());
        btn_back.setVisibility(View.VISIBLE);
        countyList=DataSupport.where("cityId=?",String.valueOf(selectedCity.getId())).find(County.class);
        if(countyList.size()>0){
            dataList.clear();
            Boolean flag=false;
            for(County county:countyList){
                List countyAdditions=DataSupport.findAll(County_Addition.class);
                for(County_Addition countyAddition:countyAdditions){
                    if(countyAddition.getWeatherName().equals(county.getCountyName())){
                        flag=true;
                        break;
                    }
                }
                if(flag){
                    dataList.add("√|"+county.getCountyName());
                    flag=false;
                } else
                    dataList.add(county.getCountyName());
            }
            adapter.notifyDataSetChanged();
            listView.setSelection(0);
            currentLevel=LEVEL_COUNTY;
        }else{
            int provinceCode=selectedProvince.getProvinceCode();
            int cityCode=selectedCity.getCityCode();
            String address="http://guolin.tech/api/china/"+provinceCode+"/"+cityCode;
            queryFromServer(address,"county");
        }
    }
    private void queryFromServer(String address, final String type){
        showProgressDialog();
        HttpUtil.sendOkHttpRequest(address, new Callback() {
            public void onResponse(Call call, Response response) throws IOException {
                String responseText=response.body().string();
                boolean result=false;
                if("province".equals(type)){
                    result= Utility.handleProvinceResponse(responseText);
                }else if("city".equals(type)){
                    result=Utility.handleCityResponse(responseText,selectedProvince.getId());
                }else if("county".equals(type)){
                    result=Utility.handleCountyResponse(responseText,selectedCity.getId());
                }
                if(result){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            closeProgressDialog();
                            if("province".equals(type)){
                                queryProvinces();
                            }else if("city".equals(type)){
                                queryCities();
                            }else if("county".equals(type)){
                                queryCounties();
                            }
                        }
                    });
                }
            }
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        closeProgressDialog();
                        Toast.makeText(ChooseAreaActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    private void showProgressDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(ChooseAreaActivity.this);
            progressDialog.setMessage("正在加载...");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }
    private void closeProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }
}

NEXT:
酷欧天气2.0(三)——查看已添加城市WeatherAddActivity
https://www.jianshu.com/p/986559f4f8bf

你可能感兴趣的:(酷欧天气2.0(二)——添加城市ChooseAreaActivity)