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

代码

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

private LinkedList

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()中写。


酷欧天气2.0(三)——查看已添加城市WeatherAddActivity_第1张图片
图片来自网络.png

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

ListText_Def.clear();
ListBtn_Add.clear();
linearLayout.removeAllViews();
List 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

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=0){
            List 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 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(三)——查看已添加城市WeatherAddActivity)