RecyclerView在活动开始时未调用onCreateViewHolder

时间:2018-06-22 10:29:59

标签: java android android-recyclerview

我一直在尝试实现一个简单的可滚动列表,该列表在名为MyOrganisedEventsActivity的活动中使用RecyclerView从Firebase数据库获取其数据。问题是,当我的主要活动启动MyOrganisedEventsActivity时,没有对onCreateViewHolder方法进行调用,因此RecyclerView为空。

但是,稍等片刻然后锁定和解锁我的手机后,将调用onCreateViewHolder,并且列表项开始出现在RecyclerView上。刚开始活动时,是否有强制呼叫的方法?还是我的代码有问题,如何解决?

到目前为止,这是我的代码:

MyOrganisedEventsActivity.java:

package com.llawl.tristonpang.intheloop;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyOrganisedEventsActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private OrganisedEventsAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<EventInfo> mEventsDataset;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_organised_events);
        mRecyclerView = (RecyclerView) findViewById(R.id.org_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        //mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mEventsDataset = new ArrayList<>();

        // specify an adapter (see also next example)
        mAdapter = new OrganisedEventsAdapter(mEventsDataset);
        mRecyclerView.setAdapter(mAdapter);

        prepareEventsData();
    }

    private void prepareEventsData() {
        final String currentUser = FirebaseAuth.getInstance().getCurrentUser().getEmail();
        Log.d("InTheLoop", "prepareEventsData(), currentUser = " + currentUser);
        FirebaseDatabase.getInstance().getReference().child("events_info").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    //EventInfo event = snapshot.getValue(EventInfo.class);
                    HashMap<String,String> data = (HashMap) snapshot.getValue();
                    //Log.d("InTheLoop", "Event name: " + event.getName());
                    //if (event.getOrganiser().equals(currentUser)) {
                    if (data.get("organiser").equals(currentUser)) {
                        Log.d("InTheLoop", "Adding event: " + data.get("name"));
                        EventInfo event = new EventInfo(data.get("name"), data.get("date"), data.get("time"), data.get("venue"),
                                data.get("desc"), data.get("imageName"), data.get("organiser"));
                        mEventsDataset.add(event);
                    }



                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        mAdapter.notifyDataSetChanged();

    }
}

OrganisedEventsAdapter.java:

package com.llawl.tristonpang.intheloop;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class OrganisedEventsAdapter extends RecyclerView.Adapter<OrganisedEventsAdapter.ViewHolder> {
    private List<EventInfo> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mName;
        public TextView mVenue;
        public TextView mDate;

        public ViewHolder(View v) {
            super(v);
            mName = v.findViewById(R.id.org_row_name);
            mVenue = v.findViewById(R.id.org_row_venue);
            mDate = v.findViewById(R.id.org_row_date);
        }
    }

    public OrganisedEventsAdapter(List<EventInfo> dataset) {
        mDataset = dataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public OrganisedEventsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.organised_event_row, parent, false);
        Log.d("InTheLoop", "Adapter, onCreateViewHolder");

        return new ViewHolder(v);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        EventInfo event = mDataset.get(position);
        Log.d("InTheLoop", "Adapter, Event name: " + event.getName());
        holder.mName.setText(event.getName());
        holder.mVenue.setText(event.getVenue());
        holder.mDate.setText(event.getDate());
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}

EventInfo.java:

package com.llawl.tristonpang.intheloop;

public class EventInfo {
    private String mName;
    private String mDate;
    private String mTime;
    private String mVenue;
    private String mDesc;
    private String mImageName;
    private String mOrganiser;

    public EventInfo() {
    }

    public EventInfo(String name, String date, String time, String venue, String desc, String imageName, String organiser) {
        mName = name;
        mDate = date;
        mTime = time;
        mVenue = venue;
        mDesc = desc;
        mImageName = imageName;
        mOrganiser = organiser;
    }

    public String getName() {
        return mName;
    }

    public String getDate() {
        return mDate;
    }

    public String getTime() {
        return mTime;
    }

    public String getVenue() {
        return mVenue;
    }

    public String getDesc() {
        return mDesc;
    }

    public String getImageName() {
        return mImageName;
    }

    public String getOrganiser() {
        return mOrganiser;
    }
}

organized_event_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/single_event_row"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/row_padding_vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/row_padding_vertical">

    <TextView
        android:id="@+id/org_row_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="name"
        android:textColor="@color/black"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/org_row_venue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/org_row_name"
        android:text="venue" />

    <TextView
        android:id="@+id/org_row_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="date"
        android:textColor="@color/black" />

</RelativeLayout>

activity_my_organised_events.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyOrganisedEventsActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/org_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

非常感谢您的帮助!谢谢!

2 个答案:

答案 0 :(得分:0)

mAdapter.notifyDataSetChanged();内移动onDataChange(DataSnapshot dataSnapshot)。您已经错过了异步调用的本质。

 FirebaseDatabase.getInstance().getReference().child("events_info").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                // Build data here
            }
            mAdapter.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 1 :(得分:0)

mAdapter.notifyDataSetChanged();的{​​{1}}的{​​{1}}中添加数据后,调用List

因此基本上可以在数据结构中添加数据,然后通知适配器使用新数据进行更新。