TỔNG QUAN ĐỀ TÀI
I. LÝ DO CHỌN ĐỀ TÀI
Đn vi hc k môn Android nâng cao, sinh viên đưc hc thêm nhiu
kin thc kh c như đ! d" liê $u, service, fragment, database, random,… t)
đ* cung c+p thêm nn t-ng đ. ch/ng em c* th. t0 t1o cho mnh nh"ng
ng d2ng Android mi th/ v3 hơn, đa chc năng hơn.
D0 b o th8i tit iWeather như l: mô $t công c2 d0 b o th8i tit cơ b-n
đưc l+y t) OpenWeatherMap. Gi/p ngư8i d?ng c* th. xem đưc th8i
tit cAa b+t k th:nh phB n:o mnh muBn: nhiê $t đô $, đô $ Dm, gi* v: mây.
Ngo:i ra đ. ph2c v2 nhu cFu công viê $c, app cGn c* chc năng d0 b o th8i
tit cAa 7 ng:y k tip. Mong rIng vi app n:y sJ gi/p Kch cho mi ngư8i
trong cuô $c sBng hIng ng:y.
II. ĐỐI TƯỢNG NGHIÊN CỨU
Đi tượng nghiên cứu: Nghiên cu, ng d2ng công c2 Android
Studio v: web site OpenWeatherMap.org
Phạm vi nghiên cứu: Ứng d2ng d0 b o th8i tit trên hệ điu h:nh
Android.
III. PHƯONG PHÁP NGHIÊN CỨU
+ Về lý thuyết:
Tm hi.u v hệ điu h:nh Android.
Tm hi.u v ngôn ng" Anroid trong Android Studio.
1
C ch chn lc v: đ! d" liê $u t) Web xuBng App vi API KEY
+ Về thực nghiệm:
Xây d0ng ng d2ng iWeather đ. xem th8i tit dY d:ng trên điê $n
tho1i Android.
2
MỤC LỤC
TỔNG QUAN ĐỀ TÀI............................................................
LỜI CẢM ƠN....................................................................
XÂY DỰNG ỨNG DgNG iWEATHER.............................................
1. T!ng quan đ t:i...............................................................
1.1. X c đ3nh yêu cFu b:i to n....................................................
1.2. M2c đKch nghiên cu........................................................
2. Xây d0ng ng d2ng............................................................
2.1. C+u tr/c chương trnh.......................................................
2.2. Bi.u tưng icon cAa chương trnh............................................
2.3 Lập trnh c c chc năng.......................................................
2.3.1 Giao diện m:n hnh ch8...................................................
2.3.2 C c chc năng chKnh.......................................................
3 Giao diện ng d2ng..............................................................
4 Kt luận........................................................................
4.1 Kt qu- đ1t đưc.............................................................
4.2 Hưng ph t tri.n.............................................................
TỔNG KẾT.......................................................................
TÀI LIỆU THAM KHẢO..........................................................
3
LỜI CẢM ƠN
--------
Xin chân th:nh c m ơn Ban gi m hiệu c?ng to:n th. quý thFy cô, đặc
biệt l: quý thFy cô Viện CNTT - Điện - Điện Tử Trư8ng Đ1i Hc B: R3a
Vũng T:u, nh"ng ngư8i đã truyn đ1t cho tôi nhiu kin thc quý b u
trong hc tập.
Xin c m ơn NguyYn Th3 Minh Nương - gi-ng viên Viện CNTT -
Điện - Điện Tử l: ngư8i đã tận tnh hưng dẫn tôi trong suBt qu trnh
ho:n th:nh đ t:i n:y
Xin trân trng c-m ơn!
Tp.Vũng Tàu, ngày 25 tháng 3 năm 2018
Sinh viên thực hiện
NguyYn Trung Hiu
XÂY DỰNG ỨNG DỤNG iWEATHER
1. Tổng quan đề tài.
1.1. Xác định yêu cầu bài toán.
Ph2c v2 nhu cFu cuô $c sBng hIng ng:y: Xem th8i tit. App cung c+p
mi thông tin b-n cFn thit cho ngư8i d?ng, cũng như l: đưa ra d0 b o
7 ng:y ti đ. thuâ $n tiê $n cho l3ch l:m viê $c cAa h.
1.2. Mục đích nghiên cứu.
Nghiên cu v: ng d2ng web OpenWeatherMap (ngu•n tin $y) p
d2ng trên nn t-ng Android đ. t1o ra s-n phDm d0 b o th8i tit ti tay
ngư8i d?ng mô $t c ch tr0c quan, dY sử d2ng.
2. Xây dựng ứng dụng.
2.1. Cấu trúc chương trình.
- Trưc tiên t1o một Android Project vi tên l: iWeather
- Câu tr/c chung cAa chương trnh g•m c c phFn như sau:
Thư mục AndroidMainfest.xml
- Khai báo tên thuộc tính của các activity.
Hình 2.1.1 Cấu trúc chung của chương trình
Thư mục layout trong file XML
- File Thit k giao diện c c layout.
Thư mục các file code
- Class d?ng đ. xử lý c c s0 kiện t1i c c m:n hnh.
Hình 2.1.2 File XML trong Proprties
Hình 2.1.3 File XML trong thư mục Layout
Hello_activity: Xử s0 kiện m:n hnh ch:o trưc khi ch1y
v:o ng d2ng.
Main2Activity: Xử lK c c s0 kiê $n „ m:n hnh th 2 (7 ng:y
tip theo).
MainActivity: Xử lý c c s0 kiện trong layout Main.
iWeather_class: Kh„i t1o đBi tưng vi c c thuô $ c tKnh v:
phương thc tương đương.
CustomAdapter_class: Kh„i t1o Adapter đ. p d2ng
CustomListView
Hình 2.1.4 Class code của sự kiện trong layout
Thư mục drawable: Cha to:n bộ file hnh -nh sử d2ng trong
chương trnh.
Thư mục x9 l: Animation khi chuy;n gi<a 2 Activity
Hình 2.1.5 File chứa hình ảnh
trong thư mục drawable
2.2. Biểu tượng icon của chương trình.
Bi.u tưng Icon đă $t trong thư m2c mipmap vi kKch thưc đa
d1ng:
Hình 2.2.1 Bi;u tượng icon
2.3 Lập trình các chức năng.
2.3.1 Giao diện màn hình chào.
Hình 2.3.1.1 Màn hình chờ
2.3.2 Các chức năng chính.
Khi nh+n c c n/t trên m:n hnh th ng d2ng sJ đưc đn m:n
hnh tương ng vi n/t chc năng trên giao diện m:n hnh.
package com.example.makio.iweather;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
Hình 2.3.2.1 Màn hình ch:nh
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Date;
public class extends MainActivity AppCompatActivity {
EditText ;edtVitri
Button , ;btnOk btnNextday
TextView , , , ,tvTenTP tvTenNuoc tvTrangThai tvNhietDo
tvNgayCapNhat tvDoAm tvGio tvMay, , , ;
ImageView ;imgIcon
String = ;City ""
@Override
protected void onCreate(Bundle savedInstanceState) {
.onCreate(savedInstanceState);super
requestWindowFeature(Window. );FEATURE_NO_TITLE
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FUL
LSCREEN,
WindowManager.LayoutParams. );FLAG_FULLSCREEN
setContentView(R.layout. );activity_main
= (EditText) findViewById(R.id. );edtVitri edtLocation
= (Button) findViewById(R.id. );btnOk btnOk
= (Button) findViewById(R.id. );btnNextday btnNext
= (TextView) findViewById(R.id. );tvTenTP tvCityName
tvTenNuoc = (TextView)
findViewById(R.id. );tvCountryName
= (TextView)tvTrangThai
findViewById(R.id. );tvTrangthai
= (TextView) findViewById(R.id. );tvNhietDo tvTemp
tvNgayCapNhat = (TextView)
findViewById(R.id. );tvDateTime
= (TextView) findViewById(R.id. );tvDoAm tvHumidity
= (TextView) findViewById(R.id. );tvGio tvWind
= (TextView) findViewById(R.id. );tvMay tvCloud
= (ImageView) findViewById(R.id. );imgIcon imageView
//Mă
c đnh 1 thnh ph khi m App lên
GetCurrentWeatherData("Vung Tau");
View.OnClickListener() {btnOk.setOnClickListener(new
@Override
public void onClick(View view) {
String city = .getText().toString();edtVitri
if(city.equals( ))""
{
= ;City "Vung Tau"
GetCurrentWeatherData( );City
}
else
{
= city;City
GetCurrentWeatherData( );City
}
GetCurrentWeatherData(city);
}
});
// Gi d liê
u t MainActivity
btnNextday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String city = .getText().toString();edtVitri
Intent intent = new Intent(MainActivity. ,this
Main2Activity. );class
intent.putExtra( ,city);"name"
startActivity(intent);
//Chuy!n c"nh Activity
overridePendingTransition(R.anim. ,slide_in_from_left
R.anim. );slide_out_to_left
}
});
}
//L$y d liê
u th%i ti&t
public void GetCurrentWeatherData(String data)
{
RequestQueue requestQueue =
Volley. (MainActivity. );newRequestQueue this
String url = "http://api.openweathermap.org/data/2.5/weather?
q=" "&units=metric&appid=be8d3e323de722ff78208a7dbb2dcd6f"+data+ ;
StringRequest stringRequest = new
StringRequest(Request.Method.GET, url, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
{try
JSONObject jsonObject = JSONObject(response);new
String day = jsonObject.getString( );"dt"
String name = jsonObject.getString( );"name"
+name);tvTenTP.setText("Tên th4nh ph5: "
//L$y d liê
u ngy câp nhât
//Chuy!n bi&n Day thnh d1ng di
long l = Long. (day);valueOf
//Gi4 tr c6a mili giây
Date date = new Date(l* );1000L
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat( );"EEEE dd-MM-yyyy HH-mm-ss"
String Day = simpleDateFormat.format(date);
.setText(Day+ );tvNgayCapNhat " updated"
JSONArray jsonArrayWeather =
jsonObject.getJSONArray( );"weather"
JSONObject jsonObject1Weather =
jsonArrayWeather.getJSONObject( );0
String status =
jsonObject1Weather.getString( );"main"
String icon =
jsonObject1Weather.getString( );"icon"
//L$y d liê
u t thư viên Pi c4t sô (Online)
Picasso. (MainActivity. ).load(with this "http://open
weathermap.org/img/w/" ".png"+icon+ ).into(imgIcon);
.setText(status);tvTrangThai
//L$y d liê
u Nhiêt đô v Đô >m
JSONObject jsonObject1Main =
jsonObject.getJSONObject( );"main"
String nhietdo =
jsonObject1Main.getString( );"temp"
String doam =
jsonObject1Main.getString( );"humidity"
Double a = Double. (nhietdo);valueOf
String Nhietdo = String. (a.intValue());valueOf
tvNhietDo.setText(Nhietdo+ );"°C"
tvDoAm.setText(doam+ );"%"
//L$y d liê
u GI@ v Mây
JSONObject jsonObjectWind =
jsonObject.getJSONObject( );"wind"
String gio = jsonObjectWind.getString( );"speed"
tvGio.setText(gio+ );"m/s"
JSONObject jsonObject1Cloud =
jsonObject.getJSONObject( );"clouds"
String may = jsonObject1Cloud.getString( );"all"
tvMay.setText(may+ );"%"
//L$y d liê
u Quc gia
JSONObject jsonObject1Sys =
jsonObject.getJSONObject( );"sys"
String country =
jsonObject1Sys.getString( );"country"
+country);tvTenNuoc.setText("M; qu5c gia: "
} (JSONException e) {catch
e.printStackTrace();
}
}
},
Response.ErrorListener() {new
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
}
Các bư]c x_ lí sự kiê
a
n
Kh„i t1o đBi tưng:
public class iWeather_class {
String ;public Day
String ;public Status
String ;public Image
String ;public MaxTemp
String ;public MinTemp
iWeather_class(String day, String status, String image,public
String maxTemp, String minTemp) {
= day;Day
= status;Status
= image;Image
= maxTemp;MaxTemp
= minTemp;MinTemp
}
}
Hình 2.3.2.2 Màn hình hiện 7 ngày gEm co
Listview và ItemListview(hình dưIi)
Kh„i t1o customAdapter
public class extends CustomAdaper_class BaseAdapter {
Context ;context
ArrayList <iWeather_class> ;arrayList
CustomAdaper_class(Context context, ArrayList<iWeather_class>public
arrayList) {
= context;this.context
= arrayList;this.arrayList
}
@Override
public int getCount()
{
return arrayList.size();
}
@Override
public Object getItem(int i)
{
return arrayList.get(i);
}
@Override
public long getItemId(int i) {
;return 0
}
@Override
//%nh x' ki)u khác
public View getView( i, View view, ViewGroup viewGroup) {int
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout. , );dong_listview null
iWeather_class iWeatherClass = arrayList.get(i);
TextView tvDay = (TextView) view.findViewById(R.id. );tvNgay
TextView tvStatus = (TextView) view.findViewById(R.id.tvTrangthai);
TextView tvMaxTemp = (TextView) view.findViewById(R.id.tvMaxTemp);
TextView tvMinTemp = (TextView) view.findViewById(R.id.tvMinTemp);
ImageView imgStatus = (ImageView)
view.findViewById(R.id.imgTrangthai);
tvDay.setText(iWeatherClass. );Day
tvStatus.setText(iWeatherClass. );Status
tvMaxTemp.setText(iWeatherClass.MaxTemp+"đô
a
C");

Preview text:

TỔNG QUAN ĐỀ TÀI
I. LÝ DO CHỌN ĐỀ TÀI
Đn vi hc k môn Android nâng cao, sinh viên đưc hc thêm nhiu
kin thc kh c như đ! d" liê $u, service, fragment, database, random,… t)
đ* cung c+p thêm nn t-ng đ. ch/ng em c* th. t0 t1o cho mnh nh"ng
ng d2ng Android mi th/ v3 hơn, đa chc năng hơn.
D0 b o th8i tit iWeather như l: mô $t công c2 d0 b o th8i tit cơ b-n
đưc l+y t) OpenWeatherMap. Gi/p ngư8i d?ng c* th. xem đưc th8i
tit cAa b+t k th:nh phB n:o mnh muBn: nhiê $t đô $, đô $ Dm, gi* v: mây.
Ngo:i ra đ. ph2c v2 nhu cFu công viê $c, app cGn c* chc năng d0 b o th8i
tit cAa 7 ng:y k tip. Mong rIng vi app n:y sJ gi/p Kch cho mi ngư8i trong cuô $c sBng hIng ng:y.
II. ĐỐI TƯỢNG NGHIÊN CỨU
Đi tượng nghiên cứu: Nghiên cu, ng d2ng công c2 Android
Studio v: web site OpenWeatherMap.org
Phạm vi nghiên cứu: Ứng d2ng d0 b o th8i tit trên hệ điu h:nh Android.
III. PHƯONG PHÁP NGHIÊN CỨU
+ Về lý thuyết:
Tm hi.u v hệ điu h:nh Android.
Tm hi.u v ngôn ng" Anroid trong Android Studio. 1
C ch chn lc v: đ! d" liê $u t) Web xuBng App vi API KEY
+ Về thực nghiệm:
Xây d0ng ng d2ng iWeather đ. xem th8i tit dY d:ng trên điê $n tho1i Android. 2 MỤC LỤC
TỔNG QUAN ĐỀ TÀI............................................................
LỜI CẢM ƠN....................................................................
XÂY DỰNG ỨNG DgNG iWEATHER.............................................
1. T!ng quan đ t:i...............................................................
1.1. X c đ3nh yêu cFu b:i to n....................................................
1.2. M2c đKch nghiên cu........................................................
2. Xây d0ng ng d2ng............................................................
2.1. C+u tr/c chương trnh.......................................................
2.2. Bi.u tưng icon cAa chương trnh............................................
2.3 Lập trnh c c chc năng.......................................................
2.3.1 Giao diện m:n hnh ch8...................................................
2.3.2 C c chc năng chKnh.......................................................
3 Giao diện ng d2ng..............................................................
4 Kt luận........................................................................
4.1 Kt qu- đ1t đưc.............................................................
4.2 Hưng ph t tri.n.............................................................
TỔNG KẾT.......................................................................
TÀI LIỆU THAM KHẢO.......................................................... 3 LỜI CẢM ƠN --------
Xin chân th:nh c m ơn Ban gi m hiệu c?ng to:n th. quý thFy cô, đặc
biệt l: quý thFy cô Viện CNTT - Điện - Điện Tử Trư8ng Đ1i Hc B: R3a
Vũng T:u, nh"ng ngư8i đã truyn đ1t cho tôi nhiu kin thc quý b u trong hc tập.
Xin c m ơn cô NguyYn Th3 Minh Nương - gi-ng viên Viện CNTT -
Điện - Điện Tử l: ngư8i đã tận tnh hưng dẫn tôi trong suBt qu trnh ho:n th:nh đ t:i n:y Xin trân trng c-m ơn!
Tp.Vũng Tàu, ngày 25 tháng 3 năm 2018
Sinh viên thực hiện NguyYn Trung Hiu
XÂY DỰNG ỨNG DỤNG iWEATHER
1. Tổng quan đề tài.
1.1. Xác định yêu cầu bài toán.
Ph2c v2 nhu cFu cuô $c sBng hIng ng:y: Xem th8i tit. App cung c+p
mi thông tin cơ b-n cFn thit cho ngư8i d?ng, cũng như l: đưa ra d0 b o
7 ng:y ti đ. thuâ $n tiê $n cho l3ch l:m viê $c cAa h.
1.2. Mục đích nghiên cứu.
Nghiên cu v: ng d2ng web OpenWeatherMap (ngu•n tin câ $y) p
d2ng trên nn t-ng Android đ. t1o ra s-n phDm d0 b o th8i tit ti tay
ngư8i d?ng mô $t c ch tr0c quan, dY sử d2ng.
2. Xây dựng ứng dụng.
2.1. Cấu trúc chương trình.
- Trưc tiên t1o một Android Project vi tên l: iWeather
- Câu tr/c chung cAa chương trnh g•m c c phFn như sau:
Hình 2.1.1 Cấu trúc chung của chương trình
Thư mục AndroidMainfest.xml
- Khai báo tên thuộc tính của các activity.
Hình 2.1.2 File XML trong Proprties
Thư mục layout trong file XML
- File Thit k giao diện c c layout.
Hình 2.1.3 File XML trong thư mục Layout
Thư mục các file code
- Class d?ng đ. xử lý c c s0 kiện t1i c c m:n hnh.
Hình 2.1.4 Class code của sự kiện trong layout
Hello_activity: Xử lý s0 kiện m:n hnh ch:o trưc khi ch1y v:o ng d2ng.
Main2Activity: Xử lK c c s0 kiê $n „ m:n hnh th 2 (7 ng:y tip theo).
MainActivity: Xử lý c c s0 kiện trong layout Main.
iWeather_class: Kh„i t1o đBi tưng vi c c thuô $ c tKnh v:
phương thc tương đương.
CustomAdapter_class: Kh„i t1o Adapter đ. p d2ng CustomListView
Thư mục drawable: Cha to:n bộ file hnh -nh sử d2ng trong chương trnh.
Hình 2.1.5 File chứa hình ảnh
trong thư mục drawable
Thư mục x9 l: Animation khi chuy;n gi
2.2. Biểu tượng icon của chương trình.
Hình 2.2.1 Bi;u tượng icon
Bi.u tưng Icon đă $t trong thư m2c mipmap vi kKch thưc đa d1ng:
2.3 Lập trình các chức năng.
2.3.1 Giao diện màn hình chào.
Hình 2.3.1.1 Màn hình chờ
2.3.2 Các chức năng chính.
Hình 2.3.2.1 Màn hình ch:nh
Khi nh+n c c n/t trên m:n hnh th ng d2ng sJ đưc đn m:n
hnh tương ng vi n/t chc năng trên giao diện m:n hnh.
package com.example.makio.iweather;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat; import java.util.Date;
public class MainActivity extends AppCompatActivity { EditText ; edtVitri
Button btnOk, btnNextday; TextView tvTenTP, tvTenNuoc, tvTrangThai, tvNhietDo,
tvNgayCapNhat, tvDoAm, tvGio, tvMay; ImageView imgIcon;
String City = ""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); requestWindowFeature(Window. ); FEATURE_NO_TITLE
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FUL LSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
edtVitri = (EditText) findViewById(R.id.edtLocation);
btnOk = (Button) findViewById(R.id.btnOk); = (Button) findViewById(R.id. btnNextday btnNext);
= (TextView) findViewById(R.id. tvTenTP tvCityName); tvTenNuoc = (TextView)
findViewById(R.id.tvCountryName); tvTrangThai = (TextView)
findViewById(R.id.tvTrangthai);
= (TextView) findViewById(R.id. tvNhietDo tvTemp); tvNgayCapNhat = (TextView)
findViewById(R.id.tvDateTime);
tvDoAm = (TextView) findViewById(R.id.tvHumidity);
tvGio = (TextView) findViewById(R.id.tvWind);
tvMay = (TextView) findViewById(R.id.tvCloud);
imgIcon = (ImageView) findViewById(R.id.imageView); //Mă
c đnh 1 thnh ph khi m App lên
GetCurrentWeatherData("Vung Tau");
btnOk.setOnClickListener( View.OnClickListener() { new @Override
public void onClick(View view) { String city = .getText().toString(); edtVitri
if(city.equals("")) {
City = "Vung Tau"; GetCurrentWeatherData( ); City } else { City = city; GetCurrentWeatherData( ); City } GetCurrentWeatherData(city); } }); // Gi d liê u t MainActivity
btnNextday.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) { String city = .getText().toString(); edtVitri
Intent intent = new Intent(MainActivity. , this Main2Activity.class);
intent.putExtra("name",city); startActivity(intent); //Chuy!n c"nh Activity
overridePendingTransition(R.anim.slide_in_from_left,
R.anim.slide_out_to_left); } }); } //L$y d liê u th%i ti&t
public void GetCurrentWeatherData(String data) { RequestQueue requestQueue =
Volley.newRequestQueue(MainActivity.this);
String url = "http://api.openweathermap.org/data/2.5/weather?
q="
+data+"&units=metric&appid=be8d3e323de722ff78208a7dbb2dcd6f"; StringRequest stringRequest = new
StringRequest(Request.Method.GET, url, new Response.Listener() { @Override
public void onResponse(String response) { try { JSONObject jsonObject = JSONObject(response); new
String day = jsonObject.getString("dt");
String name = jsonObject.getString("name");
tvTenTP.setText("Tên th4nh ph5: "+name); //L$y d liê u ngy câ p nhât
//Chuy!n bi&n Day thnh d1ng di
long l = Long.valueOf(day); //Gi4 tr c6a mili giây
Date date = new Date(l*1000L);
SimpleDateFormat simpleDateFormat = new
SimpleDateFormat("EEEE dd-MM-yyyy HH-mm-ss");
String Day = simpleDateFormat.format(date);
tvNgayCapNhat.setText(Day+ " updated"); JSONArray jsonArrayWeather =
jsonObject.getJSONArray("weather");
JSONObject jsonObject1Weather =
jsonArrayWeather.getJSONObject(0); String status =
jsonObject1Weather.getString("main"); String icon =
jsonObject1Weather.getString("icon"); //L$y d liê u t thư viê
n Pi c4t sô (Online) Picasso. (MainActivity. with
this).load("http://open
weathermap.org/img/w/"+icon+".png").into(imgIcon);
tvTrangThai.setText(status); //L$y d liê u Nhiê t đô v Đô  >m
JSONObject jsonObject1Main =
jsonObject.getJSONObject("main"); String nhietdo =
jsonObject1Main.getString("temp"); String doam =
jsonObject1Main.getString("humidity");
Double a = Double.valueOf(nhietdo);
String Nhietdo = String.valueOf(a.intValue());
tvNhietDo.setText(Nhietdo+ ); "°C"
tvDoAm.setText(doam+"%"); //L$y d liê u GI@ v Mây
JSONObject jsonObjectWind =
jsonObject.getJSONObject("wind");
String gio = jsonObjectWind.getString("speed");
tvGio.setText(gio+"m/s"); JSONObject jsonObject1Cloud = jsonObject.getJSONObject( ); "clouds"
String may = jsonObject1Cloud.getString( "all");
tvMay.setText(may+"%"); //L$y d liê u Quc gia
JSONObject jsonObject1Sys =
jsonObject.getJSONObject("sys"); String country =
jsonObject1Sys.getString("country"); tvTenNuoc.setText( +country); "M; qu5c gia: "
} catch (JSONException e) { e.printStackTrace(); } } },
new Response.ErrorListener() { @Override
public void onErrorResponse(VolleyError error) { } });
requestQueue.add(stringRequest); } }
Hình 2.3.2.2 Màn hình hiện 7 ngày gEm co
Listview và ItemListview(hình dưIi)
Các bư]c x_ lí sự kiê a n Kh„i t1o đBi tưng:
public class iWeather_class {
public String Day;
public String Status;
public String Image;
public String MaxTemp;
public String MinTemp;
public iWeather_class(String day, String status, String image,
String maxTemp, String minTemp) { Day = day; Status = status; Image = image; = maxTemp; MaxTemp = minTemp; MinTemp } } Kh„i t1o customAdapter
public class CustomAdaper_class extends BaseAdapter { Context context; ArrayList ; arrayList
public CustomAdaper_class(Context context, ArrayList arrayList) {
this.context = context; this. = arrayList; arrayList } @Override public int getCount() {
return arrayList.size(); } @Override
public Object getItem(int i) {
return arrayList.get(i); } @Override
public long getItemId(int i) { return ; 0 } @Override //%nh x' ki)u khác
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout. ,
dong_listview null);
iWeather_class iWeatherClass = arrayList.get(i);
TextView tvDay = (TextView) view.findViewById(R.id.tvNgay);
TextView tvStatus = (TextView) view.findViewById(R.id.tvTrangthai);
TextView tvMaxTemp = (TextView) view.findViewById(R.id.tvMaxTemp);
TextView tvMinTemp = (TextView) view.findViewById(R.id.tvMinTemp);
ImageView imgStatus = (ImageView)
view.findViewById(R.id.imgTrangthai);
tvDay.setText(iWeatherClass.Day);
tvStatus.setText(iWeatherClass.Status);
tvMaxTemp.setText(iWeatherClass.MaxTemp+"đô a C");