简体   繁体   中英

Why is the arraylist not being shown in the recyclerview?

I created an arraylist by using a for loop that created 100 items in my model class. I then tried to bind this data to my recyclerview by using an adapter and for some strange reason, the items are not being displayed in the recyclerview.

I first investigated my adapter and recyclerview code to ensure that they were properly implemented and I did not notice anything out of the ordinary which led me to conclude that potentially the data is not actually being created.

Here is the code:

    package com.demoapp.moester96.petheart.Models;

    import android.content.Context;

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

    public class PetRecord {
        private static PetRecord sPetRecord;

        private List<Pet> mPets;

        public static PetRecord get(Context context)
        {
            if(sPetRecord == null)
            {
                sPetRecord = new PetRecord(context);
            }

            return sPetRecord;
        }

        private PetRecord(Context context)
        {
            mPets = new ArrayList<>();

            for(int i=0; i<100; ++i)
            {
                Pet pet = new Pet();
                pet.setmTitle("Pet #" + i);
                pet.setFaved(i % 2 == 0);
                mPets.add(pet);
            }
        }

        public List<Pet> getPets()
        {
            return mPets;
        }

        public Pet getPet(UUID id)
        {
            for(Pet pet : mPets)
            {
                if(pet.getmId().equals(id))
                {
                    return pet;
                }
            }

            return null;
        }

    }

    package com.demoapp.moester96.petheart;

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import com.demoapp.moester96.petheart.Models.Pet;
    import com.demoapp.moester96.petheart.Models.PetRecord;

    import java.util.List;

    public class PetListFragment extends Fragment {

        private class PetHolder extends RecyclerView.ViewHolder
        {
            private Pet mPet;
            private TextView mTitleTextView;
            private TextView mDateTextView;

            public PetHolder(LayoutInflater inflater, ViewGroup parent)
            {
                super(inflater.inflate(R.layout.list_item_pet,parent,false));

                mTitleTextView = (TextView) itemView.findViewById(R.id.pet_list_title);
                mDateTextView = (TextView) itemView.findViewById(R.id.pet_list_date);
            }

            public void bind(Pet pet)
            {
                mPet = pet;
                mTitleTextView.setText(mPet.getmTitle());
                mDateTextView.setText(mPet.getmDate().toString());
            }
        }

        private class PetAdapter extends RecyclerView.Adapter<PetHolder>
        {
            private List<Pet> mPets;

            public PetAdapter(List<Pet> pets)
            {
                mPets = pets;
            }

            @NonNull
            @Override
            public PetHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
                return new PetHolder(layoutInflater,parent);
            }

            @Override
            public void onBindViewHolder(@NonNull PetHolder petHolder, int i) {
                Pet pet = mPets.get(i);
                petHolder.bind(pet);
            }

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

        private RecyclerView mPetRecyclerView;
        private PetAdapter mAdapter;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
        }

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.activity_pet_list,container,false);
            mPetRecyclerView = (RecyclerView) view.findViewById(R.id.pet_recycler_view);
            mPetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            updateUI();
            return view;
        }

        private void updateUI()
        {
            PetRecord petRecord = PetRecord.get(getActivity());
            List<Pet> pets = petRecord.getPets();

            mAdapter = new PetAdapter(pets);
            mPetRecyclerView.setAdapter(mAdapter);
        }
    }

I expected to have a list of 100 items that include the pet number, date and time in the recyclerview instead, I got a recyclerview that showed absolutely nothing in it.

First of all, you should call:

mPetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

after you set the adapter, not before. That's how that works. Also, do you have a .xml file for the item design? Should add it on the adapter where you say:

@NonNull
@Override
public PetHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext();
    layoutInflater.inflate(R.layout.insert_your_design_element, parent,false);
    return new PetHolder(layoutInflater,parent);
}

after setAdapter should be call notifyDataSetChanged

    recyclerView = layout.findViewById(R.id.list_recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
    ListAdapter adapter = new ListAdapter(getActivity());
    recyclerView.setAdapter(adapter);
    adapter.setList(searchList);
    adapter.notifyDataSetChanged();

The view should be created in onCreateViewHolder and should be passed to the view holder class.Then in your ViewHolder class you haven't created the view but you have used itemView to initilize the views of the layout.

public class PetListFragment extends Fragment {
   Context context;

   @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context; 
        //this is one of the best way to get context of the activity to which the particular activity is associated with
      }
    private class PetHolder extends RecyclerView.ViewHolder
    {
        private Pet mPet;
        private TextView mTitleTextView;
        private TextView mDateTextView;

        public PetHolder(@NonNull View itemView)
        {
             super(itemView);
            mTitleTextView = (TextView) itemView.findViewById(R.id.pet_list_title);
            mDateTextView = (TextView) itemView.findViewById(R.id.pet_list_date);
        }

        public void bind(Pet pet)
        {
            mPet = pet;
            mTitleTextView.setText(mPet.getmTitle());
            mDateTextView.setText(mPet.getmDate().toString());
        }
    }

    private class PetAdapter extends RecyclerView.Adapter<PetHolder>
    {
        private List<Pet> mPets;
        private Context context;
        public PetAdapter(List<Pet> pets,Context context) 
        {
            this.context=context;
            mPets = pets;
        }

        @NonNull
        @Override
        public PetHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            View view=layoutInflater.inflate(R.layout.list_item_pet,parent,false);
            return new PetHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull PetHolder petHolder, int i) {
            Pet pet = mPets.get(i);
            petHolder.bind(pet);
        }

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

    private RecyclerView mPetRecyclerView;
    private PetAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_pet_list,container,false);
        mPetRecyclerView = (RecyclerView) view.findViewById(R.id.pet_recycler_view);
        mPetRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        updateUI();
        return view;
    }

    private void updateUI()
    {
        PetRecord petRecord = PetRecord.get(context);
        List<Pet> pets = petRecord.getPets();

        mAdapter = new PetAdapter(pets,context);
        mPetRecyclerView.setAdapter(mAdapter);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM