gpt4 book ai didi

java - 为什么数组列表没有显示在回收器 View 中?

转载 作者:行者123 更新时间:2023-12-02 03:23:42 25 4
gpt4 key购买 nike

我使用 for 循环创建了一个 arraylist,该循环在我的模型类中创建了 100 个项目。然后,我尝试使用适配器将此数据绑定(bind)到我的回收器 View ,但由于某些奇怪的原因,这些项目没有显示在回收器 View 中。

我首先调查了我的适配器和 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 中完全没有显示任何内容。

最佳答案

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

关于java - 为什么数组列表没有显示在回收器 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com