-1

Guten Abend alle, Ive war auf der Suche nach einer Lösung für einen Fehler Android Studio-Protokoll sendet mit RecyclerView, um eine JSON "Produkt" Liste mit Retrofit zu zeigen.RecyclerView: Kein Adapter angeschlossen; Überspringen Layou, Retrofit

Ich habe bereits die Fragen zu diesem Fehler gelesen, bin aber nicht in der Lage, die richtige Antwort auf meine Bedürfnisse zu finden.

Android: RecyclerView: No adapter attached; skipping layout

No adapter attached; skipping layout recyclerview error

recyclerview No adapter attached; skipping layout

No adapter attached; skipping layout onCreateView()

Dies ist de Fehlerprotokoll von Android Studio zeigte

RecyclerView: Kein Adapter angebracht ist; Layout überspringen

RecyclerView: Kein Adapter angeschlossen; Layout

Oberfläche Überspringen: getSlotFromBufferLocked: unbekannt Puffer: 0xa3d9a700

OpenGLRenderer: endAllStagingAnimators auf 0xa2b6bb00 (RippleDrawable) mit Griff 0xa200a310

java.lang.Illegal: Erwartete BEGIN_OBJECT aber war BEGIN_ARRAY in Zeile 1 Spalte 2 Pfad $

Für diese proyect sind diese Klassen und Layout-Dateien verwendet

"Producto" Klasse

public class Producto { 

    @SerializedName("id") 
    @Expose 
    private int id; 
    @SerializedName("name") 
    @Expose 
    private String name; 
    @SerializedName("status") 
    @Expose 
    private String status; 
    @SerializedName("price") 
    @Expose 
    private String price; 
    @SerializedName("regular_price") 
    @Expose 
    private String regularPrice; 
    @SerializedName("sale_price") 
    @Expose 
    private String salePrice; 
    @SerializedName("price_html") 
    @Expose 
    private String priceHtml; 
    @SerializedName("on_sale") 
    @Expose 
    private boolean onSale; 
    @SerializedName("total_sales") 
    @Expose 
    private int totalSales; 
    @SerializedName("purchase_note") 
    @Expose 
    private String purchaseNote; 
    @SerializedName("categories") 
    @Expose 
    private List<Category> categories; 
    @SerializedName("menu_order") 
    @Expose 
    private int menuOrder; 


    /** 
    * 
    * @return 
    * The id 
    */ 
    public int getId() { 
     return id; 
    } 

    /** 
    * 
    * @param id 
    * The id 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * 
    * @return 
    * The name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * 
    * @param name 
    * The name 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * 
    * @return 
    * The status 
    */ 
    public String getStatus() { 
     return status; 
    } 

    /** 
    * 
    * @param status 
    * The status 
    */ 
    public void setStatus(String status) { 
     this.status = status; 
    } 

    /** 
    * 
    * @return 
    * The price 
    */ 
    public String getPrice() { 
     return price; 
    } 

    /** 
    * 
    * @param price 
    * The price 
    */ 
    public void setPrice(String price) { 
     this.price = price; 
    } 

    /** 
    * 
    * @return 
    * The regularPrice 
    */ 
    public String getRegularPrice() { 
     return regularPrice; 
    } 

    /** 
    * 
    * @param regularPrice 
    * The regular_price 
    */ 
    public void setRegularPrice(String regularPrice) { 
     this.regularPrice = regularPrice; 
    } 

    /** 
    * 
    * @return 
    * The salePrice 
    */ 
    public String getSalePrice() { 
     return salePrice; 
    } 

    /** 
    * 
    * @param salePrice 
    * The sale_price 
    */ 
    public void setSalePrice(String salePrice) { 
     this.salePrice = salePrice; 
    } 

    /** 
    * 
    * @return 
    * The priceHtml 
    */ 
    public String getPriceHtml() { 
     return priceHtml; 
    } 

    /** 
    * 
    * @param priceHtml 
    * The price_html 
    */ 
    public void setPriceHtml(String priceHtml) { 
     this.priceHtml = priceHtml; 
    } 

    /** 
    * 
    * @return 
    * The onSale 
    */ 
    public boolean isOnSale() { 
     return onSale; 
    } 

    /** 
    * 
    * @param onSale 
    * The on_sale 
    */ 
    public void setOnSale(boolean onSale) { 
     this.onSale = onSale; 
    } 

    /** 
    * 
    * @return 
    * The totalSales 
    */ 
    public int getTotalSales() { 
     return totalSales; 
    } 

