将Firebase中的值添加到RecycleView适配器中不会显示在应用程序中

时间:2018-12-07 12:12:24

标签: java android firebase firebase-realtime-database android-recyclerview

我正在尝试从我的Firebase数据库返回键和值,并将其插入到RecycleView适配器中,但是值不会显示在应用程序中。

无论如何我都无法解决。

我试图在Toast中显示值,并且可以正常工作,但是为什么不将这些值插入RecycleView适配器中呢?

这是代码。

public class test extends AppCompatActivity {
    TextView textView;
    Button button;
    String phone;

    String n;
    String a;

    FirebaseAuth mAuth;

    MyRecyclerViewAdapter adapter;

    ArrayList<String> info;

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


        Bundle b = new Bundle();
        b = getIntent().getExtras();
        phone = b.getString("number");

        textView = findViewById(R.id.textV);
        button = findViewById(R.id.signoutButton);

        mAuth = FirebaseAuth.getInstance();

        textView.setText(textView.getText().toString() + " as " + phone);


        info = new ArrayList<>();


        FirebaseDatabase myRef = FirebaseDatabase.getInstance();
        DatabaseReference sRef = myRef.getReference("users");

        sRef.addValueEventListener(new ValueEventListener() {


            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnap : dataSnapshot.getChildren()) {

                    if (childSnap.getKey().equals(phone)) {

                        for (DataSnapshot c : childSnap.getChildren()) {

                            n = c.getKey();
                            a = c.getValue().toString();

                            info.add(n);
                            info.add(a);


                        }


                    }


                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        RecyclerView recyclerView = findViewById(R.id.rvInfo);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRecyclerViewAdapter(this, info);
        adapter.setClickListener(new MyRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(test.this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);


    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取

n=get<0>(tuplefunct(abc);
score=get<1>(tuplefunct(abc));

从Firebase填充sRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { for (DataSnapshot childSnap : dataSnapshot.getChildren()) { if (childSnap.getKey().equals(phone)) { for (DataSnapshot c : childSnap.getChildren()) { n = c.getKey(); a = c.getValue().toString(); info.add(n); info.add(a); } } } notifyDataSetChanged(); } @Override public void onCancelled(@NonNull DatabaseError databaseError) { } }); 后,您必须添加notifyDataSetChanged();。因为从Firebase加载数据需要花费时间。然后在从Firebase填充ArrayList之前初始化适配器。