设置适配器后,不会执行ValueEventListener

时间:2016-10-22 22:22:02

标签: android listview android-fragments firebase firebase-realtime-database

我有一个viewPager,在每个viewPager中我都有一个ListView。\

当我运行我的应用时,在CourseListAdapter中抛出IndexOutOfBoundsException,因为ValueEventListener未执行...

但是当我删除listView.setAdapter(mAdapter);时,我的列表视图将不会被填充,但我会在记录器中看到D/CourseListAdapter: Getting information ..

My Fragment设置ListView:

public class DayFragment
        extends Fragment {

    private static final String TAG = "DayFragment";

    private CourseListAdapter mAdapter;

    private int day = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.course_list, container, false);

        Bundle bundle = getArguments();

        // Yay a day is given
        if (bundle != null) {
            day = bundle.getInt("day");
        }

        ListView listView = (ListView) rootView.findViewById(R.id.list_view);

        mAdapter = new CourseListAdapter(getActivity(), day);
        listView.setAdapter(mAdapter);

        return rootView;
    }

    @Override
    public void onStop() {
        super.onStop();

        Log.i(TAG, "Removing the listener");

        // Clean up
        mAdapter.removeListener();
    }
}

我的ListView适配器:

public class CourseListAdapter
        extends BaseAdapter {

    private static final String TAG = "CourseListAdapter";
    private Context mContext;
    private List<Day> mDays = new ArrayList<>();
    private int mDay = 0;
    private ValueEventListener mListener;
    private FirebaseDatabase mDatabase;
    private DatabaseReference mReference;

    public CourseListAdapter(final Activity context, int day) {
        mContext = context;
        mDay = day;

        // Setup Firebase
        mDatabase = FirebaseDatabase.getInstance();
        mReference = mDatabase.getReference();

        mListener = mReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d(TAG, "Getting information");

                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    Day day = postSnapshot.getValue(Day.class);

                    for (DataSnapshot course : postSnapshot.child("courses").getChildren()) {
                        day.addCourse(course.getValue(Course.class));
                    }

                    mDays.add(day);
                }

                // Tell some data changed
                notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.e(TAG, "Listen was cancelled, no more updates will occur");
            }
        });
    }

    public void removeListener() {
        mReference.removeEventListener(mListener);
    }

    @Override
    public int getCount() {
        Day day = mDays.get(mDay);

        return day.getCourses().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Day day = mDays.get(mDay);
        ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.course_items, parent, false);

            viewHolder.course = (TextView) convertView.findViewById(R.id.course);
            viewHolder.classRoom = (TextView) convertView.findViewById(R.id.class_room);
            viewHolder.begin = (TextView) convertView.findViewById(R.id.begin);
            viewHolder.end = (TextView) convertView.findViewById(R.id.end);
            viewHolder.teacher = (TextView) convertView.findViewById(R.id.teacher);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.course.setText(day.getCourses().get(position).name);
        viewHolder.classRoom.setText(day.getCourses().get(position).classRoom);
        viewHolder.begin.setText(day.getCourses().get(position).from);
        viewHolder.end.setText(day.getCourses().get(position).to);
        viewHolder.teacher.setText(day.getCourses().get(position).teacher);

        return convertView;
    }

    private class ViewHolder {
        TextView course;
        TextView classRoom;
        TextView begin;
        TextView end;
        TextView teacher;
    }
}

1 个答案:

答案 0 :(得分:0)

从Firebase同步数据是一种异步操作,这意味着它不会立即发生,因此您首先需要从firebase获取所需的所有数据,并且只有在完成所有数据的收集后,才能设置listview适配器。我的意思是你只需要在onDataChange完成时设置适配器,你可以创建一个公共静态方法来设置适配器,并在for循环后从onDataChange调用它。