    /** 
    * 
    * @param totalSales 
    * The total_sales 
    */ 
    public void setTotalSales(int totalSales) { 
     this.totalSales = totalSales; 
    } 

    /** 
    * 
    * @return 
    * The purchaseNote 
    */ 
    public String getPurchaseNote() { 
     return purchaseNote; 
    } 

    /** 
    * 
    * @param purchaseNote 
    * The purchase_note 
    */ 
    public void setPurchaseNote(String purchaseNote) { 
     this.purchaseNote = purchaseNote; 
    } 

    /** 
    * 
    * @return 
    * The categories 
    */ 
    public List<Category> getCategories() { 
     return categories; 
    } 

    /** 
    * 
    * @param categories 
    * The categories 
    */ 
    public void setCategories(List<Category> categories) { 
     this.categories = categories; 
    } 

    /** 
    * 
    * @return 
    * The menuOrder 
    */ 
    public int getMenuOrder() { 
     return menuOrder; 
    } 

    /** 
    * 
    * @param menuOrder 
    * The menu_order 
    */ 
    public void setMenuOrder(int menuOrder) { 
     this.menuOrder = menuOrder; 
    } 

} 

"Kategorie" Class (das entspricht die private List<Category> categories; Eigenschaft)

public class Category { 

    @SerializedName("id") 
    @Expose 
    private int id; 
    @SerializedName("name") 
    @Expose 
    private String name; 
    @SerializedName("slug") 
    @Expose 
    private String slug; 

    /** 
    * 
    * @return 
    * The id 
    */ 
    public int getId() { 
     return id; 
    } 

    /** 
    * 
    * @param id 
    * The id 
    */ 
    public void setId(int id) { 
     this.id = id; 
    } 

    /** 
    * 
    * @return 
    * The name 
    */ 
    public String getName() { 
     return name; 
    } 

    /** 
    * 
    * @param name 
    * The name 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /** 
    * 
    * @return 
    * The slug 
    */ 
    public String getSlug() { 
     return slug; 
    } 

    /** 
    * 
    * @param slug 
    * The slug 
    */ 
    public void setSlug(String slug) { 
     this.slug = slug; 
    } 
} 

beiden Contai nanied als Arrays in dieser Klasse namens "JSONproducts"

public class JSONproducts { 

    private Producto[] products; 
    private Category[] categories; 

    public Producto[] getProducts(){ 

     return products; 
    } 

    public Category[] getCategories(){ 
     return categories; 
    } 
} 

dann die Anforderung Schnittstelle namens "LecturaProductos"

public interface LecturaProductos { 

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

} 

Der Datenadapter für den Recycler Ansicht namens "Adaptador"

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); 
     } 
    } 
} 

Und die Aktivitätsklasse "ListaProductos"

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()); 

      } 
     }); 


    } 
} 

Und die Layout-XML-Dateien, die

RecyclerView Layout-

<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 Layout für die Produktliste

<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> 

Alles erfüllt groß und startet die App ohne ein Problem verwendet werden, aber wenn die Es wird versucht, auf Informationen zuzugreifen, es sendet die Protokollnachrichten, die am Anfang dieser Frage zitiert wurden.

Dank viel

+0

Nicht genau @ cricket_007, aber trotzdem danke –

+0

Eigentlich ja "Erwartete BEGIN_OBJECT aber war BEGIN_ARRAY" ... Das ist der echte Fehler, nicht so sehr "Layout überspringen" –

Antwort

0

A RecyclerView ist nicht viel auf sie selbst die ohne Adapter die Daten bereitstellt sie angezeigt werden müssen. Wenn also ein RecyclerView initialisiert und in das Layout eingefügt wird, aber .setAdapter noch nicht aufgerufen wurde, tritt das Problem auf. Sie nehmen eine leere Liste und verwenden diese, um den Adapter zu initialisieren und ihn auf Ihren RecyclerView zu setzen, bevor Sie die Netzwerkanforderung senden. Wenn Sie die Netzwerkanforderung stellen und eine Antwort erhalten, löschen Sie einfach die alten Werte in der Liste, fügen Sie neue Werte hinzu und benachrichtigen Sie Ihren Adapter, dass sich die Daten in der Liste geändert haben. Dies sollte das Layoutproblem überspringen, Alex.

Etwas wie folgt aus:

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(); 

Hoffnung, das hilft.

Verwandte Themen