全部代码https://gitee.com/Cutele/weather-forecast-v1
增加了发送短信等功能
主页面写的闪退了,就不记录了。
主要是借助适配器显示自己想要显示的页面。
城市管理
效果图
activity_city_manager.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".city_manager.CityManagerActivity" android:background="@mipmap/bg" > <RelativeLayout android:id="@+id/city_top_layout" android:background="#F5F5F5" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:id="@+id/city_iv_back" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@mipmap/icon_back" android:layout_marginLeft="10dp" ></ImageView> <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/city_iv_delete" android:src="@mipmap/icon_write" android:layout_alignParentRight="true" android:layout_marginRight="10dp" ></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/city_tv_title" android:text="城市管理" android:textSize="25dp" android:textStyle="bold" android:layout_centerInParent="true" ></TextView> </RelativeLayout> <ImageView android:id="@+id/city_line" android:layout_width="match_parent" android:layout_height="1dp" android:background="#DCDCDC" android:layout_below="@id/city_top_layout" ></ImageView> <ListView android:id="@+id/city_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/city_line" android:divider="@null" ></ListView> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/city_iv_add" android:src="@mipmap/icon_add" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_margin="30dp" android:padding="10dp" ></ImageView> </RelativeLayout>
item_city_manager.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <androidx.cardview.widget.CardView android:id="@+id/item_city_cv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" app:cardCornerRadius="10dp" app:cardElevation="5dp" app:contentPadding="10dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/item_city_tv_city" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="北京" android:textSize="22sp" android:textStyle="bold" /> <TextView android:id="@+id/item_city_tv_temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_alignParentRight="true" android:layout_marginRight="20dp" android:text="12℃"/> <ImageView android:id="@+id/item_city_line1" android:layout_width="match_parent" android:layout_height="2dp" android:layout_below="@id/item_city_tv_temp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="#808A87"/> <TextView android:id="@+id/item_city_tv_condition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_below="@id/item_city_line1" android:text="天气:晴"/> <TextView android:id="@+id/item_city_line2" android:layout_width="2dp" android:layout_height="20dp" android:layout_toRightOf="@id/item_city_tv_condition" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#808A87" android:layout_below="@id/item_city_line1"/> <TextView android:id="@+id/item_city_wind" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_toRightOf="@id/item_city_line2" android:layout_below="@id/item_city_line1" android:text="西南风2级"/> <TextView android:id="@+id/item_city_temprange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/item_city_line1" android:textSize="16sp" android:layout_alignParentRight="true" android:text="16~9℃" android:layout_marginRight="20dp"/> </RelativeLayout> </androidx.cardview.widget.CardView> </LinearLayout>
CityManagerActivity.java
package com.example.weather.city_manager; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import com.example.weather.R; import com.example.weather.db.DBManager; import com.example.weather.db.DatabaseBean; import java.util.ArrayList; import java.util.List; public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener { ImageView addIv,backIv,deleteIv; ListView cityLv; List<String> mDatas;//列表数据源 String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州", "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_city_manager); cityLv = findViewById(R.id.city_lv); mDatas = new ArrayList<>(); mDatas.add("北京"); mDatas.add("上海"); mDatas.add("广州"); mDatas.add("深圳"); cityLv.setAdapter(new CityManagerAdapter(this,mDatas)); } @Override public void onClick(View view) { } }
CityManagerAdapter.java
自定义适配器
package com.example.weather.city_manager; import android.content.Context; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.weather.R; import com.example.weather.db.DatabaseBean; import com.example.weather.juhe.JHTempBean; import com.google.gson.Gson; import java.util.List; public class CityManagerAdapter extends BaseAdapter { Context context; List<String> mDatas; public CityManagerAdapter(Context context, List<String> mDatas) { this.context = context; this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int i) { return mDatas.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; View convertView = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_city_manager,null); holder = new ViewHolder(convertView); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } String bean = mDatas.get(i); holder.cityTv.setText(bean); return convertView; } static class ViewHolder{ TextView cityTv,conTv,currentTempTv,windTv,tempRangeTv; public ViewHolder(View itemView){ cityTv = itemView.findViewById(R.id.item_city_tv_city); conTv = itemView.findViewById(R.id.item_city_tv_condition); currentTempTv = itemView.findViewById(R.id.item_city_tv_temp); windTv = itemView.findViewById(R.id.item_city_wind); tempRangeTv = itemView.findViewById(R.id.item_city_temprange); } } }
城市删除
效果图
activity_delete_city.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".city_manager.DeleteCityActivity" android:background="@mipmap/bg" > <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="#F5F5F5" android:id="@+id/delete_top_layout"> <ImageView android:id="@+id/delete_iv_error" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@mipmap/icon_error" android:layout_marginLeft="5dp"/> <ImageView android:id="@+id/delete_iv_right" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@mipmap/icon_right" android:layout_alignParentRight="true" android:layout_marginRight="5dp"/> <TextView android:id="@+id/delete_tv_title" android:text="城市管理" android:layout_centerInParent="true" android:textSize="18sp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:background="#DCDCDC"/> <ListView android:layout_below="@id/delete_top_layout" android:id="@+id/delete_lv" android:divider="@null" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </RelativeLayout>
item_deletecity.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp"> <androidx.cardview.widget.CardView android:id="@+id/item_delete_cv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" app:cardElevation="5dp" app:cardCornerRadius="10dp" app:contentPadding="20dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp"> <ImageView android:id="@+id/item_delete_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/icon_laji" android:layout_centerVertical="true" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/item_delete_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="北京" android:textSize="20sp" android:layout_toRightOf="@id/item_delete_iv" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:textStyle="bold"/> </RelativeLayout> </androidx.cardview.widget.CardView> </RelativeLayout>
DeleteCityActivity.java
package com.example.weather.city_manager; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import com.example.weather.R; import java.util.ArrayList; import java.util.List; public class DeleteCityActivity extends AppCompatActivity { ListView deleteLv; String[] hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州", "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"}; List<String> mDatas;//列表数据源 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_delete_city); deleteLv = findViewById(R.id.delete_lv);//找到组件 //传入数据 mDatas = new ArrayList<>(); mDatas.add("北京"); mDatas.add("上海"); mDatas.add("广州"); mDatas.add("深圳"); //设置适配器 deleteLv.setAdapter(new DeleteCityAdapter(this,mDatas)); } }
DeleteCityAdapter.java
package com.example.weather.city_manager; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.weather.R; import java.util.List; public class DeleteCityAdapter extends BaseAdapter { Context context; List<String> mDatas; List<String>deleteCitys; private LayoutInflater inflater; public DeleteCityAdapter(Context context, List<String> mDatas) { this.context = context; this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int i) { return mDatas.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { DeleteCityAdapter.ViewHolder holder = null; View convertView = null; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item_deletecity,null); holder = new DeleteCityAdapter.ViewHolder(convertView); convertView.setTag(holder); }else{ holder = (DeleteCityAdapter.ViewHolder) convertView.getTag(); } String bean = mDatas.get(i); holder.cityTv.setText(bean); return convertView; } class ViewHolder{ TextView cityTv,conTv,currentTempTv,windTv,tempRangeTv; public ViewHolder(View itemView){ cityTv = itemView.findViewById(R.id.item_delete_tv); } } }
城市搜索
效果图
activity_search_city.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="@mipmap/bg"> <ImageView android:id="@+id/search_iv_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginLeft="5dp" android:src="@mipmap/icon_search"/> <EditText android:id="@+id/search_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="3dp" android:layout_toLeftOf="@id/search_iv_submit" android:hint="搜索国内城市:" android:background="@drawable/searchbg"/> <TextView android:id="@+id/search_tv" android:layout_below="@id/search_et" android:layout_marginTop="20dp" android:textSize="16sp" android:textStyle="bold" android:text="热门城市" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <GridView android:id="@+id/search_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/search_tv" android:numColumns="5"></GridView> </RelativeLayout>
item_hotcity.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:gravity="center" android:text="北京" android:background="@drawable/frag_bg"> </TextView>
SearchCityActivity.java
package com.example.weather.city_manager; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.GridView; import android.widget.ImageView; import com.example.weather.R; public class SearchCityActivity extends AppCompatActivity implements View.OnClickListener{ EditText searchEt; ImageView submitIv; GridView searchGv; String[]hotCitys = {"北京","上海","广州","深圳","珠海","佛山","南京","苏州","厦门","长沙","成都","福州", "杭州","武汉","青岛","西安","太原","沈阳","重庆","天津","南宁"}; private ArrayAdapter<String> adapter; String city; @Override protected void onCreate(Bundle savedInstanceState) { //setContentView(R.layout.activity_search_city); super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_city); searchEt = findViewById(R.id.search_et); submitIv = findViewById(R.id.search_iv_submit); searchGv = findViewById(R.id.search_gv); submitIv.setOnClickListener(this); // 设置适配器 adapter = new ArrayAdapter<>(this, R.layout.item_hotcity, hotCitys); searchGv.setAdapter(adapter); // setListener(); } @Override public void onClick(View view) { } }