ArrayList从Firebase数组适配器返回null

时间:2019-01-18 05:56:03

标签: java android firebase arraylist firebase-realtime-database

我正在尝试将从datasnapshot中获得的数据添加到arraylist中,但是它一直返回null。我知道我应该对此方法内部的UI进行任何更改,但是我需要能够在方法外部进行访问,以便可以将值返回到片段中。

我在datasnapshot内添加了打印语句,可以看到我正在获取所需的数据,但是在@Override函数范围之外,它仍然为空。我需要一种全局访问此方法的方法,以发送到我的arrayAdapter并返回实现此片段的其他活动。请帮忙。

public class YourTripTab extends Fragment {

    private DatabaseReference userReference;
    private String userID;
    private String Tag = "YourTripTab";
    private List<Trip> trips = new ArrayList<>();

    public void setUserID(String userID) {
        this.userID = userID;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View tripView = inflater.inflate(R.layout.your_trips, container, false);

        userReference = FirebaseDatabase.getInstance().getReference("userTrips").child(userID);

        //find Id from xml file
        ListView tripListView = (ListView) tripView.findViewById(R.id.trips);

        //create our TripListAdapter array adapter
        TripListAdapter tripListAdapter = new TripListAdapter(getContext(), retrieve());

        tripListView.setAdapter(tripListAdapter);

        return tripView;

    }

    public List<Trip> retrieve() {

        userReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        String startDate = ds.child("startDate").getValue().toString();
                        String tripName = ds.child("tripName").getValue().toString();
                        System.out.println("Trip Name:~ " + ds.getKey() + ":: startDate here ~ " + startDate + "//" + "tripName ~ " + tripName);
                        trips.add(new Trip(tripName, startDate));
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                //no data exists or error getting data
            }

        });

        System.out.println("Here is the size of trips list: " + trips.size());

        return trips;
    }

}

编辑

我设法解决了,这是解决方案

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        final View tripView = inflater.inflate(R.layout.your_trips, container, false);



        getData(new FirebaseCallback() {
            @Override
            public void onCallBack(List<Trip> list) {
                for(int i = 0; i < list.size(); i++){
                    trips.add(list.get(i));
                }
                tripListView = (ListView) tripView.findViewById(R.id.trips);
                tripListAdapter = new TripListAdapter(getContext(), trips);
                tripListView.setAdapter(tripListAdapter);
            }
        });



        return tripView;

    }


    private interface FirebaseCallback {

        void onCallBack(List<Trip> list);

    }


    private void getData(final FirebaseCallback firebaseCallback) {

        userReference = FirebaseDatabase.getInstance().getReference("userTrips").child(userID);
        userReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    List<Trip> tempList = new ArrayList<>();
                    System.out.println("dataSnapshot is here: " + dataSnapshot);

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {

                        String startDate = snapshot.child("startDate").getValue().toString();
                        String tripName = snapshot.child("tripName").getValue().toString();
                        System.out.println("startDate here: " + startDate + "\n" + "tripName: " + tripName);
                        tempList.add(new Trip(tripName, startDate));

                    }

                    firebaseCallback.onCallBack(tempList);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }```


1 个答案:

答案 0 :(得分:1)

addValueEventListener是异步的,并立即返回。每当有数据可用时,传递给它的回调将在未知的时间内被调用。这意味着您的retrieve方法还立即返回初始值为trips的空值。换句话说,第一次调用retrieve时,它会返回一个空的Trip对象列表。

如果您希望ListView显示数据,则应该在收集了所有行程之后,设置其适配器 。这意味着您应该在addValueEventListener回调中完成此操作。