java - 为什么一开始启动应用程序时我的回收 View 没有显示?

标签 java android android-recyclerview google-cloud-firestore

我在电影院应用程序上做了项目。我想列出我的云 Firestore 中可用的电影院,但不知何故,我创建的应用程序没有显示我的电影院。有时它会出现,但当我进入第二个 Activity 并返回主要 Activity 时,回收器 View 为空。我认为我的 onStart 和 onStop 是正确的。我检查了我的 LocationAdapter.java、Location.java、cinemalist.xml 和 Activity_home.xml。一切看起来都很好,但是当我尝试启动我的应用程序时,只显示按钮。有人可以帮助我吗? 这是我的 Activity_home.xml,下面是我运行应用程序时的结果。

enter image description here

enter image description here

我的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>

电影列表.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>

这是我的数据库 enter image description here

已编辑:我删除了 LocationAdapter.java 并将 use FirestoreRecyclerAdapter 匿名实现放入您的 HomeActivity

最佳答案

供引用:Docs

编辑:

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);

    }
}

关于java - 为什么一开始启动应用程序时我的回收 View 没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59284438/

相关文章:

java - 遍历具有不同长度延迟的文本

android - 如何在 Panda board 上移植 Android kitkat?

android - 回收站 View 未在布局中显示任何内容

java - Android 应用程序按钮按下无法正常工作

android - 具有多种布局的 RecyclerView

android - RecyclerView viewholder 中的动态项目

java - 如何将抗锯齿应用于 GraphicsContext.fillArc() 等 javafx 方法?

java - 如何在 JAX-WS Web 服务中返回自定义复杂类型?

java - 字符串到数组 Int

android - 为什么 Android Studio 3 默认使用 cmake 3.6.0?