一、在Java代码中初始化界面
在MainActivity中初始化ViewPager界面
private void initPager() {
for (int i=0;i<cityList.size();i++){
WeatherFragment weatherFragment = new WeatherFragment();
Bundle bundle = new Bundle();
bundle.putString("city",cityList.get(i));
weatherFragment.setArguments(bundle);
fragmentList.add(weatherFragment);
}
FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager(),fragmentList);
showVp.setAdapter(fragmentPagerAdapter);
}
在WeatherFragment中获取从MainActivity中传入的城市
Bundle arguments = getArguments();
String city=arguments.getString("city");
getWeatherCity(city);
编写FragmentPagerAdapter用于fragment的显示
public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
List<Fragment> fragmentList;
public FragmentPagerAdapter(FragmentManager fragmentManager,List<Fragment> fragments) {
super(fragmentManager);
this.fragmentList=fragments;
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
二、子线程中开启网络请求
编写NetUtil类,在类中编写两个静态方法doGet和getWeatherOfCity
doGet方法用于从网络中获取数据,getWeatherOfCity方法用于拼接url之后,调用doGet方法传入url获取天气信息
public static final String URL_WEATHER="https://tianqiapi.com/api?unescape=1&version=v1&appid=22444194&appsecret=EG7XHDop";
public static String doGet(String urlString){
String result="";
String line;
StringBuilder stringBuilder=null;
BufferedReader bufferedReader=null;
HttpURLConnection connection=null;
InputStreamReader isr=null;
try {
URL url=new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
InputStream inputStream=connection.getInputStream();
isr=new InputStreamReader(inputStream);
bufferedReader=new BufferedReader(isr);
stringBuilder=new StringBuilder();
while ((line=bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
result=stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
connection.disconnect();
bufferedReader.close();
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String getWeatherOfCity(String city){
String url=URL_WEATHER+"&city="+city;
Log.i("Aye","URL:"+url);
Log.i("Aye","URLResult:"+doGet(url));
return doGet(url);
}
编写getWeatherCity方法,开启子线程,调用NetUtil类中的静态方法getWeatherOfCity来获取天气数据,并通过handler将数据传递给主线程
private void getWeatherCity(String selectCity) {
new Thread(new Runnable() {
@Override
public void run() {
String weatherOfCity=NetUtil.getWeatherOfCity(selectCity);
Message message=Message.obtain();
message.what=0;
message.obj=weatherOfCity;
handler.sendMessage(message);
}
}).start();
}
三、网络请求返回Json数据解析
使用在线工具解析Json数据并生成JavaBean导入包中
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zEJn14mR-1632214033044)(C:\Users\paranoia\AppData\Roaming\Typora\typora-user-images\image-20210915094807564.png)]
接收子线程传递的数据并使用gson解析
private Handler handler=new Handler(Looper.myLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
if (msg.what==0){
String weather= (String) msg.obj;
Log.i("Aye","主线程收到的天气数据:"+weather);
Gson gson=new Gson();
JsonRootBean jsonRootBean=gson.fromJson(weather, JsonRootBean.class);
updateWeather(jsonRootBean);
}
}
};
四、界面呈现
1. 获取天气数据并展示
编写updateWeather方法,用于获取数据并显示在界面上
private void updateWeather(JsonRootBean jsonRootBean) {
if (jsonRootBean!=null){
List<Data> dayWeather = jsonRootBean.getData();
Data todayWeather = dayWeather.get(0);
if (todayWeather!=null){
tempTv.setText(todayWeather.getTem1());
mainWeatherTv.setText(todayWeather.getWea());;
todayTv.setText("今天:"+todayWeather.getWea());
todayAirTv.setText("空气:"+todayWeather.getAir_level());
todayTempTv.setText(todayWeather.getTem()+"~"+todayWeather.getTem2());
todayIconIv.setImageResource(getImg(todayWeather.getWea_img()));
windTv.setText(todayWeather.getWin_speed());
humidityTv.setText(todayWeather.getHumidity());
pressureTv.setText(todayWeather.getPressure()+"hPa");
windTv1.setText(todayWeather.getWin().get(0));
sunriseTv.setText("日出:"+todayWeather.getSunrise());
sunsetTv.setText("日落:"+todayWeather.getSunset());
}
Data tomorrowWeather = dayWeather.get(1);
if (tomorrowWeather!=null){
tomorrowTv.setText("明天:"+tomorrowWeather.getWea());
tomorrowAirTv.setText("空气:"+tomorrowWeather.getAir_level());
tomorrowTempTv.setText(tomorrowWeather.getTem()+"~"+tomorrowWeather.getTem2());
tomorrowIconIv.setImageResource(getImg(tomorrowWeather.getWea_img()));
}
Data afterWeather = dayWeather.get(2);
if (afterWeather!=null){
afterTv.setText("后天:"+afterWeather.getWea());
afterAirTv.setText("空气:"+afterWeather.getAir_level());
afterTempTv.setText(afterWeather.getTem()+"~"+afterWeather.getTem2());
afterIconIv.setImageResource(getImg(afterWeather.getWea_img()));
}
List<Index> index = todayWeather.getIndex();
if (index!=null) {
UVTitle = index.get(0).getTitle();
UVLevel = index.get(0).getLevel();
UVDesc = index.get(0).getDesc();
clotheTitle = index.get(3).getTitle();
clotheLevel = index.get(3).getLevel();
clotheDesc = index.get(3).getDesc();
sportTitle=index.get(1).getTitle();
sportLevel=index.get(1).getLevel();
sportDesc=index.get(1).getDesc();
carTitle=index.get(4).getTitle();
carLevel=index.get(4).getLevel();
carDesc=index.get(4).getDesc();
sickTitle=index.get(2).getTitle();
sickLevel=index.get(2).getLevel();
sickDesc=index.get(2).getDesc();
airTitle=index.get(5).getTitle();
airLevel=index.get(5).getLevel();
airDesc=index.get(5).getDesc();
}
List<Hours> timeBean = todayWeather.getHours();
weatherAdapter=new WeatherAdapter(getActivity(),timeBean);
LinearLayoutManager manager=new LinearLayoutManager(getActivity(), RecyclerView.HORIZONTAL,false);
hoursRv.setAdapter(weatherAdapter);
hoursRv.setLayoutManager(manager);
}
}
2. 根据天气情况显示对应图片
编写getImg方法用于根据天气情况显示图片
private int getImg(String wea_img) {
int result = 0;
switch (wea_img) {
case "qing":
result=R.mipmap.sun;
break;
case "yin":
result=R.mipmap.yin;
break;
case "yu":
result=R.mipmap.yu;
break;
case "yun":
result=R.mipmap.yun;
break;
case "bingbao":
result=R.mipmap.bingbao;
break;
case "wu":
result=R.mipmap.wu;
break;
case "shachen":
result=R.mipmap.shachen;
break;
case "lei":
result=R.mipmap.lei;
break;
case "xue":
result=R.mipmap.xue;
break;
default:
result=R.mipmap.sun;
break;
}
return result;
}
3. 显示逐小时天气情况
编写WeatherAdapter,用于显示逐小时天气情况
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.WeatherViewHolder> {
private Context context;
private List<Hours> timeBean;
public WeatherAdapter(Context context, List<Hours> timeBean) {
this.context = context;
this.timeBean = timeBean;
}
@NonNull
@Override
public WeatherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.recylerview_item,parent,false);
WeatherViewHolder weatherViewHolder=new WeatherViewHolder(view);
return weatherViewHolder;
}
@Override
public void onBindViewHolder(@NonNull WeatherViewHolder holder, int position) {
Hours hoursBean = timeBean.get(position);
holder.timeTv.setText(hoursBean.getHours());
holder.timeWeatherTv.setText(hoursBean.getWea());
holder.timeTempTv.setText(hoursBean.getTem());
holder.timeWindTv.setText(hoursBean.getWin()+" "+hoursBean.getWin_speed());
}
@Override
public int getItemCount() {
return timeBean.size();
}
class WeatherViewHolder extends RecyclerView.ViewHolder {
TextView timeTv;
TextView timeTempTv;
TextView timeWeatherTv;
TextView timeWindTv;
public WeatherViewHolder(@NonNull View itemView) {
super(itemView);
timeTv = itemView.findViewById(R.id.timeTv);
timeTempTv = itemView.findViewById(R.id.timeTempTv);
timeWeatherTv = itemView.findViewById(R.id.timeWeatherTv);
timeWindTv = itemView.findViewById(R.id.timeWindTv);
}
}
}
4. 点击事件监听器
设置点击事件,用于显示各种指数的详细信息和跳转浏览器查看更多天气
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.webTv:
Uri uri=Uri.parse("https://tianqi.qq.com/index.htm");
Intent intent=new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(uri);
startActivity(intent);
break;
case R.id.clotheIv:
showAlertDialog(clotheTitle,clotheLevel,clotheDesc);
break;
case R.id.UVIv:
showAlertDialog(UVTitle,UVLevel,UVDesc);
break;
case R.id.sportIv:
showAlertDialog(sportTitle,sportLevel,sportDesc);
break;
case R.id.carIv:
showAlertDialog(carTitle,carLevel,carDesc);
break;
case R.id.airPollutionIv:
showAlertDialog(airTitle,airLevel,airDesc);
break;
case R.id.sickIv:
showAlertDialog(sickTitle,sickLevel,sickDesc);
break;
}
}
编写showAlertDialog方法,用于显示对话框
private void showAlertDialog(String title, String level, String desc) {
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
builder.setTitle(title).setMessage("\n"+level+"\n\n"+desc).create().show();
}
五、数据库创建和功能实现
编写CityDBHelper,继承SQLiteOpenHelper并重写方法
private static SQLiteDatabase db;
ContentValues values=new ContentValues();
private long flag=0;
public CityDBHelper(@Nullable Context context) {
super(context, "city.db", null, 1);
db=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table city(id integer primary key autoincrement,city varchar(20) unique not null)";
db.execSQL(sql);
}
public boolean addCity(String city){
values.put("city",city);
flag=db.insert("city",null,values);
return flag>0?true:false;
}
public boolean deleteCity(String city){
flag=db.delete("city","city=?",new String[]{city});
return flag>0?true:false;
}
public List<String> queryCity(){
List<String> cityList=new ArrayList<>();
Cursor cursor=db.query("city",null,null,null,null,null,null);
if (cursor!=null){
while (cursor.moveToNext()){
String city=cursor.getString(1);
cityList.add(city);
}
}
return cityList;
}
public boolean findCity(String city) {
Cursor cursor = db.query("city", null, "city=?", new String[]{city}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String findCity = cursor.getString(1);
Log.i("Aye",findCity+"findCity");
if (findCity != null) {
return false;
} else {
return true;
}
}
}
return true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
六、城市管理界面功能实现
1. listView适配器配置
编写CityAdapter继承自BaseAdapter,为listView适配器
private Context context;
private List<String> cityList;
public CityAdapter(Context context, List<String> cityList) {
this.context = context;
this.cityList = cityList;
}
@Override
public int getCount() {
return cityList.size();
}
@Override
public Object getItem(int position) {
return cityList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder=null;
if (convertView==null){
viewHolder=new ViewHolder();
convertView=View.inflate(context, R.layout.city_listview_item,null);
viewHolder.cityNameTv=convertView.findViewById(R.id.cityNameTv);
convertView.setTag(viewHolder);
}else {
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.cityNameTv.setText(cityList.get(position));
return convertView;
}
class ViewHolder{
TextView cityNameTv;
}
4. 控件监听事件
为界面控件添加监听事件,用于跳转到添加城市界面和删除城市
findTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(CityActivity.this,AddActivity.class));
}
});
showLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String deleteCity=cityList.get(position);
AlertDialog.Builder builder= new AlertDialog.Builder(CityActivity.this);
builder.setTitle("提示").setMessage("是否删除该城市天气信息?")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
flag=cityDBHelper.deleteCity(deleteCity);
if (flag){
Toast.makeText(CityActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
initView();
Intent intent=new Intent("UPDATE1");
sendBroadcast(intent);
}else {
Toast.makeText(CityActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
}
}
})
.create().show();
return true;
}
});
七、添加城市界面功能实现
1. 适配器配置、通过省会添加城市功能实现
private Context context;
private List<String> cityList;
private CityDBHelper cityDBHelper;
public AddAdapter(Context context, List<String> cityList,CityDBHelper cityDBHelper) {
this.context = context;
this.cityList = cityList;
this.cityDBHelper=cityDBHelper;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=View.inflate(context,R.layout.add_recylerview_item,null);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.cityTv.setText(cityList.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String clickCity=cityList.get(position);
if (clickCity!=null){
if (cityDBHelper.findCity(clickCity)) {
if (cityDBHelper.addCity(clickCity)) {
Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent("UPDATE");
context.sendBroadcast(intent);
}else {
Toast.makeText(context,"添加失败",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(context,"已存在该城市",Toast.LENGTH_SHORT).show();
}
}
}
});
}
@Override
public int getItemCount() {
return cityList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView cityTv;
public ViewHolder(@NonNull View itemView) {
super(itemView);
cityTv=ite
mView.findViewById(R.id.itemCityTv);
}
}
适配器绑定,数据传递
private String[] cityStrings=new String[]{"北京","天津","哈尔滨","沈阳","石家庄", "兰州", "西安", "郑州", "太原", "长沙", "南京", "贵阳", "杭州", "广州", "台北",
"上海" , "重庆", "长春", "呼和浩特", "乌鲁木齐", "西宁", "银川", "济南", "合肥", "武汉", "成都", "拉萨", "昆明", "南昌", "福州", "海口", "澳门"};
cityList=new ArrayList<>();
for (int i=0;i<cityStrings.length;i++){
cityList.add(cityStrings[i]);
}
addAdapter=new AddAdapter(this,cityList,cityDBHelper);
cityRv.setLayoutManager(new GridLayoutManager(this,3));
cityRv.setAdapter(addAdapter);
4. 通过搜索添加城市功能实现
搜索键监听事件
findTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String findCity=findEt.getText().toString();
if (findCity!=null){
if(cityDBHelper.findCity(findCity)){
if (cityDBHelper.addCity(findCity)) {
Toast.makeText(AddActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent("UPDATE");
sendBroadcast(intent);
}else {
Toast.makeText(AddActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(AddActivity.this,"已存在该城市",Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(AddActivity.this,"输入城市为空",Toast.LENGTH_SHORT).show();
}
}
});
5. 通过广播接收城市信息更新消息
在CityActivity文件中创建广播接收者
@Override
protected void onCreate(Bundle savedInstanceState) {
myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("UPDATE");
registerReceiver(myReceiver,intentFilter);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
initView();
}
}
在MainActivity中也创建广播接收者
@Override
protected void onCreate(Bundle savedInstanceState) {
myReceiver=new MyReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("UPDATE");
intentFilter.addAction("UPDATE1");
registerReceiver(myReceiver,intentFilter);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
initPager();/收到广播信息后,更新界面
}
}
|