【问题标题】:Volley not making requests排球没有提出要求
【发布时间】:2020-12-05 21:47:38
【问题描述】:

我的应用程序应该从数据库中获取数据并将其显示在 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);
    }
}

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

可能是什么问题

【问题讨论】:

  • 在构造函数中将 ListItems 赋予 Adapter 并不是一个好主意。我建议您在 onCreate 中创建一个适配器,然后将其附加到 RecyclerView。然后将下面的项目设置为适配器。将项目设置为适配器后,在适配器中调用 notifyDataSetChanged()。完成

标签: android android-recyclerview android-volley


【解决方案1】:

正如评论所指出的,有更好的方法可以做到这一点,但如果你刚开始,可能很难一次完成所有这些。

但是我可以告诉你一个快速修复,应该只是在开始时将 RecyclerView 设置为一个空列表,以便它有一个适配器(包含 0 个项目)

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

这将在设置 LayoutManager 之后直接进行

【讨论】:

    【解决方案2】:

    recyclerView.setAdapter(adapter) 后也指定
    适配器.notifyDataSetChanged();

    【讨论】:

      猜你喜欢
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      • 1970-01-01
      相关资源
      最近更新 更多