BÀI 70 - TẠO SEARCHVIEW VỚI TOOLBAR TRONG ANDROID
Đầu tiên, đây là link dữ liệu tiêm chủng Covid 19 tại Việt Nam: https://tiemchungcovid19.gov.vn/api/public/dashboard/vaccination-statistics/all
Tuy nhiên, link này thì hosting chưa có chứng chỉ SSL nên Android sẽ không cho phép bạn lấy dữ liệu nhằm tránh các nguy cơ bị tấn công mạng. Hiện tại, 10/2021 web vẫn có bị lỗi đó. Để khắc phục mình sẽ cung cấp hosting của mình để bạn sử dụng nhá!
(Hoặc bạn có thể vào bài này để khắc phục lỗi và bỏ qua mọi lỗi do thiếu chứng chỉ SSL: đây)
Link này sẽ cung cấp cho bạn dữ liệu tiêm chủng tại các địa phương và số vaccine đã tiêm ngày hôm qua: https://robotquantrac.web.app/xml/bangsolieu.xml
Link này sẽ cung cấp cho bạn bảng số liệu tiêm chủng theo ngày: https://robotquantrac.web.app/xml/tiemtheongay.xml
Trong android, tạo 2 Class như này. Nhớ tạo thêm các Constructor và Getter and Setter nữa nhé
Đây là Class chứ dữ liệu tổng theo địa phương
public class ClassTiemChungCovid implements Serializable {
String tenThanhPho;
long duKienPhanBo;
long phanBoThucTe;
long soLieuDaTiem;
long tiemMuiMot;
long tiemMuiHai;
long danSo;
long danSoTren18;
String ngayTiem;
long soMui;
public ClassTiemChungCovid(){}
}
Đây là Class chứa dữ liệu tiêm hôm qua
public class ClassAllTotalVacine implements Serializable {
String tiemHomQua;
String daTiem;
public ClassAllTotalVacine(){}
public ClassAllTotalVacine(String tiemHomQua, String daTiem) {
this.tiemHomQua = tiemHomQua;
this.daTiem = daTiem;
}
public String getTiemHomQua() {return tiemHomQua;}
public void setTiemHomQua(String tiemHomQua) {this.tiemHomQua = tiemHomQua;}
public String getDaTiem() {return daTiem;}
public void setDaTiem(String daTiem) {this.daTiem = daTiem;}
}
Tạo một cái Apdapter như này
public class AdapterThongTinTiemChung extends RecyclerView.Adapter<AdapterThongTinTiemChung.ViewHodle> {
private final Context context;
private final ArrayList<ClassTiemChungCovid> arrayListTiemChung;
public AdapterThongTinTiemChung(Context context, ArrayList<ClassTiemChungCovid> arrayListTiemChung) {
this.context = context;
this.arrayListTiemChung = arrayListTiemChung;
}
@NonNull
@Override
public ViewHodle onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.layout_thongtin_tiemchung, parent, false);
return new ViewHodle(view);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHodle holder, int position) {
holder.tvDiaPhuong.setText(arrayListTiemChung.get(position).getTenThanhPho());
DecimalFormat decimalFormat = new DecimalFormat("###,###,###");
DecimalFormat formatTyLe = new DecimalFormat("#0.00");
holder.tvDaTiem.setText(decimalFormat.format(arrayListTiemChung.get(position).getSoLieuDaTiem()));
long danSo = arrayListTiemChung.get(position).getDanSo();
long mui1 = arrayListTiemChung.get(position).getTiemMuiMot();
float tyLe = mui1*100f / danSo;
holder.tvTyLeMui1.setText(formatTyLe.format(tyLe) + " %");
holder.pgTyLeMui1.setProgress((int) tyLe);
}
@Override
public int getItemCount() {
return arrayListTiemChung.size();
}
public Context getContext() {
return context;
}
public static class ViewHodle extends RecyclerView.ViewHolder{
TextView tvDiaPhuong, tvDaTiem, tvTyLeMui1;
ProgressBar pgTyLeMui1;
public ViewHodle(@NonNull View itemView) {
super(itemView);
tvDiaPhuong = itemView.findViewById(R.id.textView_tiemchung_diaphuong);
tvDaTiem = itemView.findViewById(R.id.textview_tiemchung_datiem);
tvTyLeMui1 = itemView.findViewById(R.id.textView_tiemchung_tyle_mui1);
pgTyLeMui1 = itemView.findViewById(R.id.progress_bar_tiemchung_tyle_mui1);
}
}
}
Tạo Arraylist với cái class như trên, gán RecyclerView với mảng đó và khởi tạo Apdapter trong Main.java như này nè
if (arrayListTiemChung == null) {
arrayListTiemChung = new ArrayList<>();
}
if (arrayListTotalVacine == null) {
arrayListTotalVacine = new ArrayList<>();
}
adapterTiemChung = new AdapterThongTinTiemChung(this, arrayListTiemChung);
GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), 1);
rvTiemChung.setAdapter(adapterTiemChung);
rvTiemChung.setHasFixedSize(true);
rvTiemChung.setLayoutManager(manager);
Tạo một chương trình con tải dữ liệu như này nè!
private void downloadXml(String url_link) {
try {
URL url = new URL(url_link);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser parser = factory.newPullParser();
parser.setInput(inputStream, "UTF-8");
int eventType = parser.getEventType();
ClassTiemChungCovid tiemChungCovid = null;
ClassAllTotalVacine totalVacine = null;
String text = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String tag = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tag.equalsIgnoreCase("item")) {
tiemChungCovid = new ClassTiemChungCovid();
} else if (tag.equalsIgnoreCase("VaccinationStatisticsDTO")){
totalVacine = new ClassAllTotalVacine();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tiemChungCovid != null){
if (tag.equalsIgnoreCase("provinceName")){
tiemChungCovid.tenThanhPho = text;
} else if (tag.equalsIgnoreCase("totalVaccineAllocated")){
tiemChungCovid.duKienPhanBo = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("totalVaccineAllocatedReality")){
tiemChungCovid.phanBoThucTe = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("totalInjected")){
if (tiemChungCovid.getSoLieuDaTiem() == 0) {
tiemChungCovid.soLieuDaTiem = Long.parseLong(text);
} else {
tiemChungCovid.soMui = Long.parseLong(text);
}
} else if (tag.equalsIgnoreCase("totalOnceInjected")){
tiemChungCovid.tiemMuiMot = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("totalTwiceInjected")){
tiemChungCovid.tiemMuiHai = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("population")){
tiemChungCovid.danSo = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("popOverEighteen")){
tiemChungCovid.danSoTren18 = Long.parseLong(text);
} else if (tag.equalsIgnoreCase("item")){
arrayListTiemChung.add(tiemChungCovid);
} else if (tag.equalsIgnoreCase("reportedDate")){
tiemChungCovid.ngayTiem = text;
}
}
if (totalVacine != null) {
if (tag.equalsIgnoreCase("objectInjection")){
totalVacine.daTiem = text;
} else if (tag.equalsIgnoreCase("totalPopulation")){
totalVacine.tiemHomQua = text;
} else if (tag.equalsIgnoreCase("VaccinationStatisticsDTO")){
arrayListTotalVacine.add(totalVacine);
}
}
break;
}
eventType = parser.next();
}
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
}
Gần xong ròi, bây giờ chúng ta tạo một AsyncTask để hệ thống chạy theo tiến trình. Cái này nhằm tránh ngăn cản người dùng bấm linh tinh khi hệ thống đang xử lý một điều gì đó.
@SuppressLint("StaticFieldLeak")
private class AsyncTaskXml extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
downloadXml(strings[0]);
return null;
}
@SuppressLint("NotifyDataSetChanged")
@Override
protected void onPostExecute(Void unused) {
super.onPostExecute(unused);
adapterTiemChung.notifyDataSetChanged();
Set_data_infomationTotal();
if (pgTiemChung.isShowing()) {
pgTiemChung.cancel();
}
}
}
Gán link web vào cái nữa là xong!
pgTiemChung = new ProgressDialog(TiemChungCovidActivity.this);
pgTiemChung.setMessage(getString(R.string.doi_ti_dang_tai));
pgTiemChung.show();
pgTiemChung.setCancelable(false);
arrayListTiemChung.clear();
arrayListTotalVacine.clear();
HttpsTrustManager.allowAllSSL();
new AsyncTaskXml().execute("https://robotquantrac.web.app/xml/bangsolieu.xml");