Android Studio 实现天气预报App (简单方便展示内容超多) ...

打印 上一主题 下一主题

主题 537|帖子 537|积分 1611

高质量源码(非开源)
   
关注公众号《编程乐学》

   
背景复兴:24011501

  
目录
媒介
一、使命先容
1.1 背景
1.2目的和意义
二、 实现先容
视频演示
2.1 启动页实现
2.2注册页面实现
2.3 登岸页面实现
2.4 首页实现
2.5 都会管理列表页面实现               
三、获取源码


媒介

        在使用Android Studio开发天气预报应用步伐时,首先必要考虑提供给用户的功能和用户体验。这包罗设计直观友好的用户界面,与实时天气数据的交互以及用户权限管理。本文将领导读者使用Java语言和Android Studio创建一个天气预报应用步伐的初步框架,并先容如何获取实时天气数据以及在应用步伐中显现这些数据的方法。通过本文,读者将学习如何创建一个功能美满的天气预报应用步伐,并为用户提供实时正确的天气信息。
一、使命先容

1.1 背景

        随着移动应用步伐的普及,天气预报应用步伐成为人们一样平常生活中不可或缺的一部分。无论是旅行、户外运动还是一样平常通勤,正确的天气信息对人们的生活有偏重要影响。因此,开发一款用户友好、功能美满的天气预报应用步伐具有重要意义。
1.2目的和意义

        本文旨在指导读者使用Android Studio和Java语言开发一款天气预报应用步伐,从而使用户能够方便获取实时的天气信息。该应用步伐将资助用户做出更为正确的出行决策,进步一样平常生活中对天气变化的应对能力。另外,通过学习本文所先容的应用步伐开发过程,读者能够把握与API的交互、数据处理和用户界面设计等关键技能,为未来的移动应用开发奠定坚实的基础。
二、 实现先容

视频演示

     Android studio期末设计大作业~天气预报App
  2.1 启动页实现


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:background="#ffffff"
  8.     tools:context=".ui.StartActivity">
  9.     <ImageView
  10.         android:id="@+id/imageView"
  11.         android:layout_width="0dp"
  12.         android:layout_height="wrap_content"
  13.         app:layout_constraintBottom_toBottomOf="parent"
  14.         app:layout_constraintEnd_toEndOf="parent"
  15.         app:layout_constraintStart_toStartOf="parent"
  16.         app:layout_constraintTop_toTopOf="parent"
  17.         app:srcCompat="@drawable/logo" />
  18. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
