凌空没有提出要求

问题描述

我的应用应该从数据库获取数据并在recyclerview中显示,但是却出现错误 2020-08-16 18:12:01.712 29166-29166 / com.example.mn E / RecyclerView:未连接适配器;跳过布局,以下是我主要活动的代码

package com.example.mn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolBox.StringRequest;
import com.android.volley.toolBox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    //this is the JSON Data URL
    
    private static final String URL_PRODUCTS = "https://myUrl/russell/api.PHP";

    //a list to store all the products
    List<Product> productList;

    //the recyclerview
    RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //getting the recyclerview from xml
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new linearlayoutmanager(this));

        //initializing the productlist
        productList = new ArrayList<>();

        //this method will fetch and parse json
        //to display it in recyclerview
        loadProducts();
    }

    private void loadProducts() {

        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(Request.Method.GET,URL_PRODUCTS,new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject product = array.getJSONObject(i);

                                //adding the product to product list
                                productList.add(new Product(
                                        product.getInt("id"),product.getString("title"),product.getString("shortdesc")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            ProductsAdapter adapter = new ProductsAdapter(MainActivity.this,productList);
                            recyclerView.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printstacktrace();
                        }
                    }
                },new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(this).add(stringRequest);
    }
}

我在屏幕截图中放置了两个断点,但从未达到第二个断点

enter image description here

可能是什么问题

解决方法

正如评论所指出的那样,有更好的方法来执行此操作,但是如果您刚开始,可能很难一次完成所有这些操作。

不过,我可以告诉您一个快速解决方案,该解决方案应该只是在开始时将RecyclerView设置为一个空列表,以便它具有一个适配器(包含0个项目)

ProductsAdapter adapter = new ProductsAdapter(this,new ArrayList<Product>());
recyclerView.setAdapter(adapter);

这将直接在设置LayoutManager之后

,

在recyclerView.setAdapter(adapter)之后还要指定
adapter.notifyDataSetChanged();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...