我无法使用我的回收站视图来处理我在firebase数据库中的房屋。我想要做的是在开始活动中显示所有房屋,但现在没有运气。这是我尝试过的: 很抱歉有很多行代码,但我想展示完整的代码以便更好地解释它。
在onStart()上,我尝试填充reyclerview:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
private DatabaseReference mDatabaseHouses;
private String uid;
private String email;
private String username;
private static final int RC_SIGN_IN = 123;
private RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabaseHouses = FirebaseDatabase.getInstance().getReference().child("HOUSES");
mRecyclerView = (RecyclerView) findViewById(R.id.main_recycler_houses);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
signUser();
}
FirebaseRecyclerAdapter<Houses, HousesViewHolder> mHousesRecyclerAdapter = new FirebaseRecyclerAdapter<Houses, HousesViewHolder>(
Houses.class,
R.layout.house_single,
HousesViewHolder.class,
mDatabaseHouses
) {
@Override
protected void populateViewHolder(HousesViewHolder viewHolder, Houses house, int position) {
viewHolder.setHouseName(house.getHouse_name());
}
};
mRecyclerView.setAdapter(mHousesRecyclerAdapter);
}
private static class HousesViewHolder extends RecyclerView.ViewHolder {
View mView;
public HousesViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
void setHouseName(String house_name) {
TextView house_name_tv = (TextView) mView.findViewById(R.id.single_house_name_tv);
house_name_tv.setText(house_name);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);
View parentLayout = findViewById(android.R.id.content);
// Successfully signed in
if (resultCode == RESULT_OK) {
final FirebaseUser signedUser = mAuth.getCurrentUser();
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.child(getString(R.string.users)).child(signedUser.getUid()).exists()) {
Log.d("SignInProcess", "onDataChange: **Register");
uid = setNullToDefaultValue(signedUser.getUid());
email = setNullToDefaultValue(signedUser.getEmail());
User mUser = new User();
mUser.setUid(uid);
mUser.setEmail(email);
Map<String, Object> userMap = mUser.userToMap();
userMap.put("date", Calendar.getInstance().getTime().toString());
mDatabase.child(getString(R.string.users)).child(mAuth.getCurrentUser().getUid()).updateChildren(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
startActivity(new Intent(MainActivity.this, ChooseUsernameActivity.class));
finish();
}
});
} else {
Log.d("SignInProcess", "onDataChange: **Log In");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDatabase.addValueEventListener(valueEventListener);
Snackbar.make(parentLayout, "You Signed In!", Snackbar.LENGTH_LONG);
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
return;
} else {
// Sign in failed
if (response == null) {
// User pressed back button
Snackbar.make(parentLayout, "User presses back", Snackbar.LENGTH_LONG);
return;
}
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
Snackbar.make(parentLayout, "No Network", Snackbar.LENGTH_LONG);
return;
}
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
Snackbar.make(parentLayout, "Unknown Error", Snackbar.LENGTH_LONG);
return;
}
}
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_log_out) {
AuthUI.getInstance()
.signOut(MainActivity.this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null) {
signUser();
}
}
});
return true;
}
return super.onOptionsItemSelected(item);
}
private void signUser() {
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setTheme(R.style.DarkTheme)
// .setLogo(R.mipmap.ic_launcher)
.setIsSmartLockEnabled(true)
.setAvailableProviders(
Arrays.asList(
new AuthUI.IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build()
))
.build(),
RC_SIGN_IN);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
return true;
} else if (id == R.id.nav_profile) {
startActivity(new Intent(MainActivity.this, ProfileActivity.class));
return true;
} else if (id == R.id.nav_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
//Database Housekeeping
public String setNullToDefaultValue(String s) {
String defaultValue = "default_value";
if (s == null)
return defaultValue;
return s;
}
}
这是我使用getHouse_name()方法的模型类:
/**
* Created by nikos on 22/7/2017.
*/
public class Houses {
private String availability;
private String city;
private String country;
private String description;
private Object fotos;
private String hid;
private String house_name;
private String langitude;
private String longitude;
private String mainFoto;
private String max_people;
private String price;
private String rating;
private String postalCode;
//Services
private String aircondition;
private String balcony;
private String breakfast;
private String cafe_bar_restaurant;
private String child_keeping;
private String clothes_laundry;
private String conference_rooms;
private String dinner;
private String doctor_support;
private String elevator;
private String hair_dryer;
private String in_room_safebox;
private String iron_ironing_board;
private String minibar;
private String newspaper_delivery;
private String parking;
private String private_bath;
private String reception_24;
private String room_service;
private String soundproof_walls;
private String wifi;
private String uid;
private Object user_reviews;
public Houses() {
}
public Houses(String availability, String city, String country, String description, Object fotos, String hid, String house_name, String langitude, String longitude, String mainFoto, String max_people, String price, String rating, String postalCode, String aircondition, String balcony, String breakfast, String cafe_bar_restaurant, String child_keeping, String clothes_laundry, String conference_rooms, String dinner, String doctor_support, String elevator, String hair_dryer, String in_room_safebox, String iron_ironing_board, String minibar, String newspaper_delivery, String parking, String private_bath, String reception_24, String room_service, String soundproof_walls, String wifi, String uid, Object user_reviews) {
this.availability = availability;
this.city = city;
this.country = country;
this.description = description;
this.fotos = fotos;
this.hid = hid;
this.house_name = house_name;
this.langitude = langitude;
this.longitude = longitude;
this.mainFoto = mainFoto;
this.max_people = max_people;
this.price = price;
this.rating = rating;
this.aircondition = aircondition;
this.balcony = balcony;
this.breakfast = breakfast;
this.cafe_bar_restaurant = cafe_bar_restaurant;
this.child_keeping = child_keeping;
this.clothes_laundry = clothes_laundry;
this.conference_rooms = conference_rooms;
this.dinner = dinner;
this.doctor_support = doctor_support;
this.elevator = elevator;
this.hair_dryer = hair_dryer;
this.in_room_safebox = in_room_safebox;
this.iron_ironing_board = iron_ironing_board;
this.minibar = minibar;
this.newspaper_delivery = newspaper_delivery;
this.parking = parking;
this.private_bath = private_bath;
this.reception_24 = reception_24;
this.room_service = room_service;
this.soundproof_walls = soundproof_walls;
this.wifi = wifi;
this.uid = uid;
this.user_reviews = user_reviews;
this.postalCode = postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getPostalCode() {
return postalCode;
}
public void setAvailability(String availability) {
this.availability = availability;
}
public void setCity(String city) {
this.city = city;
}
public void setCountry(String country) {
this.country = country;
}
public void setDescription(String description) {
this.description = description;
}
public void setFotos(Object fotos) {
this.fotos = fotos;
}
public void setHid(String hid) {
this.hid = hid;
}
public void setHouse_name(String house_name) {
this.house_name = house_name;
}
public void setLangitude(String langitude) {
this.langitude = langitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public void setMainFoto(String mainFoto) {
this.mainFoto = mainFoto;
}
public void setMax_people(String max_people) {
this.max_people = max_people;
}
public void setPrice(String price) {
this.price = price;
}
public void setRating(String rating) {
this.rating = rating;
}
public void setAircondition(String aircondition) {
this.aircondition = aircondition;
}
public void setBalcony(String balcony) {
this.balcony = balcony;
}
public void setBreakfast(String breakfast) {
this.breakfast = breakfast;
}
public void setCafe_bar_restaurant(String cafe_bar_restaurant) {
this.cafe_bar_restaurant = cafe_bar_restaurant;
}
public void setChild_keeping(String child_keeping) {
this.child_keeping = child_keeping;
}
public void setClothes_laundry(String clothes_laundry) {
this.clothes_laundry = clothes_laundry;
}
public void setConference_rooms(String conference_rooms) {
this.conference_rooms = conference_rooms;
}
public void setDinner(String dinner) {
this.dinner = dinner;
}
public void setDoctor_support(String doctor_support) {
this.doctor_support = doctor_support;
}
public void setElevator(String elevator) {
this.elevator = elevator;
}
public void setHair_dryer(String hair_dryer) {
this.hair_dryer = hair_dryer;
}
public void setIn_room_safebox(String in_room_safebox) {
this.in_room_safebox = in_room_safebox;
}
public void setIron_ironing_board(String iron_ironing_board) {
this.iron_ironing_board = iron_ironing_board;
}
public void setMinibar(String minibar) {
this.minibar = minibar;
}
public void setNewspaper_delivery(String newspaper_delivery) {
this.newspaper_delivery = newspaper_delivery;
}
public void setParking(String parking) {
this.parking = parking;
}
public void setPrivate_bath(String private_bath) {
this.private_bath = private_bath;
}
public void setReception_24(String reception_24) {
this.reception_24 = reception_24;
}
public void setRoom_service(String room_service) {
this.room_service = room_service;
}
public void setSoundproof_walls(String soundproof_walls) {
this.soundproof_walls = soundproof_walls;
}
public void setWifi(String wifi) {
this.wifi = wifi;
}
public void setUid(String uid) {
this.uid = uid;
}
public void setUser_reviews(Object user_reviews) {
this.user_reviews = user_reviews;
}
public String getAvailability() {
return availability;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public String getDescription() {
return description;
}
public Object getFotos() {
return fotos;
}
public String getHid() {
return hid;
}
public String getHouse_name() {
return house_name;
}
public String getLangitude() {
return langitude;
}
public String getLongitude() {
return longitude;
}
public String getMainFoto() {
return mainFoto;
}
public String getMax_people() {
return max_people;
}
public String getPrice() {
return price;
}
public String getRating() {
return rating;
}
public String getAircondition() {
return aircondition;
}
public String getBalcony() {
return balcony;
}
public String getBreakfast() {
return breakfast;
}
public String getCafe_bar_restaurant() {
return cafe_bar_restaurant;
}
public String getChild_keeping() {
return child_keeping;
}
public String getClothes_laundry() {
return clothes_laundry;
}
public String getConference_rooms() {
return conference_rooms;
}
public String getDinner() {
return dinner;
}
public String getDoctor_support() {
return doctor_support;
}
public String getElevator() {
return elevator;
}
public String getHair_dryer() {
return hair_dryer;
}
public String getIn_room_safebox() {
return in_room_safebox;
}
public String getIron_ironing_board() {
return iron_ironing_board;
}
public String getMinibar() {
return minibar;
}
public String getNewspaper_delivery() {
return newspaper_delivery;
}
public String getParking() {
return parking;
}
public String getPrivate_bath() {
return private_bath;
}
public String getReception_24() {
return reception_24;
}
public String getRoom_service() {
return room_service;
}
public String getSoundproof_walls() {
return soundproof_walls;
}
public String getWifi() {
return wifi;
}
public String getUid() {
return uid;
}
public Object getUser_reviews() {
return user_reviews;
}
@Override
public String toString() {
return "Houses{" +
"availability='" + availability + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
", description='" + description + '\'' +
", fotos=" + fotos +
", hid='" + hid + '\'' +
", house_name='" + house_name + '\'' +
", langitude='" + langitude + '\'' +
", longitude='" + longitude + '\'' +
", mainFoto='" + mainFoto + '\'' +
", max_people='" + max_people + '\'' +
", price='" + price + '\'' +
", rating='" + rating + '\'' +
", postalCode='" + postalCode + '\'' +
", aircondition='" + aircondition + '\'' +
", balcony='" + balcony + '\'' +
", breakfast='" + breakfast + '\'' +
", cafe_bar_restaurant='" + cafe_bar_restaurant + '\'' +
", child_keeping='" + child_keeping + '\'' +
", clothes_laundry='" + clothes_laundry + '\'' +
", conference_rooms='" + conference_rooms + '\'' +
", dinner='" + dinner + '\'' +
", doctor_support='" + doctor_support + '\'' +
", elevator='" + elevator + '\'' +
", hair_dryer='" + hair_dryer + '\'' +
", in_room_safebox='" + in_room_safebox + '\'' +
", iron_ironing_board='" + iron_ironing_board + '\'' +
", minibar='" + minibar + '\'' +
", newspaper_delivery='" + newspaper_delivery + '\'' +
", parking='" + parking + '\'' +
", private_bath='" + private_bath + '\'' +
", reception_24='" + reception_24 + '\'' +
", room_service='" + room_service + '\'' +
", soundproof_walls='" + soundproof_walls + '\'' +
", wifi='" + wifi + '\'' +
", uid='" + uid + '\'' +
", user_reviews=" + user_reviews +
'}';
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> houseObject = new HashMap<>();
houseObject.put("city", city);
houseObject.put("country", country);
houseObject.put("description", description);
houseObject.put("fotos", fotos);
houseObject.put("hid", hid);
houseObject.put("house_name", house_name);
houseObject.put("langitude", langitude);
houseObject.put("longitude", longitude);
houseObject.put("mainFoto", mainFoto);
houseObject.put("max_people", max_people);
houseObject.put("price", price);
houseObject.put("rating", rating);
houseObject.put("postalCode", postalCode);
houseObject.put("aircondition", aircondition);
houseObject.put("balcony", balcony);
houseObject.put("breakfast", breakfast);
houseObject.put("cafe_bar_restaurant", cafe_bar_restaurant);
houseObject.put("child_keeping", child_keeping);
houseObject.put("clothes_laundry", clothes_laundry);
houseObject.put("conference_rooms", conference_rooms);
houseObject.put("dinner", dinner);
houseObject.put("doctor_support", doctor_support);
houseObject.put("elevator", elevator);
houseObject.put("hair_dryer", hair_dryer);
houseObject.put("in_room_safebox", in_room_safebox);
houseObject.put("iron_ironing_board", iron_ironing_board);
houseObject.put("minibar", minibar);
houseObject.put("newspaper_delivery", newspaper_delivery);
houseObject.put("parking", parking);
houseObject.put("private_bath", private_bath);
houseObject.put("reception_24", reception_24);
houseObject.put("room_service", room_service);
houseObject.put("soundproof_walls", soundproof_walls);
houseObject.put("wifi", wifi);
houseObject.put("uid", uid);
houseObject.put("user_reviews", user_reviews);
return houseObject;
}
}
答案 0 :(得分:0)
尝试使用类似此代码的内容
DataGridRow