来自Firebase的数据不会显示在我的RecyclerView中

时间:2017-01-09 19:49:40

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

数据正在FirebaseHelper的fetchData()方法中获取,但实际上并未存储在CardViewAdapter中的变量petInfo和imgURL中。这导致RecyclerView片段中没有显示卡片。当应用程序初始化时,数据集为0,运行fetchData,数据集是项目的大小,但保留petInfo和imgURL null。

FirebaseHelper:

IndexedSeq

适配器:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

您对helper.retrieve()的来电正在开始mDatabase.addChildEventListener,其结果将异步返回....与此同时,您将从该方法返回空列表(默认值为{ {1}})。您需要在结果返回时更新适配器(在您调用animal之后)