FirebaseReclycerView未显示

时间:2016-11-27 20:13:45

标签: android firebase firebase-realtime-database firebaseui

我有一个没有在屏幕上显示的FirebaseReclycerView。我在日志中有以下回复:

  

E / RecyclerView:没有连接适配器;跳过布局

以下是活动代码:

public class HistoricActivity extends CommonActivity{
private static final String TAG = HistoricActivity.class.getSimpleName();
private String id, reference;
private boolean typeUser;
private TextView tvNoHistoric;
private ImageView ivHistoric;
private RecyclerView rvHistoric;
private HistoricAdapter adapter;
private CardView cardView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_historic);
    Intent it = getIntent();
    params = it.getExtras();
    if (params != null) {
        id = params.getString("id");
        typeUser = params.getBoolean("type");
    }
    initViews();
}

@Override
protected void onResume() {
    super.onResume();
    loadHistoric();
}

private void loadHistoric() {
    openProgressBar();
    if (typeUser){
        reference = "bakeries";
    } else {
        reference = "users";
    }
    mDatabaseReference.child(reference).child(id).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChild("historic")) {
                closeProgressBar();
                if (tvNoHistoric.getVisibility() == View.VISIBLE) {
                    tvNoHistoric.setVisibility(View.GONE);
                }
                if (ivHistoric.getVisibility() == View.VISIBLE) {
                    ivHistoric.setVisibility(View.GONE);
                }
                cardView.setVisibility(View.VISIBLE);
                adapter = new HistoricAdapter(mDatabaseReference.child(reference).child(id).child("historic").getRef(),
                        HistoricActivity.this
                ) {};
                rvHistoric.setHasFixedSize(true);
                rvHistoric.setItemAnimator(new DefaultItemAnimator());
                rvHistoric.setLayoutManager(new LinearLayoutManager(HistoricActivity.this));
                rvHistoric.addItemDecoration(new DividerItemDecoration(HistoricActivity.this));
                rvHistoric.setAdapter(adapter);
            } else {
                closeProgressBar();
                tvNoHistoric.setVisibility(View.VISIBLE);
                ivHistoric.setVisibility(View.VISIBLE);
                cardView.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        }
    });
}

@Override
protected void initViews() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_historic);
    setSupportActionBar(toolbar);
    setTitle("Histórico");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    tvNoHistoric = (TextView) findViewById(R.id.tv_no_historic);
    progressBar = (ProgressBar) findViewById(R.id.simpleProgressBar);
    ivHistoric = (ImageView) findViewById(R.id.ic_historic);
    rvHistoric = (RecyclerView) findViewById(R.id.rv_historic);
    cardView = (CardView) findViewById(R.id.card_historic);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
}

适配器:

public class HistoricAdapter extends FirebaseRecyclerAdapter<Historic, HistoricViewHolder> {
private static final String TAG = HistoricAdapter.class.getSimpleName();
private Context mContext;

public HistoricAdapter(Query ref, Context context) {
    super(Historic.class, R.layout.item_historic, HistoricViewHolder.class, ref);
    this.mContext = context;
}

@Override
protected void populateViewHolder(final HistoricViewHolder viewHolder, final Historic model, final int position) {
    viewHolder.tvData.setText(model.getDate());
    viewHolder.tvMsg.setText(model.getMsg());
}
}

ViewHolder:

public class HistoricViewHolder extends RecyclerView.ViewHolder{

public TextView tvData, tvMsg;

public HistoricViewHolder(View v){
    super(v);
    tvData = (TextView) itemView.findViewById(R.id.tv_data);
    tvMsg = (TextView) itemView.findViewById(R.id.tv_msg);
}
}

XML:

<LinearLayout 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"
android:background="#CFCFCF"
android:orientation="vertical"
tools:context=".activity.HistoricActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_historic"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ProgressBar
            android:id="@+id/simpleProgressBar"
            android:layout_width="60sp"
            android:layout_height="60sp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/ic_historic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginTop="40dp"
            android:src="@drawable/ic_history_black_36dp"
            android:visibility="invisible" />

        <com.devspark.robototextview.widget.RobotoTextView
            android:id="@+id/tv_no_historic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginTop="100dp"
            android:text="@string/no_historic"
            android:textColor="@color/black_50_opacity"
            android:textSize="15sp"
            android:visibility="invisible"
            app:typeface="roboto_light_italic" />

        <android.support.v7.widget.CardView
            android:id="@+id/card_historic"
            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="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:elevation="4dp"
            android:visibility="invisible"
            app:cardCornerRadius="2dp">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_historic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </android.support.v7.widget.CardView>

    </RelativeLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您需要在onCreate()方法中初始化并设置一个空适配器。所有这些都应该在你的initViews()方法中:

adapter = new HistoricAdapter(mDatabaseReference.child(reference).child(id).child("historic").getRef(),
                    HistoricActivity.this
            ) {};
            rvHistoric.setHasFixedSize(true);
            rvHistoric.setItemAnimator(new DefaultItemAnimator());
            rvHistoric.setLayoutManager(new LinearLayoutManager(HistoricActivity.this));
            rvHistoric.addItemDecoration(new DividerItemDecoration(HistoricActivity.this));
            rvHistoric.setAdapter(adapter);

你的initViews()在loadHistoric()之前运行,所以有一个没有连接适配器的小时刻。