Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android RecyclerView:未连接适配器;跳过布局、改装_Android_Android Recyclerview_Gson_Retrofit_Woocommerce Rest Api - Fatal编程技术网

Android RecyclerView:未连接适配器;跳过布局、改装

Android RecyclerView:未连接适配器;跳过布局、改装,android,android-recyclerview,gson,retrofit,woocommerce-rest-api,Android,Android Recyclerview,Gson,Retrofit,Woocommerce Rest Api,大家晚上好,我一直在寻找一个解决方案,以解决android studio的日志使用RecyclerView显示JSON“产品”列表时出现的错误 我已经阅读了与此错误相关的问题,但我无法找到符合我需要的正确答案 这是android studio显示的反错误日志 RecyclerView:未连接适配器;跳过布局 RecyclerView:未连接适配器;跳过布局 表面:getSlotFromBufferLocked:未知缓冲区:0xa3d9a700 OpenGLRenderer:0xa2b6b

大家晚上好,我一直在寻找一个解决方案,以解决android studio的日志使用RecyclerView显示JSON“产品”列表时出现的错误

我已经阅读了与此错误相关的问题,但我无法找到符合我需要的正确答案

这是android studio显示的反错误日志

RecyclerView:未连接适配器;跳过布局

RecyclerView:未连接适配器;跳过布局

表面:getSlotFromBufferLocked:未知缓冲区:0xa3d9a700

OpenGLRenderer:0xa2b6bb00(RippledRavable)上带有句柄0xa200a310的endAllStagingAnimators

java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$

对于该项目,已使用该类和布局文件

“Producto”类

在这个名为“JSONproducts”的类中,这两个都包含为数组

然后请求接口称为“讲师产品”

公共接口讲师产品{
@获取(“产品”)
调用ListarProductos();
}
回收器视图的数据适配器称为“Adapter”

公共类适配器扩展了RecyclerView.Adapter{
私有ArrayList产品;
私有数组列表分类;
公共适配器(ArrayList productos,ArrayList categoria){
this.productos=productos;
this.categoria=categoria;
}
@凌驾
public Adapter.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
View View=LayoutInflater.from(parent.getContext()).flate(R.layout.product_视图,parent,false);
返回新的ViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(Adapter.ViewHolder,内部位置){
holder.nom_pro_tv.setText(productos.get(position.getName());
holder.id\u pro\u tv.setText(productos.get(position.getId());
holder.cat_pro.setText(categoria.get(position.getName());
}
@凌驾
public int getItemCount(){
返回productos.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
私密文本视图:姓名专业电视、id专业电视、cat专业电视;
公共视图持有者(视图项视图){
超级(项目视图);
nom_pro_tv=(TextView)itemView.findviewbyd(R.id.nom_pro_tv);
id\u pro\u tv=(TextView)itemView.findviewbyd(R.id.id\u pro\u tv);
cat_pro=(TextView)itemView.findViewById(R.id.cat_pro_tv);
}
}
}
以及活动类“ListaProductos”

公共类ListaProductos扩展了AppCompatActivity{
私人回收站;
私有ArrayList产品;
私有数组列表类别;
私人改编者改编者;
公共静态最终字符串BASE_URL=”https://mydomain.com.mx/wp-json/wc/v1/";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u lista\u productos);
showView();
}
私有void showView(){
recyclerView=(recyclerView)findViewById(R.id.prod\u recycler\u视图);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
cargarJSON();
}
私有void cargarJSON(){
改装改装=新改装.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
最终讲师Productos producto=改装.create(讲师Productos.class);
调用productoCall=producto.ListarProductos();
productoCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
JSONproducts=response.body();
product=newarraylist(Arrays.asList(jsonproducts.getProducts());
category=newarraylist(Arrays.asList(jsonproducts.getCategories());
Adadador=新Adadador(产品、类别);
recyclerView.setAdapter(适配器);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“Error”,t.getMessage());
}
});
}
}
以及已使用的布局XML文件

回收视图布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_productos"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mx.com.corpcap.elrollorepartidor.ListaProductos">

<android.support.v7.widget.RecyclerView
    android:id="@+id/prod_recycler_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/></LinearLayout>

产品列表的CardView布局

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/nom_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>

        <TextView
            android:id="@+id/id_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/cat_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>

一切都很好,启动应用程序时没有任何问题,但当试图访问信息时,它会发送问题开头引用的日志消息


非常感谢