2.2注册页面实现



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:background="#ffffff"
  8.     tools:context=".ui.RegisterActivity">
  9.     <ImageView
  10.         android:id="@+id/imageView5"
  11.         android:layout_width="0dp"
  12.         android:layout_height="230dp"
  13.         app:layout_constraintEnd_toEndOf="parent"
  14.         app:layout_constraintStart_toStartOf="parent"
  15.         app:layout_constraintTop_toTopOf="parent"
  16.         app:srcCompat="@drawable/logo" />
  17.     <TextView
  18.         android:id="@+id/tv_login"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_marginTop="24dp"
  22.         android:text="已有帐号,立即登陆!"
  23.         android:textColor="#00b5ff"
  24.         android:textStyle="bold"
  25.         app:layout_constraintEnd_toEndOf="@+id/register_button"
  26.         app:layout_constraintHorizontal_bias="0.429"
  27.         app:layout_constraintStart_toStartOf="@+id/register_button"
  28.         app:layout_constraintTop_toBottomOf="@+id/register_button" />
  29.     <Button
  30.         android:id="@+id/register_button"
  31.         android:layout_width="0dp"
  32.         android:layout_height="40dp"
  33.         android:layout_marginTop="32dp"
  34.         android:background="@drawable/login"
  35.         android:text="立 即 注 册 "
  36.         android:textColor="#fff"
  37.         android:textSize="16sp"
  38.         app:layout_constraintEnd_toEndOf="@+id/linearLayout"
  39.         app:layout_constraintHorizontal_bias="0.0"
  40.         app:layout_constraintStart_toStartOf="@+id/linearLayout"
  41.         app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
  42.     <LinearLayout
  43.         android:id="@+id/linearLayout"
  44.         android:layout_width="0dp"
  45.         android:layout_height="wrap_content"
  46.         android:layout_marginStart="24dp"
  47.         android:layout_marginTop="8dp"
  48.         android:layout_marginEnd="24dp"
  49.         android:orientation="vertical"
  50.         app:layout_constraintEnd_toEndOf="parent"
  51.         app:layout_constraintStart_toStartOf="parent"
  52.         app:layout_constraintTop_toBottomOf="@+id/imageView5">
  53.         <TextView
  54.             android:id="@+id/textView2"
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:text="用户名"
  58.             android:textColor="#000000"
  59.             android:textSize="14sp"
  60.             android:textStyle="bold" />
  61.         <EditText
  62.             android:id="@+id/username_edittext"
  63.             android:layout_width="match_parent"
  64.             android:layout_height="wrap_content"
  65.             android:layout_marginTop="10dp"
  66.             android:ems="10"
  67.             android:hint="请输入用户名"
  68.             android:inputType="textPersonName" />
  69.         <TextView
  70.             android:id="@+id/textView3"
  71.             android:layout_width="match_parent"
  72.             android:layout_height="wrap_content"
  73.             android:layout_marginTop="20dp"
  74.             android:text="密码"
  75.             android:textColor="#000000"
  76.             android:textSize="14sp"
  77.             android:textStyle="bold" />
  78.         <EditText
  79.             android:id="@+id/password_edittext"
  80.             android:layout_width="match_parent"
  81.             android:layout_height="wrap_content"
  82.             android:layout_marginTop="10dp"
  83.             android:ems="10"
  84.             android:hint="请输入用户密码"
  85.             android:inputType="textPassword" />
  86.         <TextView
  87.             android:id="@+id/textView4"
  88.             android:layout_width="match_parent"
  89.             android:layout_height="wrap_content"
  90.             android:layout_marginTop="20dp"
  91.             android:text="重复密码"
  92.             android:textColor="#000000"
  93.             android:textSize="14sp"
  94.             android:textStyle="bold" />
  95.         <EditText
  96.             android:id="@+id/repassword"
  97.             android:layout_width="match_parent"
  98.             android:layout_height="wrap_content"
  99.             android:layout_marginTop="10dp"
  100.             android:ems="10"
  101.             android:hint="请再次输入用户密码"
  102.             android:inputType="textPassword" />
  103.     </LinearLayout>
  104. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
        在 onCreate 方法中,它初始化了一些控件,并设置了点击事件监听器。当用户点击注册按钮时,它会获取用户输入的用户名和暗码,并与重复暗码举行比较,然后根据注册结果显示相应的消息,并在注册成功时跳转到 LoginActivity 页面。
        另外,它还提供了点击事件监听器,用于在用户点击"已有账号,去登录"时跳转到登录页。
  1. package com.example.weather.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import com.example.weather.Data.DatabaseHelper;
  11. import com.example.weather.R;
  12. public class RegisterActivity extends AppCompatActivity {
  13.     private EditText mUserNameEditText;
  14.     private EditText mPasswordEditText,mRePasswordEditText;
  15.     private Button registerButton;
  16.     private TextView loginTv;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_register);
  21.         // 获取控件
  22.         mUserNameEditText = findViewById(R.id.username_edittext);
  23.         mPasswordEditText = findViewById(R.id.password_edittext);
  24.         mRePasswordEditText = findViewById(R.id.repassword);
  25.         registerButton = findViewById(R.id.register_button);
  26.         loginTv = findViewById(R.id.tv_login);
  27.   
  28.         // 点击跳转登录页
  29.         loginTv.setOnClickListener(new View.OnClickListener() {
  30.             @Override
  31.             public void onClick(View v) {
  32.                 Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  33.                 startActivity(intent);
  34.             }
  35.         });
  36.         // 注册成功后跳转登录页
  37.         registerButton.setOnClickListener(new View.OnClickListener() {
  38.             @Override
  39.             public void onClick(View v) {
  40.                 // 获取用户输入的内容
  41.                 String username = mUserNameEditText.getText().toString().trim();
  42.                 String password = mPasswordEditText.getText().toString().trim();
  43.                 String repassword = mRePasswordEditText.getText().toString().trim();
  44.                 if (username.isEmpty() || password.isEmpty()|| repassword.isEmpty()) {
  45.                     Toast.makeText(getApplicationContext(), "请输入账号或密码或重复密码", Toast.LENGTH_SHORT).show();
  46.                     return;
  47.                 }
  48.                 if (result) {
  49.                     Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
  50.                     Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  51.                     startActivity(intent);
  52.                     finish();
  53.                 } else {
  54.                     Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
  55.                 }
  56.             }
  57.         });
  58.     }
  59. }
