Je m'enseigne moi-même sur le développement d'applications Android (et Java). J'ai du mal à comprendre pourquoi les éléments de ma liste n'apparaissent pas. Le pop-up fonctionne bien, donc je n'ai pas inclus le code pour cela.

J'ai essayé de voir si le GroceryItemAdapter fonctionne en ajoutant un message Log.d() et il ne s'affiche pas. Je suppose que c'est l'adaptateur que j'ai fabriqué?

Mettre à jour: Correction d'une ligne, mais maintenant je reçois ceci (je le mettrai à jour plus tard, en recherchant la cause ou en le déboguant):

La réponse d'AutoCrab a aidé à résoudre le problème d'objet nul, j'ai également compris que je devais convertir mon entier en une chaîne avant de le passer à setText().

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
        at com.example.myapplication.GroceryListAdapter$ViewHolder.<init>




package com.example.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

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

//responsible for displaying data from the model into a row in the recycler view
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
    private List<GroceryList> lists = new ArrayList<>();
    public GroceryListAdapter(List<GroceryList> lists) {
        this.lists = lists;
    }
    

    //onCreateViewHolder is responsible for creating each view
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //create a new view and wrap it inside a new viewholder
        //use layout inflator to inflate a view
        View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
        Log.d("MEJD", "Iamhere?");
        //wrap it inside a ViewHolder and return it
        ViewHolder viewHolder = new ViewHolder(groceryListView);
        return viewHolder;
    }

    //onBindViewHolder is responsible for taking data at a particular position and
    //putting it into a viewholder
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //get data model by position
        GroceryList groceryListItem = lists.get(position);

        //set the item views based on the data models
        TextView textView = holder.listTitle;
        textView.setText(groceryListItem.getListName());
        Log.d("MEJD", "Iamhere");
        TextView numItemsView = holder.numItems;
        numItemsView.setText(groceryListItem.getNumItems());

   }

    //get number of items in the data
    @Override
    public int getItemCount() {
        lists.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        //member variables
        public TextView listTitle;
        public TextView numItems;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            listTitle.findViewById(R.id.groceryListName);
            numItems.findViewById(R.id.numItemsList);
        }
    }
}

Classe de liste d'épicerie:

    package com.example.myapplication;
    
    import java.util.List;
    
    public class GroceryList {
        //class to hold the info of each list item
       private String listName;
       private int numItems;
       //will need to refactor later.....
    
        public GroceryList(String listName, int numItems) {
            this.listName = listName;
            this.numItems = numItems;
    
        }
        public String getListName() {
            return listName;
        }
    
        public void setListName(String listName) {
            this.listName = listName;
        }
    
        public int getNumItems() {
            return numItems;
        }
    
        public void setNumItems(int numItems) {
            this.numItems = numItems;
        }
    
    
    }

Adaptateur de liste d'épicerie:

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

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

//responsible for displaying data from the model into a row in the recycler view
public class GroceryListAdapter extends RecyclerView.Adapter<GroceryListAdapter.ViewHolder>{
    private List<GroceryList> lists = new ArrayList<>();
    public GroceryListAdapter(List<GroceryList> lists) {
        this.lists = lists;
    }
    //create a constructor


    //onCreateViewHolder is responsible for creating each view
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //create a new view and wrap it inside a new viewholder
        //use layout inflator to inflate a view
        View groceryListView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grocery_list_entry,parent,false);
        Log.d("MEJD", "Iamhere?");
        //wrap it inside a ViewHolder and return it
        ViewHolder viewHolder = new ViewHolder(groceryListView);
        return viewHolder;
    }

    //onBindViewHolder is responsible for taking data at a particular position and
    //putting it into a viewholder
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //get data model by position
        GroceryList groceryListItem = lists.get(position);

        //set the item views based on the data models
        TextView textView = holder.listTitle;
        textView.setText(groceryListItem.getListName());
        Log.d("MEJD", "Iamhere");
        TextView numItemsView = holder.numItems;
        numItemsView.setText(groceryListItem.getNumItems());

   }

    //get number of items in the data
    @Override
    public int getItemCount() {
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        //member variables
        public TextView listTitle;
        public TextView numItems;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            listTitle.findViewById(R.id.groceryListName);
            numItems.findViewById(R.id.numItemsList);
        }
    }
}

Fichier XML pour MainActivity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/homeToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#27D3C2"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:title="Add Grocery List"
        app:titleMarginStart="130dp" />

    <ImageButton
        android:id="@+id/addButton"
        android:layout_width="61dp"
        android:layout_height="56dp"
        android:layout_alignBottom="@+id/homeToolbar"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="322dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="29dp"
        android:layout_marginBottom="0dp"
        android:backgroundTint="#00FFFFFF"
        android:clickable="true"
        android:contentDescription="@+string/add list button"
        app:layout_constraintEnd_toEndOf="@+id/homeToolbar"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/homeToolbar"
        app:layout_constraintTop_toTopOf="@+id/homeToolbar"
        app:srcCompat="@drawable/add_icon"
        android:focusable="true" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/homeToolbar"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="1dp" />
</RelativeLayout>

XML pour la listeItem que je souhaite utiliser:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="15dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_begin="35dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/numItemsList"
    android:layout_width="28dp"
    android:layout_height="23dp"
    android:layout_marginEnd="34dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:text="@string/numberItems"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="15dp" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_end="35dp" />

<TextView
    android:id="@+id/groceryListName"
    android:layout_width="270dp"
    android:layout_height="22dp"
    android:layout_marginStart="35dp"
    android:layout_marginEnd="44dp"
    android:text="@string/groceryListName"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/numItemsList"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2
icanbelieveitsnotbutter 16 sept. 2020 à 04:50

2 réponses

Meilleure réponse

Ajouter itemCount

//
@Override
public int getItemCount() {
   return lists.size();
}

Modifier la classe ViewHolder

class ViewHolder extends RecyclerView.ViewHolder{
    //member variables
    public TextView listTitle;
    public TextView numItems;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        listTitle = itemView.findViewById(R.id.groceryListName);
        numItems = itemView.findViewById(R.id.numItemsList);
    }
}
0
Autocrab 16 sept. 2020 à 04:28

Peut être ça:

//get number of items in the data
    @Override
    public int getItemCount() {
        return 0;
    }

Éditer

//get number of items in the data
    @Override
    public int getItemCount() {
        return lists.size();
    }
0
Hoàng Vũ Anh 16 sept. 2020 à 02:01