使用Cardview从Firestore显示数据

时间:2017-10-25 14:49:46

标签: android firebase android-cardview google-cloud-firestore

我正在尝试从Firestore获取数据,然后使用 public static void HideByRemoval(this TabPage tp) { TabControl tc = tp.Parent as TabControl; if (tc != null && tc.TabPages.Contains(tp)) { // Store TabControl and Index tp.Tag = new Tuple<TabControl, Int32>(tc, tc.TabPages.IndexOf(tp)); tc.TabPages.Remove(tp); } } public static void ShowByInsertion(this TabPage tp) { Tuple<TabControl, Int32> tagObj = tp.Tag as Tuple<TabControl, Int32>; if (tagObj?.Item1 != null) { // Restore TabControl and Index tagObj.Item1.TabPages.Insert(tagObj.Item2, tp); } } 显示它。 它似乎获得了所有必要的代码,但是当我运行应用程序时,没有显示任何数据。该应用程序运行没有崩溃。我无法弄清楚问题出在哪里。 如果有人能告诉我我失踪的地方或错误的地方,我将非常感激。

enter image description here

主要活动:

CardView

餐厅适配器

public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    RestaurantAdapter.OnRestaurantSelectedListener {


private static final String TAG = "MainActivity";

private static final int RC_SIGN_IN = 9001;


// FireStore
@BindView(R.id.recycler_restaurants)
RecyclerView mRestaurantsRecycler;

private FirebaseFirestore mFirestore;
private Query mQuery;

private RestaurantAdapter mAdapter;


// FireStore END


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ButterKnife.bind(this);


    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);

    // Firestore
    mFirestore = FirebaseFirestore.getInstance();

    // Get ${LIMIT} restaurants
    mQuery = mFirestore.collection("restaurants")
            .limit(5);


    // RecyclerView
    mAdapter = new RestaurantAdapter(mQuery, this);


    mRestaurantsRecycler.setLayoutManager(new LinearLayoutManager(this));
    mRestaurantsRecycler.setAdapter(mAdapter);


}


@Override
protected void onStart() {
    super.onStart();

    // Start listening for Firestore updates
    if (mAdapter != null) {
        mAdapter.startListening();
    }

}


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

    if (mAdapter != null) {
        mAdapter.stopListening();
    }

}


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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    }
    return super.onOptionsItemSelected(item);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {

    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}


@Override
public void onRestaurantSelected(DocumentSnapshot restaurant) {

}

回收站:

public class RestaurantAdapter extends FireStoreAdapter<RestaurantAdapter.ViewHolder> {


public interface OnRestaurantSelectedListener {

    void onRestaurantSelected(DocumentSnapshot restaurant);

}


private OnRestaurantSelectedListener mListener;


public RestaurantAdapter(Query query, OnRestaurantSelectedListener listener) {
    super(query);
    mListener = listener;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    return new ViewHolder(inflater.inflate(R.layout.item_restaurant, parent, false));
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(getSnapshot(position), mListener);
}


static class ViewHolder extends RecyclerView.ViewHolder {


    @BindView(R.id.restaurant_item_name)
    TextView nameView;

    @BindView(R.id.restaurant_item_description)
    TextView descriptionView;


    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    public void bind(final DocumentSnapshot snapshot,
                     final OnRestaurantSelectedListener listener) {

        Restaurant restaurant = snapshot.toObject(Restaurant.class);


        nameView.setText(restaurant.getName());
        descriptionView.setText(restaurant.getDescription());


        // Click listener
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onRestaurantSelected(snapshot);
                }
            }
        });
    }

}

CardView:

<!-- Main Restaurants recycler -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_restaurants"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    tools:listitem="@layout/item_restaurant"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="8dp" />

1 个答案:

答案 0 :(得分:0)

尝试设置调试断点。我想你正在打电话

mAdapter.startListening()
在实例化适配器之前

尝试从onStart()中删除它并在适配器设置后放置。 如果结果开始出现,请根据需要重构代码。