复制代码
2.3 登岸页面实现


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:background="#ffffff"
  8.     tools:context=".ui.LoginActivity">
  9.     <ImageView
  10.         android:id="@+id/imageView3"
  11.         android:layout_width="0dp"
  12.         android:layout_height="230dp"
  13.         app:layout_constraintEnd_toEndOf="parent"
  14.         app:layout_constraintStart_toStartOf="parent"
  15.         app:layout_constraintTop_toTopOf="parent"
  16.         app:srcCompat="@drawable/logo" />
  17.     <LinearLayout
  18.         android:id="@+id/linearLayout2"
  19.         android:layout_width="0dp"
  20.         android:layout_height="wrap_content"
  21.         android:layout_marginStart="24dp"
  22.         android:layout_marginTop="8dp"
  23.         android:layout_marginEnd="24dp"
  24.         android:orientation="vertical"
  25.         app:layout_constraintEnd_toEndOf="parent"
  26.         app:layout_constraintStart_toStartOf="parent"
  27.         app:layout_constraintTop_toBottomOf="@+id/imageView3">
  28.         <TextView
  29.             android:id="@+id/textView"
  30.             android:layout_width="match_parent"
  31.             android:layout_height="wrap_content"
  32.             android:text="用户名"
  33.             android:textColor="#000000"
  34.             android:textSize="14sp"
  35.             android:textStyle="bold" />
  36.         <EditText
  37.             android:id="@+id/user"
  38.             android:layout_width="match_parent"
  39.             android:layout_height="wrap_content"
  40.             android:layout_marginTop="10dp"
  41.             android:ems="10"
  42.             android:hint="请输入用户名"
  43.             android:inputType="textPersonName"/>
  44.         <TextView
  45.             android:id="@+id/textView5"
  46.             android:layout_width="match_parent"
  47.             android:layout_height="wrap_content"
  48.             android:layout_marginTop="20dp"
  49.             android:text="密码"
  50.             android:textColor="#000000"
  51.             android:textSize="14sp"
  52.             android:textStyle="bold" />
  53.         <EditText
  54.             android:id="@+id/pass"
  55.             android:layout_width="match_parent"
  56.             android:layout_height="wrap_content"
  57.             android:layout_marginTop="10dp"
  58.             android:ems="10"
  59.             android:hint="请输入用户密码"
  60.             android:inputType="textPassword" />
  61.     </LinearLayout>
  62.     <Button
  63.         android:id="@+id/login_button"
  64.         android:layout_width="0dp"
  65.         android:layout_height="40dp"
  66.         android:layout_marginStart="32dp"
  67.         android:layout_marginTop="32dp"
  68.         android:layout_marginEnd="32dp"
  69.         android:background="@drawable/login"
  70.         android:text="立 即 登 录 "
  71.         android:textColor="#fff"
  72.         android:textSize="16sp"
  73.         app:layout_constraintEnd_toEndOf="parent"
  74.         app:layout_constraintHorizontal_bias="0.0"
  75.         app:layout_constraintStart_toStartOf="parent"
  76.         app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />
  77.     <TextView
  78.         android:id="@+id/login_register"
  79.         android:layout_width="wrap_content"
  80.         android:layout_height="wrap_content"
  81.         android:layout_marginTop="8dp"
  82.         android:text="新用户,立即注册!"
  83.         android:textColor="#00b5ff"
  84.         android:textStyle="bold"
  85.         app:layout_constraintEnd_toEndOf="@+id/login_button"
  86.         app:layout_constraintStart_toStartOf="@+id/login_button"
  87.         app:layout_constraintTop_toBottomOf="@+id/login_button" />
  88. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
        在 onCreate 方法中,它设置了一个延迟执行的 runnable 和一个计时器。计时器用于在一段时间后移除延迟执行的 runnable,以防止页面跳转后仍旧执行跳转逻辑。
        在 tomainActive 方法中,它启动了一个跳转到 LoginActivity 的 Intent,并在跳转完成后调用了 finish 方法注销当前页面。
  1. package com.example.weather.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import com.example.weather.Data.DatabaseHelper;
  11. import com.example.weather.R;
  12. public class LoginActivity extends AppCompatActivity {
  13.     private TextView loginRegister;
  14.     private EditText user;
  15.     private EditText pass;
  16.     private Button mLoginButton;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_login);
  21.         initView();
  22.         register();
  23.         home();
  24.     }
  25.     // 登陆成功后跳转首页
  26.     private void home() {
  27.         mLoginButton.setOnClickListener(new View.OnClickListener() {
  28.             @Override
  29.             public void onClick(View v) {
  30.                 // 获取用户输入的内容
  31.                 String username = user.getText().toString().trim();
  32.                 String password = pass.getText().toString().trim();
  33.                 if (username.isEmpty() || password.isEmpty()) {
  34.                     Toast.makeText(getApplicationContext(), "请输入账号或密码", Toast.LENGTH_SHORT).show();
  35.                     return;
  36.                 }
  37.                 boolean result = mDatabaseHelper.checkUser(username, password);
  38.                 if (result) {
  39.                     Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
  40.                     Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  41.                     startActivity(intent);
  42.                 } else {
  43.                     Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
  44.                 }
  45.             }
  46.         });
  47.     }
  48.     // 点击跳转注册页
  49.     private void register() {
  50.         loginRegister.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  54.                 startActivity(intent);
  55.             }
  56.         });
  57.     }
  58.     // 获取控件
  59.     private void initView() {
  60.         user = findViewById(R.id.user);
  61.         pass = findViewById(R.id.pass);
  62.         mLoginButton = findViewById(R.id.login_button);
  63.         loginRegister = findViewById(R.id.login_register);
  64.     }
  65. }
