我使用for循环创建了一个数组列表,该循环在我的模型类中创建了100个项目。然后,我尝试使用适配器将这些数据绑定到我的recyclerview上,由于某些奇怪的原因,这些项目没有显示在recyclerview中。
我首先研究了我的适配器和recyclerview代码,以确保它们得到正确实施,并且我没有发现任何与众不同的地方,这使我得出结论,实际上并没有真正创建数据。
代码如下:
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);
}
}
我希望在recyclerview中包含100个物品的清单,其中包括宠物的编号,日期和时间,而我得到的recyclerview中绝对没有任何内容。
答案 0 :(得分:0)
首先,您应该致电:
mPetRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
在设置适配器之后,而不是之前。就是这样。另外,您是否有用于项目设计的.xml文件?应该将其添加到您说的适配器上:
@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);
}
答案 1 :(得分:0)
setAdapter之后应调用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();
答案 2 :(得分:0)
应该在
onCreateViewHolder
中创建视图,并将其传递给视图持有者类。然后,在ViewHolder类中,您尚未创建视图,但已使用itemView
来初始化视图布局。
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);
}
}