美文网首页
酷欧天气2.0(三)——查看已添加城市WeatherAddAct

酷欧天气2.0(三)——查看已添加城市WeatherAddAct

作者: 麦香菌 | 来源:发表于2018-03-05 16:11 被阅读0次

    代码

    一、准备工作:
    1、LinkedList存储已添加的控件,制成索引,可配合数据库删除数据。

    private LinkedList<Button> ListBtn_Add;
    private LinkedList<TextView> ListText_Def;
    

    2、获取手机屏幕的宽高,用于设置控件的宽高。

    DisplayMetrics dm = new DisplayMetrics();
    dm = getResources().getDisplayMetrics();
    width=dm.widthPixels;
    height=dm.heightPixels;
    

    3、用EDITSTATE判断是否可以删除控件和数据,为0时不能删除,为1时则相反

    private int EDITSTATE=0
    

    4、因为不论是跳入WeatherAddActivity还是从ChooseAreaActivity退回到该Activity,都要根据数据库里的城市数据动态添加控件,所以我把程序都挪到了onStart()中写。


    图片来自网络.png

    二、onStart()
    1、清空LinkedList的所有数据,并删除所有动态添加的控件,再根据数据库添加控件

    ListText_Def.clear();
    ListBtn_Add.clear();
    linearLayout.removeAllViews();
    List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
    for(County_Addition countyAddition:countyAdditions){//动态添加数据库中所有数据
            addBtn(countyAddition.getWeatherName(),countyAddition.getDef());
    }
    

    2、动态添加控件:

    private void addBtn(String countyName, Boolean Def_flag){
            //添加外部Layout
            LinearLayout linear_btn = new LinearLayout(this);
            linear_btn.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams liParam = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            linear_btn.setLayoutParams(liParam);
            //添加按钮
            Button btnAdd=new Button(this);
            LinearLayout.LayoutParams btnAddPar=new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, height/8,3);
            btnAddPar.setMargins(0,10,5,10);
            btnAdd.setLayoutParams(btnAddPar);
            btnAdd.setText(countyName);
            btnAdd.setTextColor(Color.argb(255, 255, 255, 255));
            btnAdd.setBackgroundColor(Color.argb(136, 0, 0, 0));
            btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(EDITSTATE==1)
                        delBtn(view);
                }
            });
            linear_btn.addView(btnAdd);
            ListBtn_Add.add(btnAdd);
            //添加TextView
            TextView textDef=new TextView(this);
            LinearLayout.LayoutParams textDefPar = new LinearLayout.LayoutParams
                    (ViewGroup.LayoutParams.WRAP_CONTENT, height/8, 1);
            textDefPar.setMargins(0, 10, 0, 10);
            textDef.setLayoutParams(textDefPar);
            if(Def_flag==true){
                textDef.setBackgroundColor(Color.argb(255, 171, 52, 56));
                textDef.setText("默认");
            }else if(Def_flag==false){
                textDef.setText("设为默认");
                textDef.setBackgroundColor(Color.argb(136, 0, 0, 0));
            }
            textDef.setGravity(Gravity.CENTER);
            textDef.setTextColor(Color.argb(255, 255, 255, 255));
            linear_btn.addView(textDef);
            textDef.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int k=0;
                    for(int i=0;i<ListText_Def.size();i++){
                        ListText_Def.get(i).setBackgroundColor(Color.argb(136, 0, 0, 0));
                        ListText_Def.get(i).setText("设为默认");
                        if(ListText_Def.get(i)==view){
                            view.setBackgroundColor(Color.argb(255, 171, 52, 56));
                            ListText_Def.get(i).setText("默认");
                            k=i;
                        }
                    }
                    County_Addition countyAddition=new County_Addition();
                    countyAddition.setToDefault("Def");
                    countyAddition.updateAll();
                    County_Addition countyAddition1=new County_Addition();
                    countyAddition1.setDef(true);
                    countyAddition1.updateAll("WeatherName = ?",ListBtn_Add.get(k).getText().toString());
                }
            });
            ListText_Def.add(textDef);
            linearLayout.addView(linear_btn);
        }
    

    3、动态删除控件和数据:

    private void delBtn(View view){
            int countyid;
            Boolean flag=false;
            if(view==null){
                return;
            }
            int iIndex=-1,position=0;
            for(int i=0;i<ListBtn_Add.size();i++){
                if(ListBtn_Add.get(i)==view){
                    iIndex=i;
                    break;
                }
            }
            if(iIndex>=0){
                List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
                for(County_Addition countyAddition:countyAdditions){
                    if(position==iIndex){
                        countyid=countyAddition.getId();
                        Log.d("TAG",countyid+"");
                        if(countyAddition.getDef()==true){
                            flag=true;
                        }
                        DataSupport.delete(County_Addition.class,countyid);
                        break;
                    }
                    position++;
                }
                ListBtn_Add.remove(iIndex);
                ListText_Def.remove(iIndex);
                Log.d("TAG",!ListBtn_Add.isEmpty()+"");
                if(flag&&(!ListBtn_Add.isEmpty())){
                    Log.d("TAG","First");
                    ListText_Def.get(0).setText("默认");
                    ListText_Def.get(0).setBackgroundColor(Color.argb(255, 171, 52, 56));
                    County_Addition countyAddition=new County_Addition();
                    countyAddition.setDef(true);
                    countyAddition.updateAll("WeatherName = ?",ListBtn_Add.get(0).getText().toString());
                }
                linearLayout.removeViewAt(iIndex);
            }
        }
    

    3、点击事件:

    btn_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(EDITSTATE==0){
                        btn_edit.setText("确定");
                        EDITSTATE=1;
                    }else if(EDITSTATE==1){
                        btn_edit.setText("编辑");
                        EDITSTATE=0;
                    }
                }
            });
    btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(WeatherAddActivity.this,ChooseAreaActivity.class);
                    SharedPreferences.Editor editor= PreferenceManager.
                            getDefaultSharedPreferences(WeatherAddActivity.this).edit();
                    editor.putBoolean("Where",true);
                    editor.apply();
                    linearLayout.removeAllViews();
                    startActivity(intent);
                }
            });
    btn_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //判断数据库的数据是否为空
                    List<County_Addition> countyAdditions= DataSupport.findAll(County_Addition.class);
                    for(County_Addition countyAddition:countyAdditions){
                        number++;
                    }
                    if(number==0){
                        Toast.makeText(WeatherAddActivity.this,"城市不能为空,请添加城市",Toast.LENGTH_SHORT).show();
                    }else{
                        number=0;
                        finish();
                    }
                }
            });
    

    NEXT:
    酷欧天气2.0(四)——显示城市天气数据WeatherActivity
    https://www.jianshu.com/p/62a96eab333f

    相关文章

      网友评论

          本文标题:酷欧天气2.0(三)——查看已添加城市WeatherAddAct

          本文链接:https://www.haomeiwen.com/subject/khpqfftx.html