使用Firebase数据填充Android微调器

时间:2018-03-29 12:53:56

标签: java android firebase firebase-realtime-database

我在propertyAddress表格中填充propertiesMaintenanceActivity中的微调器时出现了一些问题。

问题本身不在代码中,而是使用可以使用Firebase控制台修改的数据库规则。

目前,我的规则是这样的:

{
  "rules": {
    "maintenance" : {
      "$uid" : {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

在记录维护查询时允许基于用户的身份验证。

但是,当规则如下时,微调器将仅使用propertyAddress填充:

{
  "rules": {
    ".read":"true",
    ".write": "true"
  }
}

我想知道我需要对规则做些什么才能允许我的微调器spinnerProperty访问我的数据库的properties部分。

以下是MaintenanceActivity的代码摘录:

MaintenanceActivity

fDatabaseRoot.child("properties").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        dataSnapshot.getChildren() to
        // initialize the array
        final List<String> propertyAddressList = new ArrayList<String>();

        for (DataSnapshot addressSnapshot: dataSnapshot.getChildren()) {
            String propertyAddress = addressSnapshot.child("propertyAddress").getValue(String.class);
            if (propertyAddress!=null){
                propertyAddressList.add(propertyAddress);
            }
        }

        Spinner spinnerProperty = (Spinner) findViewById(R.id.spinnerProperty);
        ArrayAdapter<String> addressAdapter = new ArrayAdapter<String>(MaintenanceActivity.this, android.R.layout.simple_spinner_item, propertyAddressList);
        addressAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProperty.setAdapter(addressAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

以下是我的属性表的图像:

properties table

对此的任何帮助都将非常感激。

同样,这不是代码本身的问题,只是数据库的规则。

1 个答案:

答案 0 :(得分:0)

我已更新Firebase数据库的规则,以包含 public void getWeeksOfMonth( int year) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy"); Calendar cal = Calendar.getInstance(); int currentmonth = cal.get(Calendar.MONTH); int val = 0; cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, currentmonth); cal.set(DAY_OF_MONTH, 1); int ndays = cal.getActualMaximum(DAY_OF_MONTH); System.out.println(ndays + "<<<ff"); while (cal.get(DAY_OF_WEEK) != FRIDAY) { cal.add(DAY_OF_MONTH, 1); ndays--; } int remainingDays = ndays % 7; if (remainingDays == 0) ndays += 7; else ndays = ndays + 7 - remainingDays; int inc = 1; for (int i = 1; i <= ndays; i++) { String day = sdf.format(cal.getTime()); System.out.println(day + "<<<"); Log.e("quest", day + "<<<"); inc++; if (val == 0) { firstweek = day.substring(0, 6); // weeklist.add(firstweek); val = 1; } if (i % 7 == 0) { String s = day.substring(0, 6); weeklist.add(firstweek + " to " + s); val = 0; Log.e("weekdayss", "=======week days===========" + weeklist); inc = 0; } if (inc >= 1 && i == ndays) { for (int ii = inc; ii <= 6; ii++) { String dayi = sdf.format(cal.getTime()); System.out.println(dayi + "<<<"); Log.e("quest1", dayi + "<<<"); inc++; } } cal.add(Calendar.DATE, 1); } if (weeklist.size() == 5) { weeklist.remove(4); } if (weeklist.size() == 6) { weeklist.remove(5); weeklist.remove(4); } } 节点的读/写功能。

[
02-Mar   to   08-Mar
09-Mar   to   15-Mar
16-Mar   to   22-Mar
23-Mar   to   29-Mar
]