如何从Firebase到Android应用程序检索子数据

时间:2019-03-14 13:50:46

标签: android firebase firebase-realtime-database android-listview

我目前是应用程序开发的新手,并且从下面的代码中,我希望能够从Firebase检索值数据。我在我的Android应用程序中设置了ListView,将能够单击Listview中的项目并打开另一个活动。我希望新活动显示Firebase数据库中的数据。

enter image description here

任何人都可以提出建议,非常感谢?

ublic class MainActivity extends AppCompatActivity {

private ArrayAdapter<String> arrayAdapter;

private ListView mListView;


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

    final String uid = FirebaseAuth.getInstance().getCurrentUser().myHikes();
    DatabaseReference mDbRef = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference uidRef = mDbRef.child("Hikes").child(uid);
    final List<String> list = new ArrayList<>();
    final ArrayList<String> id = new ArrayList<>();
    arrayAdapter = new ArrayAdapter<>(getApplication(), android.R.layout.simple_list_item_1, list);


    String key = uidRef.push().getKey();
    final hikeModClass report = new hikeModClass(String);
    report.setName("name");
    report.setDescription("description");
    report.setKey(key);
    uidRef.child(key).setValue(report);

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 : DataSnapshot.getChildren()){
                    id.add(dataSnapshot1.getKey());
                    String name = dataSnapshot.child("name").getValue(String.class);
                    String description = dataSnapshot.child("description").getValue(String.class);
                    list.add(name + "\n" + description + "\n");
                    Log.d("TAG", name);
                }

                ListView = findViewById(R.id.mListView);
                mListView.setAdapter(arrayAdapter);
        }

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

        }
    };
    uidRef.addListenerForSingleValueEvent(eventListener);
    final ListView progressList = findViewById(R.id.progressList);

    progressList.setOnClickListener(new AdapterView.OnItemClickListener());{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int x, long l){

            ValueEventListener eventListener1 = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    ArrayList<hikeModClass> list = new ArrayList<>();
                    for (DataSnapshot dataSnapshot1: DataSnapshot.getChildren()){
                        hikeModClass report = dataSnapshot.getValue(hikeModClass.class);
                        list.add(report);
                    }
                    Intent intent = new Intent(MainActivity.this, hikeModClass.class);
                    ListView listView = findViewById(R.id.progressList);
                    listView.setAdapter(arrayAdapter);
                    startActivity(intent);

                    listView.setOnClickListener(new AdapterView.OnItemClickListener(){

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                            hikeModClass arrayAdapterItem = arrayAdapter.getItem(i);
                            Intent intent = new Intent(MainActivity.this, hikeModClass.class);
                            intent.putExtra("object", hikeModClass);
                            startActivity(intent);
                        }

                    });
                }

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

                }
            };
            uidRef.addListenerForSingleValueEvent(eventListener);
        }
    }



    }
}

模型类

public class hikeModClass {

private String name;
private String description;
private String key;

public hikeModClass(String name, String description, String key) {
    this.name = name;
    this.description = description;
    this.key = key;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
}

1 个答案:

答案 0 :(得分:0)

以下代码可帮助您获取 myHike 键下的所有数据,并将其存储到 Arraylist 中, 您可以在 listView 中进一步显示列表

像这样在顶部定义Arralist ...

List<hikeModClass> list = new ArrayList<>();

用于在ArrayList中添加数据的代码

databaseReference = FirebaseDatabase.getInstance().getReference("hike-finder"); //hike-finder is your database name
    databaseReference.child("Hikes").child("myHikes").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
               if (snapshot.exists()) {

                        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                            hikeModClass report = dataSnapshot.getValue(hikeModClass.class);
                            list.add(report); //NOW ALL THE DATA ADDED INTO THE LIST YOU CAN SIMPLY DISPLAY IN LISTVIEW
                        }
                }
        }
});

现在,您必须在 ListView

中显示列表