我正在尝试在 RecyclerView 中显示从 AirVisual API 提供的 JSON 收集的数据。但是,我遇到了一个问题,即 JSON 中的数据不会显示在 RecyclerView 中,而如果我自己向其中添加数据(通过 ArrayList),数据将显示在 RecyclerView 中。
这是我从 API 解析的 JSON 数据:
{
"status": "success",
"data": {
"city": "New York",
"state": "New York",
"country": "USA",
"location": {
"type": "Point",
"coordinates": [
-73.928596,
40.694401
]
},
"current": {
"weather": {
"ts": "2018-09-12T23:00:00.000Z",
"hu": 93,
"ic": "10d",
"pr": 1024,
"tp": 23,
"wd": 102,
"ws": 2.31
},
"pollution": {
"ts": "2018-09-12T22:00:00.000Z",
"aqius": 18,
"mainus": "p2",
"aqicn": 6,
"maincn": "p2"
}
}
}
}
我怀疑来自 JSON 的数据根本没有添加到 ArrayList 中,因为记录返回的 ArrayList 的大小2,这是我手动添加到列表中的 2 个对象。
对于从 JSON 存储数据的Station类,我使用 jsonschema2pojo.org 生成 Gson 反序列化所需的类(不确定这是否是使用 JSON 数据的正确/最佳方式)。
这是 MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final String url = "http://api.airvisual.com/v2/city?city=New%20York&state=New%20York&country=USA&key={{API_KEY}}";
private RecyclerView recyclerView;
private Adapter adapter;
private List<Station> stationList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
stationList = new ArrayList<>();
loadRecyclerViewData();
//Information for these objects are in the code
//but I decided not to include it in here
//because these are just for testing
Station station1 = new Station();
Station station2 = new Station();
stationList.add(station1);
stationList.add(station2);
adapter = new Adapter(this, stationList);
交互式爱情
相关分类