如果没有适配器提供必须显示的数据,RecyclerView本身就没有多大用处。因此,当一个RecyclerView被初始化并放置在布局中,但尚未调用.setAdapter时,您遇到的问题就会发生在发送网络请求之前,取一个空列表并使用它初始化适配器并将其设置为您的RecyclerView。当您发出网络请求并得到响应时,只需清除列表中的旧值,添加新值并通知适配器列表中的数据已更改。Alex,这应该可以避免跳过布局的问题

类似这样的内容:

private ArrayList<YourObjectClass> listOfYourObjects = new ArrayList<>();
.
.
.
SomeAdapter yourAdapter = new SomeAdapter(listOfYourObjects , context);
yourRecyclerView.setAdapter(yourAdapter);
.
.
.
onResponse:
list.clear();
//Let the adapter know the list is empty now
yourAdapter.notifyDataSetChanged();
//Fill in your list with values from server using a for/while loop etc.
//Again notify your adapter that the list has changed:
yourAdapter.notifyDataSetChanged();
private ArrayList listOfYourObjects=new ArrayList();
.
.
.
SomeAdapter yourAdapter=新的SomeAdapter(对象列表、上下文);
yourRecyclerView.setAdapter(yourAdapter);
.
.
.
答复:
list.clear();
//让适配器知道列表现在为空
yourAdapter.notifyDataSetChanged();
//使用for/while循环等,用服务器提供的值填写列表。
//再次通知适配器列表已更改:
yourAdapter.notifyDataSetChanged();
希望他
public interface LecturaProductos {

    @GET("Products")
    Call<JSONproducts> ListarProductos();

}
public class Adaptador extends RecyclerView.Adapter<Adaptador.ViewHolder> {

    private ArrayList<Producto> productos;
    private ArrayList<Category> categoria;

    public Adaptador(ArrayList<Producto> productos, ArrayList<Category> categoria){
        this.productos = productos;
        this.categoria = categoria;
    }

    @Override
    public Adaptador.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_view, parent, false );

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Adaptador.ViewHolder holder, int position) {
        holder.nom_pro_tv.setText(productos.get(position).getName());
        holder.id_pro_tv.setText(productos.get(position).getId());
        holder.cat_pro.setText(categoria.get(position).getName());

    }

    @Override
    public int getItemCount() {

        return productos.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView nom_pro_tv, id_pro_tv, cat_pro;
        public ViewHolder(View itemView) {
            super(itemView);

            nom_pro_tv = (TextView)itemView.findViewById(R.id.nom_pro_tv);
            id_pro_tv = (TextView)itemView.findViewById(R.id.id_pro_tv);
            cat_pro = (TextView)itemView.findViewById(R.id.cat_pro_tv);
        }
    }
}
public class ListaProductos extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<Producto> product;
    private ArrayList<Category> category;
    private Adaptador adaptador;



    public static final String BASE_URL= "https://mydomain.com.mx/wp-json/wc/v1/";

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

    }

    private void showView(){
        recyclerView = (RecyclerView)findViewById(R.id.prod_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        cargarJSON();

    }

    private void cargarJSON(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        final LecturaProductos producto = retrofit.create(LecturaProductos.class);
        Call<JSONproducts> productoCall = producto.ListarProductos();
        productoCall.enqueue(new Callback<JSONproducts>() {
            @Override
            public void onResponse(Call<JSONproducts> call, Response<JSONproducts> response) {

                JSONproducts jsonproducts = response.body();
                product = new ArrayList<>(Arrays.asList(jsonproducts.getProducts()));
                category = new ArrayList<>(Arrays.asList(jsonproducts.getCategories()));
                adaptador = new Adaptador(product, category);
                recyclerView.setAdapter(adaptador);
            }

            @Override
            public void onFailure(Call<JSONproducts> call, Throwable t) {
                Log.d("Error", t.getMessage());

            }
        });


    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_productos"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mx.com.corpcap.elrollorepartidor.ListaProductos">

<android.support.v7.widget.RecyclerView
    android:id="@+id/prod_recycler_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/></LinearLayout>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/nom_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"/>

        <TextView
            android:id="@+id/id_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/cat_pro_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>
private ArrayList<YourObjectClass> listOfYourObjects = new ArrayList<>();
.
.
.
SomeAdapter yourAdapter = new SomeAdapter(listOfYourObjects , context);
yourRecyclerView.setAdapter(yourAdapter);
.
.
.
onResponse:
list.clear();
//Let the adapter know the list is empty now
yourAdapter.notifyDataSetChanged();
//Fill in your list with values from server using a for/while loop etc.
//Again notify your adapter that the list has changed:
yourAdapter.notifyDataSetChanged();