将Retrofit2与包含不同数量项目的适配器中的JSON结合使用

huangapple go评论107阅读模式
英文:

Combining Retrofit2 with JSON inside an Adapter with a Different Number of Items

问题

以下是您提供的代码的翻译部分:

  1. 我正尝试从一个 REST API 获取一些数据但我遇到了一个问题因为我有一些包含不同数量项的对象
  2. 例如
  3. [{7 }, {7 }, {6 }...{1 }]
  4. 它们有相同的键但问题在于在某些对象中一个或多个参数可能会丢失
  5. 我的应用程序开始运行但当我尝试向下滚动时它会崩溃我收到了以下错误
  6. 尝试从空对象引用中读取字段 'android.widget.TextView com.example.covidapp.covidAdapter$ViewHolder.tv_activeCases'
  7. 这是我的 `MainActivity` `ListView`
  8. public class MainActivity extends AppCompatActivity {
  9. private static final String TAG = "MainActivity";
  10. private ListView listView;
  11. private covidAdapter covidAdapter;
  12. private CovidApi covidApi;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. listView = (ListView) findViewById(R.id.list_view_main);
  18. Gson gson = new GsonBuilder().serializeNulls().create();
  19. Retrofit retrofit = new Retrofit.Builder().baseUrl("https://covid-19-tracking.p.rapidapi.com/")
  20. .addConverterFactory(GsonConverterFactory.create(gson)).build();
  21. covidApi = retrofit.create(CovidApi.class);
  22. Call<List<covid>> call = covidApi.getInfo();
  23. call.enqueue(new Callback<List<covid>>() {
  24. @Override
  25. public void onResponse(Call<List<covid>> call, Response<List<covid>> response) {
  26. if (!response.isSuccessful()){
  27. return;
  28. }
  29. List<covid> covids = response.body();
  30. covidAdapter = new covidAdapter((ArrayList<covid>) covids, getApplicationContext());
  31. listView.setAdapter(covidAdapter);
  32. }
  33. @Override
  34. public void onFailure(Call<List<covid>> call, Throwable t) {
  35. Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
  36. }
  37. });
  38. }
  39. }
  40. 这是 `Adapter`
  41. public class covidAdapter extends BaseAdapter {
  42. private ArrayList<covid> covidList;
  43. private Context context;
  44. public covidAdapter(ArrayList<covid> covidList, Context context){
  45. this.covidList = covidList;
  46. this.context = context;
  47. }
  48. @Override
  49. public int getCount() {
  50. return this.covidList.size();
  51. }
  52. @Override
  53. public Object getItem(int i) {
  54. return this.covidList.get(i);
  55. }
  56. @Override
  57. public long getItemId(int i) {
  58. return i;
  59. }
  60. @Override
  61. public View getView(int position, View convertView, ViewGroup parent) {
  62. ViewHolder holder = null;
  63. if (convertView == null) {
  64. LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  65. convertView = inf.inflate(R.layout.template, null);
  66. holder = new ViewHolder();
  67. holder.tv_activeCases =(TextView)convertView.findViewById(R.id.textview_active_cases);
  68. holder.tv_country =(TextView)convertView.findViewById(R.id.textview_country);
  69. //holder.tv_lastUpdate =(TextView)convertView.findViewById(R.id.textview_last_update);
  70. holder.tv_newCases =(TextView)convertView.findViewById(R.id.textview_new_cases);
  71. holder.tv_newDeaths =(TextView)convertView.findViewById(R.id.textview_new_deaths);
  72. holder.tv_totalCases =(TextView)convertView.findViewById(R.id.textview_total_cases);
  73. holder.tv_totalDeaths =(TextView)convertView.findViewById(R.id.textview_total_deaths);
  74. holder.tv_totalRecovered =(TextView)convertView.findViewById(R.id.textview_total_recovered);
  75. } else {
  76. holder = (ViewHolder) convertView.getTag();
  77. }
  78. covid covid = covidList.get(position);
  79. if ( covidList.size() < 0){
  80. System.out.println("No dps key");
  81. }else {
  82. holder.tv_activeCases.setText("Active Cases: " + covid.getActive());
  83. holder.tv_country.setText("Country: " + covid.getCountry());
  84. // holder.tv_lastUpdate.setText("Last Update: " + covid.getLastUpdate());
  85. holder.tv_newDeaths.setText("New Deaths: " + covid.getNewDeaths());
  86. holder.tv_newCases.setText("New Cases: " + covid.getNewCases());
  87. holder.tv_totalRecovered.setText("Recovered: " + covid.getRecovered());
  88. holder.tv_totalDeaths.setText("Deaths: " + covid.getTotalDeaths());
  89. holder.tv_totalCases.setText("Total Cases: " + covid.getTotalCases());
  90. }
  91. return convertView;
  92. }
  93. private static class ViewHolder{
  94. public TextView tv_country, tv_activeCases, tv_newCases, tv_newDeaths, tv_totalCases, tv_totalDeaths, tv_totalRecovered;
  95. }
  96. }
  97. 我将不胜感激任何帮助
英文:

I am trying to get some data from a rest API, but I have a problem because I have some objects with a different numbers of items.

For example:

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

They have the same key, but the problem is in some objects, one or more arguments can be missing.

