0
Firestoreからデータを取得しようとしていますが、CardView
を使用して表示しています。 必要なコードはすべて入っているようですが、アプリを実行するとデータは表示されません。アプリはクラッシュすることなく実行されます。問題がどこにあるのか分かりません。 誰かが私に行方不明または間違っていることを示すことができるなら、私は非常に感謝します。Cardviewを使用してFirestoreからデータを表示
主な活動:
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);
}
}
});
}
}
リサイクラー:
<!-- 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" />
CardView:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardElevation="10dp">
<LinearLayout
android:orientation="vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/restaurant_item_name"
android:textSize="30sp"
android:textColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/restaurant_item_description"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:textStyle="italic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
あなたの 'onActivityResult'は何かがあれば空ですか? –