Я получаю JSON Массив и пытаюсь показать его в виде списка. Я отладил и получил, что получаю массив по мере необходимости, но когда я запускаю, мое представление списка пусто на экране android. При запуске не отображается ошибка в синтаксисе или в эмуляторе. Вот мой код: я использовал залп для вызова API и получения json данных.
DashBoard. java
public class Dashboard extends AppCompatActivity {
private RequestQueue queue;
ListView listView;
ArrayList<rowitem> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
queue = Volley.newRequestQueue(this);
listView = (ListView) findViewById(R.id.myListView);
arrayList = new ArrayList<>();
String url = "https://api.rootnet.in/covid19-in/stats/latest";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
try {
JSONObject result = new JSONObject(response).getJSONObject("data");
JSONArray jsonArray = result.getJSONArray("regional");
for(int i=0; i<jsonArray.length(); i++)
{
JSONObject json_data = jsonArray.getJSONObject(i);
String location = json_data.getString("loc");
String totalcase = json_data.getString("totalConfirmed");
String recovered = json_data.getString("discharged");
String deaths = json_data.getString("deaths");
rowitem model = new rowitem();
model.setLocation(location);
model.setTotalcase(totalcase);
model.setRecovered(recovered);
model.setDeaths(deaths);
arrayList.add(model);
}
} catch (JSONException e) {
Toast.makeText(Dashboard.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Dashboard.this, "Server is not responding (Covid-19 Tracker)", Toast.LENGTH_LONG).show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
customlistviewadapter adapter = new customlistviewadapter(this, arrayList);
listView.setAdapter(adapter);
}
}
customlistviewadapter. java
package com.example.maps;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class customlistviewadapter extends BaseAdapter {
Context context;
ArrayList<rowitem> arrayList;
public customlistviewadapter(Context context,ArrayList<rowitem> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.row, parent, false);
}
TextView location, totalcase, recovered, deaths;
location = (TextView) convertView.findViewById(R.id.location);
totalcase = (TextView) convertView.findViewById(R.id.cases);
recovered = (TextView) convertView.findViewById(R.id.healthy);
deaths = (TextView) convertView.findViewById(R.id.deaths);
location.setText(arrayList.get(position).getLocation());
totalcase.setText(arrayList.get(position).getTotalcase());
recovered.setText(arrayList.get(position).getRecovered());
deaths.setText(arrayList.get(position).getDeaths());
return convertView;
}
}