复制代码
2.4 首页实现

          首页重要实现当前都会的天气情况,24小时和未来7天还有生活指数的显示

         初始化控件:通过findViewById()方法获取布局文件中的各个控件。
        check()方法:用于查询都会信息,根据通报过来的都会信息设置默认都会,并将都会信息显示在TextView中。
        addCity()方法:用于添加都会,当点击添加按钮时,跳转到都会选择界面。
        nowWeather()方法:用于获取当天实时天气信息。首先获取都会名称,然后通过Volley库发送网络哀求,获取JSON数据。剖析JSON数据,将实时天气信息设置给对应的TextView,并根据天气切换对应的图标。还处理了RecyclerView的数据源,并将数据显示在RecyclerView中。末了,设置生活指数信息,将指数信息显示在页面中。
        futureWeather()方法:用于获取未来七每天气信息。发送网络哀求获取JSON数据,剖析JSON数据,将天气信息封装到FutureBean对象中,并设置到RecyclerView中显示。
        initView()方法:用于初始化控件,通过findViewById()方法获取各个控件的引用。
  1. package com.example.weather.ui;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.ImageView;
  6. import android.widget.TextView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import androidx.recyclerview.widget.LinearLayoutManager;
  9. import androidx.recyclerview.widget.RecyclerView;
  10. import com.android.volley.Request;
  11. import com.android.volley.RequestQueue;
  12. import com.android.volley.Response;
  13. import com.android.volley.VolleyError;
  14. import com.android.volley.toolbox.JsonObjectRequest;
  15. import com.android.volley.toolbox.Volley;
  16. import com.example.weather.Adapter.FutureAdapter;
  17. import com.example.weather.Adapter.NowAdapter;
  18. import com.example.weather.Bean.FutureBean;
  19. import com.example.weather.Bean.NowBean;
  20. import com.example.weather.R;
  21. import org.json.JSONArray;
  22. import org.json.JSONException;
  23. import org.json.JSONObject;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. public class MainActivity extends AppCompatActivity {
  27.     // 声明控件
  28.     private TextView cityTextView;
  29.     private TextView tem;
  30.     private ImageView weaImg;
  31.     private TextView wea;
  32.     private TextView humidity;
  33.     private TextView win;
  34.     private TextView winSpeed;
  35.     private RecyclerView rvNow;
  36.     private ImageView imgAdd;
  37.     private RecyclerView futuer;
  38.     private TextView chuanyi;
  39.     private TextView daisan;
  40.     private TextView ganmao;
  41.     private TextView chenlian;
  42.     private TextView ziwaixian;
  43.     private TextView liangshai;
  44.     private TextView xiche;
  45.     private TextView lvyou;
  46.     private TextView diaoyu;
  47.     private TextView kouzhaoTextView;
  48.     private TextView yundongTextView;
  49.     private TextView jinghuaqiTextView;
  50.     private String city;
  51.     @Override
  52.     protected void onCreate(Bundle savedInstanceState) {
  53.         super.onCreate(savedInstanceState);
  54.         setContentView(R.layout.activity_main);
  55.         initView(); // 初始化控件
  56.         check();
  57.         addCity();//添加城市
  58.         nowWeather(); // 获取当天实时天气
  59.         futureWeather();//获取未来7天天气
  60.     }
  61.     // 查询城市
  62.     private void check() {
  63.         // 获取传递过来的城市信息
  64.         String selectedCity = getIntent().getStringExtra("selected_city");
  65.         // 判断选中城市信息是否存在,并去掉结尾的"市"或"区"
  66.         if (selectedCity != null && (selectedCity.endsWith("市") || selectedCity.endsWith("区"))) {
  67.             selectedCity = selectedCity.substring(0, selectedCity.length() - 1);
  68.         } else if (city != null && (city.endsWith("市") || city.endsWith("区"))) {
  69.             city = city.substring(0, city.length() - 1);
  70.         }
  71.         // 在activity中的逻辑中进行判断并设置默认城市
  72.         if (selectedCity == null) {
  73.             selectedCity = "西安"; // 设置默认城市为西安
  74.         }
  75.         // 将城市信息显示在TextView中
  76.         cityTextView.setText(selectedCity);
  77.     }
  78.     // 修改添加城市
  79.     private void addCity() {
  80.         imgAdd.setOnClickListener(new View.OnClickListener() {
  81.             @Override
  82.             public void onClick(View v) {
  83.                 Intent intent = new Intent(MainActivity.this, CityActivity.class);
  84.                 startActivity(intent);
  85.             }
  86.         });
  87.     }
  88.     // 获取当天实时天气
  89.     private void nowWeather() {
  90.     }
  91.     // 未来七天天气
  92.     private void futureWeather() {
  93.       
  94.     }
  95.     // 初始化控件
  96.     private void initView() {
  97.         cityTextView = findViewById(R.id.cityTextView);
  98.         tem = findViewById(R.id.tem);
  99.         weaImg = findViewById(R.id.wea_img);
  100.         wea = findViewById(R.id.wea);
  101.         humidity = findViewById(R.id.humidity);
  102.         win = findViewById(R.id.win);
  103.         winSpeed = findViewById(R.id.win_speed);
  104.         rvNow = findViewById(R.id.rv_now);
  105.         imgAdd = findViewById(R.id.img_add);
  106.         futuer = findViewById(R.id.futuer);
  107.         chuanyi = findViewById(R.id.chuanyi);
  108.         daisan = findViewById(R.id.daisan);
  109.         ganmao = findViewById(R.id.ganmao);
  110.         chenlian = findViewById(R.id.chenlian);
  111.         ziwaixian = findViewById(R.id.ziwaixian);
  112.         liangshai = findViewById(R.id.liangshai);
  113.         xiche = findViewById(R.id.xiche);
  114.         lvyou = findViewById(R.id.lvyou);
  115.         diaoyu = findViewById(R.id.diaoyu);
  116.         kouzhaoTextView = findViewById(R.id.kouzhao);
  117.         yundongTextView = findViewById(R.id.yundong);
  118.         jinghuaqiTextView = findViewById(R.id.jinghuaqi);
  119.     }
  120. }
