所以我想为我的导航抽屉做一个家庭碎片,这是我的默认家庭布局,但是以某种方式我不知道我弄错了什么。代码本身在工作室中没有错误,但是当我打开它时,它停止工作。家庭碎片包含来自Firestore和recycleview的数据。
片段
public class Fragment_HomeSpot extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference spotRef = db.collection("Spot");
private SpotAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setUpRecyclerView();
}
private void setUpRecyclerView() {
Query query = spotRef.orderBy("",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Spot> options = new FirestoreRecyclerOptions.Builder<Spot>()
.setQuery(query, Spot.class)
.build();
adapter = new SpotAdapter(options);
RecyclerView recyclerView = Objects.requireNonNull(getView()).findViewById(R.id.recyclerview_layout);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
/*
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new FragmentHome()).commit();
}**/
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
Intent intent;
switch (id){
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Fragment_HomeSpot()).commit();
break;
case R.id.nav_search:
break;
case R.id.nav_map:
intent = new Intent(MainActivity.this,Map.class);
startActivity(intent);
break;
case R.id.nav_account:
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}
}
我的适配器
public class SpotAdapter extends FirestoreRecyclerAdapter <Spot, SpotAdapter.SpotHolder> {
SpotAdapter(@NonNull FirestoreRecyclerOptions<Spot> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull SpotHolder spotHolder, int i, @NonNull Spot spot) {
spotHolder.textViewNamaSpot.setText(spot.getNama());
spotHolder.textViewAlamatSpot.setText(spot.getAlamat());
spotHolder.imageViewSpotIMG.setImageResource(spot.getThumbnail());
}
@NonNull
@Override
public SpotHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_spot_item, parent, false);
return new SpotHolder(v);
}
class SpotHolder extends RecyclerView.ViewHolder {
TextView textViewNamaSpot;
TextView textViewAlamatSpot;
ImageView imageViewSpotIMG;
SpotHolder(@NonNull View itemView) {
super(itemView);
textViewNamaSpot= itemView.findViewById(R.id.nama_spot);
textViewAlamatSpot = itemView.findViewById(R.id.alamat_spot);
imageViewSpotIMG= itemView.findViewById(R.id.spot_thumbnail);
}
}
}
我的现货课程
public class Spot {
private String Nama;
private String Alamat;
private int Thumbnail;
public Spot() {
//Empty Constructor
}
public Spot(String nama, String alamat, int thumbnail) {
this.Nama = nama;
this.Alamat = alamat;
this.Thumbnail = thumbnail;
}
String getNama() {
return Nama;
}
String getAlamat() {
return Alamat;
}
int getThumbnail() {
return Thumbnail;
}
}