在上一篇文章中,我们讨论了如何创建仅显示Global Stats的COVID-19 Tracker Android应用。在本文中,我们将学习如何创建COVID-19 Tracker Android应用,该应用将显示印度任何州的任何城市的详细信息。诸如-总活跃病例,治愈病例,死亡人数以及任何相应城市的确诊病例总数之类的详细信息。在此应用程序中,让我们跟踪5个城市的演示,一个城市可以根据他/她的需求进行更改。
方法
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:现在添加一些文件,然后再编写XML和Java代码
- 转到应用程序-> res->值-> colors.xml部分,然后设置应用程序的颜色。
- 这些颜色用于各种地方的应用程序中。
colors.xml
#024265
#024265
#05af9b
#fb7268
#ededf2
#E3E0E0
#FFA726
#66BB6A
#EF5350
#29B6F6
AndroidManifests.xml
activity_main.xml
testing.xml
Model.java
public class Model {
private String name, total, death, cured, active, incAct, incDec, incRec;
public Model(String name, String total, String death, String cured,
String active, String incAct, String incDec, String incRec)
{
this.name = name;
this.total = total;
this.death = death;
this.cured = cured;
this.active = active;
this.incAct = incAct;
this.incDec = incDec;
this.incRec = incRec;
}
public String getIncAct()
{
return incAct;
}
public void setIncAct(String incAct)
{
this.incAct = incAct;
}
public String getIncDec()
{
return incDec;
}
public void setIncDec(String incDec)
{
this.incDec = incDec;
}
public String getIncRec()
{
return incRec;
}
public void setIncRec(String incRec)
{
this.incRec = incRec;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTotal()
{
return total;
}
public void setTotal(String total)
{
this.total = total;
}
public String getDeath()
{
return death;
}
public void setDeath(String death)
{
this.death = death;
}
public String getCured()
{
return cured;
}
public void setCured(String cured)
{
this.cured = cured;
}
public String getActive()
{
return active;
}
public void setActive(String active)
{
this.active = active;
}
}
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
// Create A Class Adapter
public class Adapter extends ArrayAdapter {
private Context context;
private List modelList;
public Adapter(Context context, List modelList) {
super(context, R.layout.testing, modelList);
this.context = context;
this.modelList = modelList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.testing, null,true);
// In this step we connect the XML with Java File
TextView state = view.findViewById(R.id.state);
TextView active = view.findViewById(R.id.active);
TextView cured = view.findViewById(R.id.cured);
TextView death = view.findViewById(R.id.death);
TextView total = view.findViewById(R.id.total);
TextView incactive = view.findViewById(R.id.incactive);
TextView inccured = view.findViewById(R.id.inccured);
TextView incdeath = view.findViewById(R.id.incdeath);
// Adding Data to modellist
state.setText(modelList.get(position).getName());
active.setText(modelList.get(position).getActive());
cured.setText(modelList.get(position).getCured());
death.setText(modelList.get(position).getDeath());
total.setText(modelList.get(position).getTotal());
incactive.setText(modelList.get(position).getIncAct());
inccured.setText(modelList.get(position).getIncRec());
incdeath.setText(modelList.get(position).getIncDec());
return view;
}
}
MainActivity.java
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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 org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
public static List modelList = new ArrayList<>();
Model model;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
fetchData();
}
private void fetchData() {
// The Link Through Which We Can Fetch Data
String url = "https://api.covid19india.org/state_district_wise.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
try {
// Creating JSON Object
JSONObject object = new JSONObject(response);
// From that object we are fetching data
JSONObject object1 = object.getJSONObject("Uttar Pradesh");
JSONObject object2 = object1.getJSONObject("districtData");
JSONObject object3 = object2.getJSONObject("Prayagraj");
JSONObject object4 = object3.getJSONObject("delta");
String active = object3.getString("active");
String confirmed = object3.getString("confirmed");
String deceased = object3.getString("deceased");
String recovered = object3.getString("recovered");
String confInc = object4.getString("confirmed");
String confDec = object4.getString("deceased");
String confRec = object4.getString("recovered");
model = new Model("Prayagraj", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Ballia");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Ballia", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Lucknow");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Lucknow", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Varanasi");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Varanasi", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Agra");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Agra", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
adapter = new Adapter(MainActivity.this, modelList);
listView.setAdapter(adapter);
// In case of error it will run
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// In case of error it will run
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
}
- 转到Gradle脚本-> build.gradle(模块:应用程序)部分,导入以下依赖项,然后单击“立即同步”按钮以同步APP。
- 要了解有关Volley Library的更多信息,请单击此处。
implementation ‘com.android.volley:volley:1.1.1’
- 现在转到应用程序->清单-> AndroidManifests.xml部分,并允许“ Internet Permission ”,因为此应用程序将使用Internet权限。
AndroidManifests.xml
第3步:使用JSON解析从网站获取数据
- 单击此URL- https://api.covid19india.org/state_district_wise.json
- 它将显示整个国家的数据,在这里,我们将仅从此处获取数据。
步骤4:设计activity_main.xml的布局
- 转到应用-> res->布局-> activity_main.xml
- 向布局添加TextView:在屏幕顶部显示GEEKSFORGEEKS。
- 将ListView添加到布局:在屏幕上显示跟踪详细信息的城市列表。
activity_main.xml
输出界面:
步骤5:为ListView设计布局
- 通过右键单击res文件夹内的布局文件夹来创建新的布局
- 然后单击布局资源文件并设置名称testing.xml
- 单击完成保存
- 单击testing.xml并为ListView启动设计UI布局
- testing.xml中使用的Drawable文件为“ arrow arrow ”。转到drawable-> New-> Vector Asset并搜索“ arrow arrow up”并将其添加到您的文件中。
testing.xml
输出界面:
第6步:创建一个新的Java类,以获取要从网站获取的数据
- 创建一个新的Java类,将其命名为Model。Java
- 使用getter和setter函数来为您要从网站获取的数据创建一个函数。
模型。Java
public class Model {
private String name, total, death, cured, active, incAct, incDec, incRec;
public Model(String name, String total, String death, String cured,
String active, String incAct, String incDec, String incRec)
{
this.name = name;
this.total = total;
this.death = death;
this.cured = cured;
this.active = active;
this.incAct = incAct;
this.incDec = incDec;
this.incRec = incRec;
}
public String getIncAct()
{
return incAct;
}
public void setIncAct(String incAct)
{
this.incAct = incAct;
}
public String getIncDec()
{
return incDec;
}
public void setIncDec(String incDec)
{
this.incDec = incDec;
}
public String getIncRec()
{
return incRec;
}
public void setIncRec(String incRec)
{
this.incRec = incRec;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getTotal()
{
return total;
}
public void setTotal(String total)
{
this.total = total;
}
public String getDeath()
{
return death;
}
public void setDeath(String death)
{
this.death = death;
}
public String getCured()
{
return cured;
}
public void setCured(String cured)
{
this.cured = cured;
}
public String getActive()
{
return active;
}
public void setActive(String active)
{
this.active = active;
}
}
步骤7:创建适配器类
现在创建一个新的Java适配器类,以将已获取的数据放入之前创建的ListView中。
Java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
// Create A Class Adapter
public class Adapter extends ArrayAdapter {
private Context context;
private List modelList;
public Adapter(Context context, List modelList) {
super(context, R.layout.testing, modelList);
this.context = context;
this.modelList = modelList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.testing, null,true);
// In this step we connect the XML with Java File
TextView state = view.findViewById(R.id.state);
TextView active = view.findViewById(R.id.active);
TextView cured = view.findViewById(R.id.cured);
TextView death = view.findViewById(R.id.death);
TextView total = view.findViewById(R.id.total);
TextView incactive = view.findViewById(R.id.incactive);
TextView inccured = view.findViewById(R.id.inccured);
TextView incdeath = view.findViewById(R.id.incdeath);
// Adding Data to modellist
state.setText(modelList.get(position).getName());
active.setText(modelList.get(position).getActive());
cured.setText(modelList.get(position).getCured());
death.setText(modelList.get(position).getDeath());
total.setText(modelList.get(position).getTotal());
incactive.setText(modelList.get(position).getIncAct());
inccured.setText(modelList.get(position).getIncRec());
incdeath.setText(modelList.get(position).getIncDec());
return view;
}
}
步骤8:使用MainActivity。 Java文件
在此文件中,我们将使用Volley库。请参阅代码内的注释以了解代码。
主要活动。Java
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
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 org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
public static List modelList = new ArrayList<>();
Model model;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
fetchData();
}
private void fetchData() {
// The Link Through Which We Can Fetch Data
String url = "https://api.covid19india.org/state_district_wise.json";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener() {
@Override
public void onResponse(String response) {
try {
// Creating JSON Object
JSONObject object = new JSONObject(response);
// From that object we are fetching data
JSONObject object1 = object.getJSONObject("Uttar Pradesh");
JSONObject object2 = object1.getJSONObject("districtData");
JSONObject object3 = object2.getJSONObject("Prayagraj");
JSONObject object4 = object3.getJSONObject("delta");
String active = object3.getString("active");
String confirmed = object3.getString("confirmed");
String deceased = object3.getString("deceased");
String recovered = object3.getString("recovered");
String confInc = object4.getString("confirmed");
String confDec = object4.getString("deceased");
String confRec = object4.getString("recovered");
model = new Model("Prayagraj", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Ballia");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Ballia", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Lucknow");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Lucknow", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Varanasi");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Varanasi", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
// Creating JSON Object
object3 = object2.getJSONObject("Agra");
// From that object we are fetching data
active = object3.getString("active");
confirmed = object3.getString("confirmed");
deceased = object3.getString("deceased");
recovered = object3.getString("recovered");
object4 = object3.getJSONObject("delta");
confInc = object4.getString("confirmed");
confDec = object4.getString("deceased");
confRec = object4.getString("recovered");
model = new Model("Agra", confirmed, deceased, recovered, active,
confInc, confDec, confRec);
// placing data into the app using AdapterClass
modelList.add(model);
adapter = new Adapter(MainActivity.this, modelList);
listView.setAdapter(adapter);
// In case of error it will run
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// In case of error it will run
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
}
输出
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!