猿问

RecyclerView 不显示来自 JSON 的数据

我正在尝试在 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);

qq_花开花谢_0
浏览 98回答 1
1回答

交互式爱情

&nbsp; private void loadRecyclerViewData() { RequestQueue requestQueue = Volley.newRequestQueue(this); // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Using Gson to turn JSON to Java object of Station GsonBuilder gsonBuilder = new GsonBuilder(); Gson gson = gsonBuilder.create(); Station station = gson.fromJson(response, Station.class); stationList.add(station);adapter.notifyDataSetChanged();Log.d("API RESPONSE", response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("VOLLEY ERROR", error.toString()); } }); // Add the request to the RequestQueue. requestQueue.add(stringRequest); }将数据添加到数据集后,您必须调用 notifyDataSetChanged()。
随时随地看视频慕课网APP

相关分类

Java
我要回答