复制代码
2.5 都会管理列表页面实现               


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:background="#ffffff"
  8.     tools:context=".ui.CityActivity">
  9.     <View
  10.         android:id="@+id/view2"
  11.         android:layout_width="0dp"
  12.         android:layout_height="40dp"
  13.         android:background="#00b5ff"
  14.         app:layout_constraintEnd_toEndOf="parent"
  15.         app:layout_constraintStart_toStartOf="parent"
  16.         app:layout_constraintTop_toTopOf="parent" />
  17.     <ImageView
  18.         android:id="@+id/img_back"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:layout_marginStart="16dp"
  22.         app:layout_constraintBottom_toBottomOf="@+id/view2"
  23.         app:layout_constraintStart_toStartOf="parent"
  24.         app:layout_constraintTop_toTopOf="parent"
  25.         app:srcCompat="@drawable/back" />
  26.     <TextView
  27.         android:id="@+id/textView25"
  28.         android:layout_width="wrap_content"
  29.         android:layout_height="wrap_content"
  30.         android:text="城市管理"
  31.         android:textColor="#ffffff"
  32.         android:textSize="20sp"
  33.         app:layout_constraintBottom_toBottomOf="@+id/view2"
  34.         app:layout_constraintEnd_toEndOf="@+id/view2"
  35.         app:layout_constraintStart_toStartOf="parent"
  36.         app:layout_constraintTop_toTopOf="parent" />
  37.     <ImageView
  38.         android:id="@+id/img_add"
  39.         android:layout_width="20dp"
  40.         android:layout_height="20dp"
  41.         android:layout_marginEnd="16dp"
  42.         app:layout_constraintBottom_toBottomOf="@+id/view2"
  43.         app:layout_constraintEnd_toEndOf="parent"
  44.         app:layout_constraintTop_toTopOf="@+id/view2"
  45.         app:srcCompat="@drawable/add_city" />
  46.     <ListView
  47.         android:id="@+id/lv"
  48.         android:layout_width="0dp"
  49.         android:layout_height="0dp"
  50.         android:layout_marginTop="8dp"
  51.         android:background="#ffffff"
  52.         app:layout_constraintBottom_toBottomOf="parent"
  53.         app:layout_constraintEnd_toEndOf="parent"
  54.         app:layout_constraintStart_toStartOf="parent"
  55.         app:layout_constraintTop_toBottomOf="@+id/view2" />
  56. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
         在refreshListView方法中,通过适配器清空数据,然后从数据库中获取全部数据,并将它们添加到适配器中显示。如果适配器中没有数据,则显示提示信息。末了调用适配器的notifyDataSetChanged方法刷新列表视图。
        在add方法中,点击添加按钮时,会弹出一个对话框,用户可以在对话框中输入都会名称。点击确定按钮后,会判断输入的都会是否为空和是否已存在于数据库中,如果都满意,则将都会添加到数据库中,并清空输入框的内容。末了关闭对话框并刷新列表视图。
        在delete方法中,长按列表项会弹出一个对话框,扣问用户是否确认删除选中的都会。如果确认删除,则将选中的都会从数据库中删除,并显示删除成功的提示信息。末了刷新列表视图。
        在toDetails方法中,点击都会名称会跳转到天气页面。将选中的都会信息通过Intent通报给MainActivity,并启动MainActivity。同时,竣事当前的CityActivity。
        在back方法中,点击返回按钮会竣事当前的CityActivity,返回上一个页面。
        在initView方法中,获取布局中的各个控件,并举行必要的初始化操作。此中,通过CityAdapter创建一个适配器,并将其设置为ListView的适配器。然后调用refreshListView方法来显示数据库中的数据。
  1. package com.example.weather.ui;
  2. import android.app.Dialog;
  3. import android.content.DialogInterface;
  4. import android.content.Intent;
  5. import android.database.Cursor;
  6. import android.graphics.Color;
  7. import android.graphics.drawable.ColorDrawable;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.ImageView;
  15. import android.widget.ListView;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18. import androidx.appcompat.app.AlertDialog;
  19. import androidx.appcompat.app.AppCompatActivity;
  20. import com.example.weather.Adapter.CityAdapter;
  21. import com.example.weather.Data.CityHelper;
  22. import com.example.weather.R;
  23. import java.util.ArrayList;
  24. public class CityActivity extends AppCompatActivity {
  25.     private View view2;
  26.     private ImageView imgBack;
  27.     private TextView textView25;
  28.     private ListView lv;
  29.     private ImageView imgAdd;
  30.     private CityHelper cityHelper;
  31.     private ArrayAdapter<String> adapter;
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_city);
  36.         initView();
  37.         back();
  38.         add();
  39.         delete();
  40.         toDetails();
  41.     }
  42.     // 刷新适配器
  43.     private void refreshListView() {
  44.         // 清空适配器中的数据
  45.         adapter.clear();
  46.       
  47.     }
  48.     // 添加城市
  49.     private void add() {
  50.         imgAdd.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.               
  54.                 // 显示对话框
  55.                 dialog.show();
  56.             }
  57.         });
  58.     }
  59.     // 长按列表项进行城市删除
  60.     private void delete() {
  61.         lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  62.             @Override
  63.             public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  64.                 final String selectedCity = adapter.getItem(position);
  65.               
  66.             }
  67.         });
  68.     }
  69.     // 点击城市名称跳转天气页面
  70.     private void toDetails() {
  71.         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  72.             @Override
  73.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  74.                 String selectedCity = adapter.getItem(position);
  75.                 // 创建Intent来跳转到MainActivity
  76.                 Intent intent = new Intent(CityActivity.this, MainActivity.class);
  77.                 // 使用putExtra方法将选中的城市信息传递给MainActivity
  78.                 intent.putExtra("selected_city", selectedCity);
  79.                 startActivity(intent);
  80.                 finish();
  81.             }
  82.         });
  83.     }
  84.     //返回主页
  85.     private void back() {
  86.         imgBack.setOnClickListener(new View.OnClickListener() {
  87.             @Override
  88.             public void onClick(View v) {
  89.                 finish();
  90.             }
  91.         });
  92.     }
  93.     // 获取控件
  94.     private void initView() {
  95.         view2 = findViewById(R.id.view2);
  96.         imgBack = findViewById(R.id.img_back);
  97.         textView25 = findViewById(R.id.textView25);
  98.         lv = findViewById(R.id.lv);
  99.         imgAdd = findViewById(R.id.img_add);
  100.         cityHelper = new CityHelper(this);
  101.       
  102.     }
  103. }
复制代码
        到此为止,我们的项目就已经完成了! 
   
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天空闲话

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表