从Firebase数据库检索数据到可扩展列表视图

时间:2019-11-15 20:10:35

标签: android firebase-realtime-database expandablelistview

我正在构建一个小型应用程序,用于管理用户并在Android Studio中进行转换,并且我将Firebase数据库用作后端。

用户和班次具有M:M关系,因为可以将许多用户分配给同一班次,而一个班次可以有许多用户。

这是我的数据库的结构:


 String FROM = "sender@example.com";
 String FROMNAME = "Sender Name";
 String TO = "recipient@amazon.com";
 String SMTP_USERNAME = "smtp_username";
 String SMTP_PASSWORD = "smtp_password";
 String CONFIGSET = "ConfigSet";
 String HOST = "email-smtp.us-west-2.amazonaws.com";

 int PORT = 587;

 String SUBJECT =
 "Amazon SES test (SMTP interface accessed using C#)";
 String BODY =
 "<h1>Amazon SES Test</h1>" +
 "<p>This email was sent through the " +
 "<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
 "using the .NET System.Net.Mail library.</p>";
 MailMessage message = new MailMessage();
 message.IsBodyHtml = true;
 message.From = new MailAddress(FROM, FROMNAME);
 message.To.Add(new MailAddress(TO));
 message.Subject = SUBJECT;
 message.Body = BODY;
 message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
 using (var client = new System.Net.Mail.SmtpClient(HOST, PORT))
 {
 client.Credentials =
 new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
 client.EnableSsl = true;
 try
 {
 Console.WriteLine("Attempting to send email...");
 client.Send(message);
 Console.WriteLine("Email sent!");
 }
 catch (Exception ex)
 {
 Console.WriteLine("The email was not sent.");
 Console.WriteLine("Error message: " + ex.Message);
 }
 }
 }
 }
}

构建我的应用程序后,当用户单击日历中的日期时,会打开{ "linking" : { "shift-assign" : { "key1" : { "111" : true, "222" : true }, "key2" : { "111" : true } }, "user-assign" : { "111" : { "key1" : true, "key2" : true }, "222" : { "key1" : true } } }, "shifts" : { "20191116" : { "key1" : { "endTime" : "00:00", "name" : "Morning", "numOfEmps" : 3, "startTime" : "00:00", "wage" : 0 }, "key2" : { "endTime" : "00:00", "name" : "Night", "numOfEmps" : 1, "startTime" : "00:00", "wage" : 0 } } }, "users" : { "111" : { "id" : "111", "password" : "111", ... }, "222" : { "id" : "222", "password" : "222", ... }, "99999" : { "id" : "99999", "password" : "123", ... } } } ,其中列出了与该日期相关的所有班次。

我正在使用一个可扩展列表将数据呈现给用户-标头代表一个班次(在所述日期下),其子级代表当前分配给该班次的所有用户。

但是我很难从Firebase提取数据。

这是我的DateActivity

DateActivity

显示了可扩展列表,但数据全部弄乱了。而不是public class DateActivity extends AppCompatActivity { private String[] dates; // the chosen date and its prior dates private List<mShift> shiftList; // parent items of expandable list private List<mUser> userList; // child items of expandable list private HashMap<mShift, List<mUser>> collection = new HashMap<>(); // mapping parent-children private ExpandableListView expListView; private mUser user; // store the logged-in user ,to be replaced with user's id @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_date); // get user object from shared preferences SharedPreferences sp = getSharedPreferences("sp", 0); String json = sp.getString("user", ""); user = (new Gson()).fromJson(json, mUser.class); // get dates array // chosen date in index 0 // yesterday's date in index 1 // and so on dates = sp.getString("days", "").split(","); //bind elements here... expListView = (ExpandableListView) findViewById(R.id.expListView); // Fetch all shifts for selected date fetchShifts(); } private void fetchShifts() { // root reference DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(); // reference to shifts/dates[0] mDatabase.child("shifts") .child(dates[0]) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { // create a new shift list shiftList = new ArrayList<>(); // loop through all shift objects for (DataSnapshot ds : dataSnapshot.getChildren()) { mShift shift = ds.getValue(mShift.class); shift.setKey(ds.getKey()); shiftList.add(shift); // add shift to list // reference to linking/shift-assign/shift-key mDatabase.child("linking") .child("shift-assign") .child(shift.getKey()) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { // create a new user list userList = new ArrayList<>(); // loop through all user objects for(DataSnapshot ds1 : snapshot.getChildren()){ // reference to users/user-id mDatabase.child("users") .child(ds1.getKey()) .addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot2) { mUser u = snapshot2.getValue(mUser.class); userList.add(u); // add user to list } @Override public void onCancelled(@NonNull DatabaseError databaseError) {} }); } // map current shift and its users collection.put(shiftList.get(0), userList); } @Override public void onCancelled(@NonNull DatabaseError databaseError) {} }); } final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(DateActivity.this, shiftList, collection, dates, user); expListView.setAdapter(expListAdapter); } @Override public void onCancelled(@NonNull DatabaseError databaseError) {} }); } 下的两个用户,我仅获得第一个(ID为key1的用户,而111下的我应该显示一个用户,但没有一个用户。

0 个答案:

没有答案