【问题标题】:Data from Firebase won't show in my RecyclerView来自 Firebase 的数据不会显示在我的 RecyclerView 中
【发布时间】:2017-05-24 04:08:06
【问题描述】:

数据是在 FirebaseHelper 的 fetchData() 方法中获取的,但实际上并未存储在 CardViewAdapter 的变量 petInfo 和 imgURL 中。这导致 RecyclerView 片段中没有显示任何卡片。应用初始化时,数据集为 0,遍历 fetchData,数据集为项目大小,但 petInfo 和 imgURL 为空。

FirebaseHelper:

public class FirebaseHelper {
    private DatabaseReference mDatabase;


    Boolean saved = null;
    ArrayList<AnimalType> animal = new ArrayList<>();

    public FirebaseHelper(DatabaseReference mDatabase) {
        this.mDatabase = mDatabase;
    }

    //Save
    public Boolean save (AnimalType animalType){
        if (animalType==null){
            saved = false;
        }
        else{
            try{
                mDatabase.child("AnimalType").push().setValue(animalType);
                saved=true;
            }catch (DatabaseException e){
                e.printStackTrace();
                saved=false;
            }
        }
        return saved;
    }

    //Read
    public ArrayList<AnimalType> retrieve(){
        mDatabase.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                fetchData(dataSnapshot);
                Log.i(TAG, "onChildAdded");
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                fetchData(dataSnapshot);
                Log.i(TAG, "onChildChanged");
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return animal;
    }

    private void fetchData (DataSnapshot dataSnapshot){
        animal.clear();
        for (DataSnapshot ds : dataSnapshot.getChildren()){
            AnimalType animalType = new AnimalType();
            animalType.setPetInfo(ds.getValue(AnimalType.class).getPetInfo());
            animalType.setImgURL(ds.getValue(AnimalType.class).getImgURL());
            animal.add(animalType);
        }
    }

}

适配器:

public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.ViewHolder> {

    Context mContext;
    private List<AnimalType> mAnimalData = new ArrayList<>();

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView petInfo;
        public ImageView imgURL;
        public ViewHolder(View view){
            super(view);
            imgURL = (ImageView) view.findViewById(R.id.pet_image);
            petInfo = (TextView) view.findViewById(R.id.pet_description);
        }
    }

    //constructor
    public CardViewAdapter(Context mContext, List<AnimalType> mAnimalData){
        this.mAnimalData = mAnimalData;
    }

    //create new views
    @Override
    public CardViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);

        mContext = parent.getContext();

        return viewHolder;
    }

    //replace contents of view
    @Override
    public void onBindViewHolder(ViewHolder holder, int position){
        holder.petInfo.setText(mAnimalData.get(position).getPetInfo());
        PicassoClient.downloadImage(mContext,mAnimalData.get(position).getImgURL(), holder.imgURL);


    }

    //return size of dataset
    public int getItemCount(){
        return mAnimalData.size();
    }
}

片段:

public class DogFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mCardAdapter;
    private RecyclerView.LayoutManager mCardLayoutManager;

    DatabaseReference mDatabaseReference;
    FirebaseHelper helper;


    public static DogFragment newInstance(int page) {
        DogFragment dogFragment = new DogFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        dogFragment.setArguments(args);
        return dogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_dog, container, false);

        //cardview
        mRecyclerView = (RecyclerView)rootView.findViewById(R.id.card_view);

        //setup firebase
        mDatabaseReference = FirebaseDatabase.getInstance().getReference();
        helper= new FirebaseHelper(mDatabaseReference);

        //create adapter class
        //mCardAdapter = new CardViewAdapter(mAimalTypeList);
        mCardAdapter = new CardViewAdapter(getActivity().getApplicationContext(), helper.retrieve());
        mRecyclerView.setAdapter(mCardAdapter);

        //add linear layout manager
        mCardLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mCardLayoutManager);

        //preparePetData();

        return rootView;
    }
}

毕加索:

public class PicassoClient {

    public static void downloadImage(Context context, String url, ImageView img){
        if(url != null && url.length()>0){
            Picasso.with(context).load(url).placeholder(R.drawable.placeholder).into(img);
        }
        else {
            Picasso.with(context).load(R.drawable.placeholder).into(img);
        }
    }
}

【问题讨论】:

    标签: android android-recyclerview firebase-realtime-database picasso android-cardview


    【解决方案1】:

    您对helper.retrieve() 的调用正在启动mDatabase.addChildEventListener,其结果将异步返回....同时您从该方法返回空列表(默认值为animal)。当结果返回时,您需要更新适配器(在您调用 fetchData(dataSnapshot); 之后)

    【讨论】:

    • 谢谢,我认为发生了类似的事情。我对android编程比较陌生。如何更新适配器,以便在返回结果时实际填充数据?
    • 有几种方法可以做到这一点....一种选择是向适配器添加类似update(List&lt;AnimalType&gt; animalData) 的方法,在得到结果后调用它(并调用@ 987654326@ 在那个方法中)。
    • 谢谢!我最终做的是将 fetchData 并检索到主片段中,以便我可以轻松添加 notifyDataSetchanged
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2020-06-25
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多