I did project on cinemas app. I want to list up the cinema that available from my cloud firestore, but somehow the application i created did not show my cinema. Sometimes it show up but when I got to second activity and come back to the main activity, the recycler view is empty. I think my onStart and onStop is correct. I did check on my LocationAdapter.java, Location.java,cinemalist.xml, and activity_home.xml. Everything seems fine but when i try to launch my app the only the button show up. Anyone can help me? This is my activity_home.xml and below it is the result when i run my app.
My HomeActivity.java
public class HomeActivity extends AppCompatActivity {
RecyclerView recyclerView;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference movieRef = db.collection("cinemasLocation");
RecyclerView.LayoutManager layoutManager;
private FirestoreRecyclerAdapter<Location, LocationViewHolder> adapter;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button buttonPI = (Button)findViewById(R.id.bPI);
Button buttonMA = (Button)findViewById(R.id.bMovie);
Toolbar toolbar = (Toolbar)findViewById(R.id.TB);
setSupportActionBar(toolbar);
buttonPI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (HomeActivity.this,AccountInformation.class);
startActivity(intent);
}
});
buttonMA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeActivity.this,MovieActivity.class);
startActivity(intent);
}
});
Query query = movieRef.orderBy("title",Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Location> options = new FirestoreRecyclerOptions.Builder<Location>()
.setQuery(query,Location.class)
.build();
recyclerView = findViewById(R.id.rvCinema);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new FirestoreRecyclerAdapter<Location, LocationViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull LocationViewHolder holder, int position, @NonNull Location model) {
holder.setData(model.address,model.image,model.located,model.phone,model.title);
}
@NonNull
@Override
public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cinemalist,parent,false);
return new LocationViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
private class LocationViewHolder extends RecyclerView.ViewHolder{
private View view;
LocationViewHolder(View itemView){
super(itemView);
view = itemView;
}
void setData(String address, String image, String located, String phone, String title){
TextView tvTitleLocation;
TextView tvLocation;
TextView tvPhone;
ImageView moviePicture;
tvTitleLocation = itemView.findViewById(R.id.tvTitleLocation);
tvLocation = itemView.findViewById(R.id.tvAddress);
tvPhone = itemView.findViewById(R.id.tvContact);
moviePicture = itemView.findViewById(R.id.ivLocation);
// Set data to views
tvTitleLocation.setText(title);
tvLocation.setText(located);
tvPhone.setText(phone);
Picasso.get().load(image).into(moviePicture);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.logOut:
FirebaseAuth.getInstance().signOut();
Intent intToMain = new Intent ( HomeActivity.this,MainActivity.class);
startActivity(intToMain);
finish();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
onResume();
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
}
activtiy_home.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".HomeActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/TB"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/TB"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvCinema"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp" />
<Button
android:id="@+id/bPI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Personal Information" />
<Space
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/bMovie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Movie Available" />
</LinearLayout>
</RelativeLayout>
movielist.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
app:contentPadding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/ivMovie"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:background="@drawable/loading"
android:scaleType="centerCrop"></ImageView>
<TextView
android:id="@+id/tvReleaseDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ReleaseDate"
android:textSize="20sp" />
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Description" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Edited: I remove my LocationAdapter.java and put use FirestoreRecyclerAdapter annonymous implementation inside your HomeActivity