如何将用户的 ArrayList 存储在 Android 的 Firebase 数据库中?
Posted
技术标签:
【中文标题】如何将用户的 ArrayList 存储在 Android 的 Firebase 数据库中?【英文标题】:How do i store an ArrayList of users in a Firebase Database in Android? 【发布时间】:2018-09-13 19:01:06 【问题描述】:我正在尝试制作一个拥有大量用户的应用程序。我想到的是大厅的模型,它的属性之一是用户的 ArrayList。但从我目前阅读的内容来看,说起来容易做起来难。这就是我目前所拥有的......
Lobby.class(大厅模型)
@Parcel
public class Lobby
private String owner;
private String location;
private double cost;
private String startDate;
private ArrayList<FirebaseUser> members;
public Lobby()
public Lobby(String owner, String location, double cost, String startDate, ArrayList<FirebaseUser> members)
this.owner = owner;
this.location = location;
this.cost = cost;
this.startDate = startDate;
this.members = members;
public String getOwner()
return owner;
public void setOwner(String owner)
this.owner = owner;
public String getLocation()
return location;
public void setLocation(String location)
this.location = location;
public double getCost()
return cost;
public void setCost(double cost)
this.cost = cost;
public String getStartDate()
return startDate;
public void setStartDate(String startDate)
this.startDate = startDate;
public ArrayList<FirebaseUser> getMembers()
return members;
public void setMembers(ArrayList<FirebaseUser> members)
this.members = members;
public String getDateText()
String dateString = DateFormat.getDateInstance().format(new Date());
return dateString;
public int getTotalMembers()
return members.size();
这是我将大厅保存到火力基地的地方
public class createLobby extends AppCompatActivity
private static String TAG = "CreateLobby";
private TextView displayDate;
private DatePickerDialog.OnDateSetListener dateListener;
private EditText locIn, costIn;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private DatabaseReference usernameGet;
private Button submitLobbyButton;
private String ownerName;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_lobby);
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference();
usernameGet = databaseReference.child(user.getUid()).child("userName");
usernameGet.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot dataSnapshot)
ownerName = dataSnapshot.getValue(String.class);
Log.v("Username", ownerName);
@Override
public void onCancelled(DatabaseError databaseError)
);
locIn = (EditText)findViewById(R.id.loc_input);
costIn = (EditText)findViewById(R.id.cost_input);
displayDate = (TextView)findViewById(R.id.date_picker);
displayDate.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
createLobby.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateListener, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
);
dateListener = new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
month = month+1;
String date = month + "/" + dayOfMonth + "/" + year;
displayDate.setText(date);
;
submitLobbyButton = (Button)findViewById(R.id.create_lobby_button);
submitLobbyButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
submitLobby();
);
public void submitLobby()
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
DatabaseReference lobbyRef = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
String owenerMan = ownerName;
String gameLoc = locIn.getText().toString();
Double gameCost = Double.parseDouble(costIn.getText().toString());
String gameDate = displayDate.getText().toString();
ArrayList<FirebaseUser> lobbyMembers = new ArrayList<>();
Lobby newLobby = new Lobby(owenerMan, gameLoc, gameCost, gameDate, lobbyMembers);
lobbyMembers.add(user);
lobbyRef.push().setValue(newLobby);
Log.v("creator", owenerMan);
Log.v("newLobby", newLobby.getStartDate());
Toast.makeText(getApplicationContext(), "Lobby Added!", Toast.LENGTH_SHORT).show();
当我运行我的代码时,会出现一个错误,指出我的 Lobby.class 文件中的 getTotalMembers() 函数正在返回一个空指针异常。数组不存储
【问题讨论】:
为什么你目前没有工作的代码?你有任何错误吗?还是您遇到了意想不到的行为? 你能详细说明你的问题吗? 或者在你运行你的代码之后有什么更新,更详细的? 请添加您的数据库结构并说明您想要实现的目标。 @DeepPatel 已编辑 【参考方案1】:尝试一次替换此函数,然后告诉我会发生什么:
public void submitLobby()
firebaseAuth = FirebaseAuth.getInstance();
user = firebaseAuth.getCurrentUser();
DatabaseReference lobbyRef = FirebaseDatabase.getInstance().getReference(Constants.LOBBIES);
String owenerMan = ownerName;
String gameLoc = locIn.getText().toString();
Double gameCost = Double.parseDouble(costIn.getText().toString());
String gameDate = displayDate.getText().toString();
ArrayList<FirebaseUser> lobbyMembers = new ArrayList<>();
lobbyMembers.add(user); // You were adding user into Array after calling constructor
Lobby newLobby = new Lobby(owenerMan, gameLoc, gameCost, gameDate, lobbyMembers);
lobbyRef.push().setValue(newLobby);
Log.v("creator", owenerMan);
Log.v("newLobby", newLobby.getStartDate());
Toast.makeText(getApplicationContext(), "Lobby Added!", Toast.LENGTH_SHORT).show();
【讨论】:
用什么替换函数? 在您的代码中使用相同的功能,我更改了位置: lobbyMembers.add(user); // 您在调用构造函数后将用户添加到数组中 谢谢伙计!编码愉快! 您好!我知道这听起来很奇怪,但是如果我要求您支持原始问题,可以吗? :D 恐怕我问了太多问题,谁的分数从来没有超过 0,我很遗憾地用完了。 不用担心!有时要求支持是可以的,但不要每次都这样做。否则您将在版主审核中失去积分。 :P以上是关于如何将用户的 ArrayList 存储在 Android 的 Firebase 数据库中?的主要内容,如果未能解决你的问题,请参考以下文章
如果使用 Asynctask 将 URL 存储在 ArrayList 中,如何从服务器下载图像?
如何将 HashMap<String, ArrayList<String>> 存储在列表中?
如何将 Url 从 Firebase Storage 存储到 ArrayList 中?