My app starts running, but when I try to scroll down it crashes. I have am receiving this error:

  1. Attempt to read from field &#39;android.widget.TextView com.example.covidapp.covidAdapter$ViewHolder.tv_activeCases&#39; on a null object reference.

Here is my MainActivity and ListView:

  1. public class MainActivity extends AppCompatActivity {
  2. private static final String TAG = &quot;MainActivity&quot;;
  3. private ListView listView;
  4. private covidAdapter covidAdapter;
  5. private CovidApi covidApi;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. listView = (ListView) findViewById(R.id.list_view_main);
  11. Gson gson = new GsonBuilder().serializeNulls().create();
  12. Retrofit retrofit = new Retrofit.Builder().baseUrl(&quot;https://covid-19-tracking.p.rapidapi.com/&quot;)
  13. .addConverterFactory(GsonConverterFactory.create(gson)).build();
  14. covidApi = retrofit.create(CovidApi.class);
  15. Call&lt;List&lt;covid&gt;&gt; call = covidApi.getInfo();
  16. call.enqueue(new Callback&lt;List&lt;covid&gt;&gt;() {
  17. @Override
  18. public void onResponse(Call&lt;List&lt;covid&gt;&gt; call, Response&lt;List&lt;covid&gt;&gt; response) {
  19. if (!response.isSuccessful()){
  20. return;
  21. }
  22. List&lt;covid&gt; covids = response.body();
  23. covidAdapter = new covidAdapter((ArrayList&lt;covid&gt;) covids, getApplicationContext());
  24. listView.setAdapter(covidAdapter);
  25. }
  26. @Override
  27. public void onFailure(Call&lt;List&lt;covid&gt;&gt; call, Throwable t) {
  28. Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_LONG).show();
  29. }
  30. });
  31. }
  32. }

This is the Adapter class:

  1. public class covidAdapter extends BaseAdapter {
  2. private ArrayList&lt;covid&gt; covidList;
  3. private Context context;
  4. public covidAdapter(ArrayList&lt;covid&gt; covidList, Context context){
  5. this.covidList = covidList;
  6. this.context = context;
  7. }
  8. @Override
  9. public int getCount() {
  10. return this.covidList.size();
  11. }
  12. @Override
  13. public Object getItem(int i) {
  14. return this.covidList.get(i);
  15. }
  16. @Override
  17. public long getItemId(int i) {
  18. return i;
  19. }
  20. @Override
  21. public View getView(int position, View convertView, ViewGroup parent) {
  22. ViewHolder holder = null;
  23. if (convertView == null) {
  24. LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  25. convertView = inf.inflate(R.layout.template, null);
  26. holder = new ViewHolder();
  27. holder.tv_activeCases =(TextView)convertView.findViewById(R.id.textview_active_cases);
  28. holder.tv_country =(TextView)convertView.findViewById(R.id.textview_country);
  29. //holder.tv_lastUpdate =(TextView)convertView.findViewById(R.id.textview_last_update);
  30. holder.tv_newCases =(TextView)convertView.findViewById(R.id.textview_new_cases);
  31. holder.tv_newDeaths =(TextView)convertView.findViewById(R.id.textview_new_deaths);
  32. holder.tv_totalCases =(TextView)convertView.findViewById(R.id.textview_total_cases);
  33. holder.tv_totalDeaths =(TextView)convertView.findViewById(R.id.textview_total_deaths);
  34. holder.tv_totalRecovered =(TextView)convertView.findViewById(R.id.textview_total_recovered);
  35. } else {
  36. holder = (ViewHolder) convertView.getTag();
  37. }
  38. covid covid = covidList.get(position);
  39. if ( covidList.size() &lt; 0){
  40. System.out.println(&quot;No dps key&quot;);
  41. }else {
  42. holder.tv_activeCases.setText(&quot;Active Cases: &quot; + covid.getActive());
  43. holder.tv_country.setText(&quot;Country: &quot; + covid.getCountry());
  44. // holder.tv_lastUpdate.setText(&quot;Last Update: &quot; + covid.getLastUpdate());
  45. holder.tv_newDeaths.setText(&quot;New Deaths: &quot; + covid.getNewDeaths());
  46. holder.tv_newCases.setText(&quot;New Cases: &quot; + covid.getNewCases());
  47. holder.tv_totalRecovered.setText(&quot;Recovered: &quot; + covid.getRecovered());
  48. holder.tv_totalDeaths.setText(&quot;Deaths: &quot; + covid.getTotalDeaths());
  49. holder.tv_totalCases.setText(&quot;Total Cases: &quot; + covid.getTotalCases());
  50. }
  51. return convertView;
  52. }
  53. private static class ViewHolder{
  54. public TextView tv_country, tv_activeCases, tv_newCases, tv_newDeaths, tv_totalCases, tv_totalDeaths, tv_totalRecovered;
  55. }
  56. }

I would appreciate any help.

答案1

得分: 1

为了在下次调用getView()时能够检索到持有者,请进行以下存储:

  1. convertView.setTag(holder)
英文:

To store the holder so that it can be retrieved when getView() is called again:

  1. convertView.setTag(holder)

huangapple
  • 本文由 发表于 2020年7月29日 00:31:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定