0

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.

enter image description here

enter image description here

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>

This is my database enter image description here

Edited: I remove my LocationAdapter.java and put use FirestoreRecyclerAdapter annonymous implementation inside your HomeActivity

2 Answers2

1

For reference : Docs

Edit :

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(phont);
        moviePicture.setText(image);

    }
}
Kushal
  • 8,100
  • 9
  • 63
  • 82
  • Cannot resolve method for `return locations.get(position);` and `return locations.size();` – MUHAMAD NUR FIKRY BIN MARJAMAL Dec 11 '19 at 11:22
  • Basically, you fixed the problem. Now, we need to check how can we extract `List of Locations` from `options` parameter. I'm unaware about it, if you can get it, just replace the copying line in constructor of `LocationAdapter` – Kushal Dec 11 '19 at 11:27
  • Can you give a try by removing `constructor`, `getItem()` and `getItemCount()` methods all together? And just use `FirestoreRecyclerAdapter` annonymous implementation inside your `HomeActivity`. Ref : https://stackoverflow.com/a/49277842/1994950 – Kushal Dec 11 '19 at 11:35
  • Please check my editted section of answer, you are 1% away of your output :) – Kushal Dec 11 '19 at 12:01
  • I edit my code back to change the query options to from "releaseDate" to "title". then it show up. but when i go to another activity and go back to home activity the recycler adapter gone – MUHAMAD NUR FIKRY BIN MARJAMAL Dec 11 '19 at 12:27
  • When you are coming back to `HomeActivity`, does `onCreate()` gets called? As I suspect if you are opening new activity as partial or not-fullscreen. – Kushal Dec 11 '19 at 12:32
  • I'm not sure about that, i check my logcat this is what appear `V/FA: Activity resumed, time: 27095932 D/EGL_emulation: eglMakeCurrent: 0x9af862a0: ver 3 0 (tinfo 0xa2e1b440) D/FA: Logging event (FE): screen_view(_vs), Bundle[{ga_event_origin(_o)=auto, ga_previous_class(_pc)=MovieActivity, ga_previous_id(_pi)=1696508578162266275, ga_screen_class(_sc)=HomeActivity, ga_screen_id(_si)=1696508578162266274}]` – MUHAMAD NUR FIKRY BIN MARJAMAL Dec 11 '19 at 12:40
  • Can you put `Log.d("TAG","onCreate() called");` statement in `onCreate()` and all others like `onStart()`, `onStop()` etc. If your method gets called, these logs would get print in `logcat` – Kushal Dec 11 '19 at 13:11
  • this is the result when i go from second activity to home activity [picture](https://ibb.co/yPSkvmV) – MUHAMAD NUR FIKRY BIN MARJAMAL Dec 11 '19 at 13:30
1

Please update this line

recyclerView.setHasFixedSize(true);

by

recyclerView.setHasFixedSize(false);
Nilesh Rathore
  • 886
  • 1
  • 12
  • 22