Firebase未填充列表

时间:2019-06-07 02:41:19

标签: java android firebase firebase-realtime-database

我正在按照Angga Risky教程构建一个Android购物清单应用。

代码正在运行,没有错误,但是我所创建的列表并未由与其连接的firebase实时数据库填充。以下是主要活动

public class MainActivity extends AppCompatActivity {

    TextView titlepage, subtitlepage, endpage;

    DatabaseReference reference;
    RecyclerView ourlist;
    ArrayList < MyList > list;
    ListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        titlepage = findViewById(R.id.titlepage);
        subtitlepage = findViewById(R.id.subtitlepage);
        endpage = findViewById(R.id.endpage);

        //working with data
        ourlist = findViewById(R.id.ourlist);
        ourlist.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList < MyList > ();

        //get data from firebase
        reference = FirebaseDatabase.getInstance().getReference().child("compshopper3");
        reference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //set code to retrieve data and replace layout
                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
                    MyList p = dataSnapshot1.getValue(MyList.class);
                    list.add(p);
                }
                listAdapter = new ListAdapter(MainActivity.this, list);
                ourlist.setAdapter(listAdapter);
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                // set code to show an error
                Toast.makeText(getApplicationContext(), "Dude Where's My Data???", Toast.LENGTH_SHORT).show();

            }

        });
    }
}

这里是查看火力地堡数据

enter image description here

这是仿真器输出的快照。

enter image description here

2 个答案:

答案 0 :(得分:1)

除了@ peter-haddad的答案外,请确保您的Firebase数据库规则允许您无需身份验证即可访问数据(以防万一,您通常会在开发人员测试期间这样做)。

Firebase控制台->数据库(实时数据库)->规则->

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

答案 1 :(得分:1)

首先将引用更改为引用父节点:

reference = FirebaseDatabase.getInstance().getReference().child("CompShopperApp");

然后删除onDataChange()内的for循环,并确保您的模型类包含字段price1price2price3itemtitle

    reference.addValueEventListener( new ValueEventListener () {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            MyList p    = dataSnapshot.getValue(MyList.class);
            list.add(p);
            listAdapter = new ListAdapter(MainActivity.this, list);
            ourlist.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            // set code to show an error
            Toast.makeText(getApplicationContext(), "Dude Where's My Data???", Toast.LENGTH_SHORT).show();

        }