[英]How to move the data that I have called from the Firebase real-time database, in a listview to another activity in android studio?
So, I have a simple listview application.所以,我有一个简单的列表视图应用程序。 When I click an item, it will send the data to the next activity using Bundle.当我单击一个项目时,它将使用 Bundle 将数据发送到下一个活动。 In the first time, I use this JSON structure第一次用这个JSON结构
"teacher": [
{
"name": "Carl",
"age": "40"
},
{
"name": "Johnson",
"age": "44"
}
],
And this is my Java code:这是我的 Java 代码:
Query query = FirebaseDatabase.getInstance().getReference("teacher");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
TeacherHelperClass teacherHelperClass = new TeacherHelperClass();
String name = data.child("name").getValue(String.class);
String age = data.child("age").getValue(String.class);
teacherHelperClass.setName(name);
teacherHelperClass.setAge(age);
arrayList.add(teacherHelperClass);
}
teacherAdapter.notifyDataSetChanged();
listView.setAdapter(teacherAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
Query _query = FirebaseDatabase.getInstance().getReference("teacher");
_query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String _name = dataSnapshot.child(String.valueOf(position)).child("name").getValue(String.class);
String _age = dataSnapshot.child(String.valueOf(position)).child("age").getValue(String.class);
Bundle bundle = new Bundle();
bundle.putString("dataName", _name);
bundle.putString("dataAge", _age);
TeacherDetailBottomSheet bottomSheetDialog = new TeacherDetailBottomSheet();
bottomSheetDialog.setArguments(bundle);
bottomSheetDialog.show(getSupportFragmentManager(), "exampleBottomSheet");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
The code works fine.代码工作正常。 But now I want to change the database to keyed JSON:但现在我想将数据库更改为键控 JSON:
"teacher": {
"001": {
"name": "Carl",
"age": "40"
},
"002": {
"name": "Johnson",
"age": "44"
}
}
And maybe add another child也许再加一个孩子
"teacher": {
"001": {
"name": "Carl",
"age": "40",
"email": "....",
"phone-number": "...."
},
"002": {
"name": "Johnson",
"age": "44",
"email": "....",
"phone-number": "...."
}
}
What should I change in the Java code?我应该在 Java 代码中更改什么? especially in the setOnItemClick method.特别是在 setOnItemClick 方法中。 I want to show name & age child in the listview, and send all the data (name, age, email, phone) to the next activity.我想在列表视图中显示孩子的姓名和年龄,并将所有数据(姓名、年龄、email、电话)发送到下一个活动。 I've searched for the similar question, but haven't found a clear solution.我搜索了类似的问题,但没有找到明确的解决方案。
The code should be like:代码应该是这样的:
Teacher Model class Should be like below:老师 Model class 应该如下:
Public class TeacherModel{
String key,name ,age;
// CREATE empty constructor ,GETTER and SETTER FOR THESE VARIABLES
}
Now, Create a list of TeacherModel class现在,创建一个 TeacherModel class 列表
List<TeacherModel> teachersList = new ArrayList<>();
FirebaseDatabase.getInstance().getReference("teacher").
addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
for (DataSnapshot data : dataSnapshot.getChildren()) {
TeacherModel teacher =
dataSnapshot.getValue(TeacherModel.class);
teacher.setKey(dataSnapshot.getKey());
teachersList.add(teacher);
}
teacherAdapter.notifyDataSetChanged();
listView.setAdapter(teacherAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
And to send Data from one activity to another, you can use shared ViewModel or can make a public class and a variable of that data type and set the value to that variable and access it in another activity;并且要将数据从一个活动发送到另一个活动,您可以使用共享 ViewModel 或可以创建公共 class 和该数据类型的变量并将值设置为该变量并在另一个活动中访问它;
Public class Common{
Public static List<TeacherModel> teacherList;
}
and in onClickListener you can set value to teacherList like Common.teacherList = teacherList;
在 onClickListener 中,您可以将值设置为teacherList,例如Common.teacherList = teacherList;
Now in the another activity you can get that list using Common.teacherList;现在在另一个活动中,您可以使用 Common.teacherList 获取该列表;
Feel free to ask if something is unclear.随意询问是否有不清楚的地方。 And, kindly mark this as the correct answer if it helps you.如果对您有帮助,请将此标记为正确答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.