我确实在电影院应用程序上进行了投影。我想列出我的Cloud Firestore中可用的电影院,但是以某种方式创建的应用程序没有显示我的电影院。有时会显示出来,但是当我进入第二项活动并回到主要活动时,回收者视图为空。我认为我的onStart和onStop是正确的。我确实检查了我的LocationAdapter.java,Location.java,cinemalist.xml和activity_home.xml。一切似乎都很好,但是当我尝试启动我的应用程序时,仅显示按钮。有人可以帮助我吗? 这是我的activity_home.xml,下面是我运行应用程序时的结果。
我的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>
已编辑:我删除了LocationAdapter.java,并将使用FirestoreRecyclerAdapter的匿名实现放入您的HomeActivity
答案 0 :(得分:1)
供参考: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);
}
}
答案 1 :(得分:1)
请更新此行
recyclerView.setHasFixedSize(true);
作者
recyclerView.setHasFixedSize(false);