在具有不同项数的适配器内将Reformi2与JSON相结合



我试图从一个rest API获取一些数据,但我遇到了一个问题,因为我有一些对象的项数不同。

例如:

[{7 items}, {7 items}, {6 items}...{1 item}]

它们有相同的键,但问题是在某些对象中,可能缺少一个或多个参数。

我的应用程序开始运行,但当我尝试向下滚动时,它会崩溃。我收到这个错误:

Attempt to read from field 'android.widget.TextView com.example.covidapp.covidAdapter$ViewHolder.tv_activeCases' on a null object reference. 

这是我的MainActivityListView:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ListView listView;
private covidAdapter covidAdapter;
private CovidApi covidApi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_view_main);
Gson gson = new GsonBuilder().serializeNulls().create();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://covid-19-tracking.p.rapidapi.com/")
.addConverterFactory(GsonConverterFactory.create(gson)).build();
covidApi = retrofit.create(CovidApi.class);
Call<List<covid>> call = covidApi.getInfo();
call.enqueue(new Callback<List<covid>>() {
@Override
public void onResponse(Call<List<covid>> call, Response<List<covid>> response) {
if (!response.isSuccessful()){
return;
}
List<covid> covids = response.body();

covidAdapter = new covidAdapter((ArrayList<covid>) covids, getApplicationContext());
listView.setAdapter(covidAdapter);
}
@Override
public void onFailure(Call<List<covid>> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}

这是Adapter类:

public class covidAdapter extends BaseAdapter {
private ArrayList<covid> covidList;
private Context context;
public covidAdapter(ArrayList<covid> covidList, Context context){
this.covidList = covidList;
this.context = context;
}
@Override
public int getCount() {
return this.covidList.size();
}
@Override
public Object getItem(int i) {
return this.covidList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.template, null);
holder = new ViewHolder();
holder.tv_activeCases =(TextView)convertView.findViewById(R.id.textview_active_cases);
holder.tv_country =(TextView)convertView.findViewById(R.id.textview_country);
//holder.tv_lastUpdate =(TextView)convertView.findViewById(R.id.textview_last_update);
holder.tv_newCases =(TextView)convertView.findViewById(R.id.textview_new_cases);
holder.tv_newDeaths =(TextView)convertView.findViewById(R.id.textview_new_deaths);
holder.tv_totalCases =(TextView)convertView.findViewById(R.id.textview_total_cases);
holder.tv_totalDeaths =(TextView)convertView.findViewById(R.id.textview_total_deaths);
holder.tv_totalRecovered =(TextView)convertView.findViewById(R.id.textview_total_recovered);

} else {
holder = (ViewHolder) convertView.getTag();
}
covid covid = covidList.get(position);
if ( covidList.size() < 0){
System.out.println("No dps key");
}else {
holder.tv_activeCases.setText("Active Cases: " + covid.getActive());
holder.tv_country.setText("Country: " + covid.getCountry());
// holder.tv_lastUpdate.setText("Last Update: " + covid.getLastUpdate());
holder.tv_newDeaths.setText("New Deaths: " + covid.getNewDeaths());
holder.tv_newCases.setText("New Cases: " + covid.getNewCases());
holder.tv_totalRecovered.setText("Recovered: " + covid.getRecovered());
holder.tv_totalDeaths.setText("Deaths: " + covid.getTotalDeaths());
holder.tv_totalCases.setText("Total Cases: " + covid.getTotalCases());
}
return convertView;
}

private static class ViewHolder{
public TextView tv_country, tv_activeCases,  tv_newCases, tv_newDeaths, tv_totalCases, tv_totalDeaths, tv_totalRecovered;
}
}

如果有任何帮助,我将不胜感激。

存储持有者,以便在再次调用getView()时可以检索:

convertView.setTag(holder) 

最新更新