title: 课堂练习之疫情查询APP
date: 2020-03-17 20:08:51
tags:
在之前的体温记录APP上改进,只写出疫情信息查询页面的代码。
实体类与上篇博客SSM整合中的CityData相同。
package com.example.fightvirus;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
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.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeChatFragment extends Fragment {
BarChart barChart;
private List<CityData> cityList = new ArrayList<CityData>();
private RadioGroup radioGroup;
private RadioButton province,city,country;
private EditText queryDate,keyName;
private Button queryButton;
private String way,key,date;
public HomeChatFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_home_chat, container, false);
barChart = inflate.findViewById(R.id.barChart);
radioGroup = inflate.findViewById(R.id.radioGroup);
province = inflate.findViewById(R.id.province);
city = inflate.findViewById(R.id.city);
country = inflate.findViewById(R.id.country);
queryDate = inflate.findViewById(R.id.queryDate);
keyName = inflate.findViewById(R.id.keyName);
queryButton = inflate.findViewById(R.id.queryButton);
return inflate;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.province:
way = "getInfoByProvince";
break;
case R.id.country:
way = "getInfoByCountry";
break;
case R.id.city:
way = "getInfoByName";
break;
default:
break;
}
}
});
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
key = String.valueOf(keyName.getText().toString().trim());
date = String.valueOf(queryDate.getText().toString());
String url = "http://117.50.96.227/EpidemicInfo/city/" + way + "/" + key + "/" + date;
RequestQueue queue = Volley.newRequestQueue(getActivity());
// Toast.makeText(getActivity(),url,Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(StringRequest.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Gson gson = new Gson();
Type cityType = new TypeToken<ArrayList<CityData>>() {
}.getType();
cityList = gson.fromJson(response, cityType);
barChart.clear();
initMyView(cityList);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("myTAG", "onErrorResponse: " + error);
}
});
queue.add(stringRequest);
}
});
}
private void initMyView(List<CityData> cityList) {
//chart的设计
Description description = new Description();
description.setText("该页面数据仅供参考");
barChart.setNoDataText("没有数据!");
barChart.setDescription(description);
barChart.setDrawBorders(false);
barChart.setBackgroundColor(Color.WHITE);
barChart.setDrawGridBackground(false);
barChart.setDrawBarShadow(false);
barChart.setHighlightFullBarEnabled(false);
barChart.setDragEnabled(true);
barChart.setNoDataText("正在加载数据...");
//X轴数据
ArrayList<BarEntry> entries_all = new ArrayList<>();
ArrayList<BarEntry> entries_cure = new ArrayList<>();
ArrayList<BarEntry> entries_dead = new ArrayList<>();
for (int i = 0; i < cityList.size(); i++) {
entries_all.add(new BarEntry(i, Float.parseFloat(cityList.get(i).getConfirm())));
entries_cure.add(new BarEntry(i, Float.parseFloat(cityList.get(i).getHeal())));
entries_dead.add(new BarEntry(i, Float.parseFloat(cityList.get(i).getDead())));
}
List<String> citiesName = new ArrayList<>();
for (int i = 0; i < cityList.size(); i++) {
citiesName.add(cityList.get(i).getName());
}
//X轴设计
XAxis xAxis = barChart.getXAxis();
xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(cityList.size());
xAxis.setValueFormatter(new IndexAxisValueFormatter(citiesName));
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawAxisLine(false);
xAxis.setLabelCount(cityList.size());
xAxis.setCenterAxisLabels(true);
//Y轴的设计
YAxis rightY = barChart.getAxisRight();
YAxis leftY = barChart.getAxisLeft();
rightY.setEnabled(false);
leftY.setEnabled(false);
BarDataSet barDataSet_all = new BarDataSet(entries_all, "确诊人数");
barDataSet_all.setColor(Color.RED);
BarDataSet barDataSet_cure = new BarDataSet(entries_cure, "治愈人数");
barDataSet_cure.setColor(Color.GREEN);
BarDataSet barDataSet_dead = new BarDataSet(entries_dead, "死亡人数");
barDataSet_dead.setColor(Color.GRAY);
BarData barData = new BarData();
barData.addDataSet(barDataSet_all);
barData.addDataSet(barDataSet_cure);
barData.addDataSet(barDataSet_dead);
barData.setBarWidth(0.3f);
barData.groupBars(0f, 0.1f, 0f);
barChart.setData(barData);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".HomeChatFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.1" />
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline8" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.40" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toEndOf="@+id/textViewHeal"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/province"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="省份" />
<RadioButton
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="城市" />
<RadioButton
android:id="@+id/country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="国家" />
</RadioGroup>
<TextView
android:id="@+id/textViewHeal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择查询方式"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择查询日期"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline7" />
<EditText
android:id="@+id/queryDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="yyyy-MM-dd"
android:inputType="date"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline7" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入关键字"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/guideline10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline9" />
<EditText
android:id="@+id/keyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入要查询的关键字"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/guideline10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline9" />
<Button
android:id="@+id/queryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline10" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
手机扫一扫
移动阅读更方便
你可能感兴趣的文章