我正在使用Firebase实时数据库构建一个小型应用程序。
该应用列出了每个给定日期的班次。
每个班次都显示为一个组项目,并且注册到该班次的每个员工都显示为该组中的孩子。
我在组项目中使用了ParentHeader类的ArrayList,在子项目中使用了字符串的ArrayList(以列出员工的姓名)-将来对于更复杂的对象可能会更改。
这是我的代码:
public class DateActivity extends ExpandableListActivity {
// date and isAdmin are passed to this activity using shared preferences
private String date;
private boolean isAdmin;
// Adapter declaration
private CustomExpandableListAdapter mAdapter;
// ArrayList to store group items
private ArrayList<ParentHeader> parents = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
// initialize date and isAdmin here
// Fetch all shifts for selected date from Firebase
Validator.fetchShiftsPerDayFromDB(date, new Callback()
{
@Override
void shiftsCallback(ArrayList<Shift> shifts)
{
super.shiftsCallback(shifts);
if (shifts == null)
return;
for(Shift shift : shifts){
parents.add(new ParentHeader(shift));
}
mAdapter = new CustomExpandableListAdapter();
setListAdapter(mAdapter);
}
});
}
private class CustomExpandableListAdapter extends BaseExpandableListAdapter
{
private LayoutInflater inflater;
public CustomExpandableListAdapter()
{
// Create Layout Inflator
inflater = LayoutInflater.from(DateActivity.this);
}
@Override
public int getGroupCount()
{
return parents.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
int size = 0;
if(parents.get(groupPosition).getEmployeesList() != null)
size = parents.get(groupPosition).getEmployeesList().size();
return size;
}
@Override
public Object getGroup(int groupPosition)
{
return parents.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return parents.get(groupPosition).getEmployeesList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parentView)
{
final ParentHeader parentHeader = parents.get(groupPosition);
// Inflate group_row.xml file for group rows
convertView = inflater.inflate(R.layout.group_row, parentView, false);
// Get group_row.xml file elements and set values here
//Add User to selected shift
convertView.findViewById(R.id.add).setOnClickListener(v -> {
//TODO insert constraints
Validator.addUserToShiftInDB(parentHeader.getShift().getKey(), date, userName, new Callback() {
@Override
void onUserAssignedToShiftCallback() {
super.onUserAssignedToShiftCallback();
// TODO attach as child-item to chosen group-item
}
});
});
convertView.findViewById(R.id.delete).setOnClickListener(v -> {
if(isAdmin){
//TODO open confirmation window
Validator.deleteShiftFromDateInDB(parentHeader.getShift().getKey(), date, new Callback() {
@Override
void onDeletedShiftCallback() {
super.onDeletedShiftCallback();
// shift gets deleted in Firebase but the expandable list does not refresh
parents.remove(groupPosition);
mAdapter.notifyDataSetChanged();
}
});
}
else{
Toast.makeText(getApplicationContext(),"Admin privileges required", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parentView)
{
final ParentHeader parentHeader = parents.get(groupPosition);
final String childString = parentHeader.getEmployeesList().get(childPosition);
// Inflate child_row.xml file for child rows
convertView = inflater.inflate(R.layout.child_row, parentView, false);
// Get child_row.xml file elements and set values here
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
@Override
public boolean isEmpty()
{
return ((parents == null) || parents.isEmpty());
}
@Override
public void notifyDataSetChanged()
{
// Refresh List rows
super.notifyDataSetChanged();
}
}
}
这是ParentHeader类,用于存储每个组项目上的数据。
public class ParentHeader {
private Shift shift;
private boolean isDeleteButtonClicked, isAddButtonClicked, isHeaderClicked;
// Array to store child objects
private ArrayList<String> employeesList = new ArrayList<>();
public ParentHeader() { }
public ParentHeader(Shift shift) {
this.shift = shift;
}
// Getters/Setters here
}
最后是Shift类,该类存储每个班次的数据。
public class Shift {
private String startTime, endTime, name, key;
private Integer wage, numOfEmps, employees;
public Shift(){}
public Shift(String name, String startTime, String endTime, Integer wage, Integer numOfEmps, Integer employees) {
this.startTime = startTime;
this.endTime = endTime;
this.name = name;
this.wage = wage;
this.numOfEmps = numOfEmps;
this.employees = employees;
}
//Getters/Setters here
}
第一个问题
从父项上的ExpandableListAdapter中移除组项目。
正如您在上面看到的,我先叫parents.remove
,然后叫notifyDataSetChanged
,但是列表没有刷新。
但是,如果我重新加载活动,它确实可以工作。
我希望它能动态工作。
第二个问题
在单击父项的按钮上将孩子添加到组中。
我的数据库的结构如下:
管理员将新班次添加到日期后。
现在,我希望每个用户都能够将自己分配给一个班次,并希望其名称显示为该组项目班次的子项目。
点击所选的组项目转移后,他们的数据应存储在Firebase中,如下所示:
我希望存储在employees
下的每个名称在其对应的组项目移动下均显示为子项目。当然是动态的。
我非常感谢您为解决这两个问题提供帮助。我是Android开发的新手,没有太多经验。
谢谢。