我想申请预订。我需要在其他节点中查找一些数据,然后将其存储在变量中,并在其他节点插入新的数据输入。这是我想要的数据结构:
SELCT 1
例如,我想获取数据描述表格laundry_category节点并将其插入节点洗衣店。可能吗? 我在我的代码中尝试它但它不起作用。这是我的addLaundry.java代码:
addLaundry.java
{
"laundry_category" : {
"laundry_category1" : {
"name" : "clean laundry",
"description" : "xxxxxxx"
}
},
"laundry" : {
"laundry_category1" : {
"laundry_id_1" : {
"name" : "ABC",
"colour" : "red",
"description" : "xxxxx"
}
}
}
Laundry.java
DatabaseReference databaseLaundry;
DatabaseReference databaseCategory;
List<Laundry> laundryList;
@Override
protected void onStart() {
super.onStart();
databaseLaundry.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
laundryList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Laundry laundry = postSnapshot.getValue(Laundry.class);
laundryList.add(laundry);
}
LaundryList adapter = new LaundryList(TambahLaundryActivity.this, laundryList);
listViewLaundry.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_laundry);
btAddLaundry = (Button)findViewById(R.id.bn_add_laundry);
listViewLaundry = (ListView)findViewById(R.id.listViewLaundryLis);
laundryList = new ArrayList<>();
Intent intentAddLaundry = getIntent();
String id_category = intentAddLaundry.getStringExtra(MainActivity.category_id);
String description_category = intentTambahLaundry.getStringExtra(MainActivity.category_description);
databaseLaundry = FirebaseDatabase.getInstance().getReference("laundry").child(id_category);
databaseLaundry = FirebaseDatabase.getInstance().getReference("laundry").child(description_category);
btAddLaundry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addLaundry();
}
});
}
public void addLaundry() {
databaseLaundry.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
category a = postSnapshot.getValue(category.class);
String var_category_description = ((Map)a).get("category_description").toString();
String id = databaseLaundry.push().getKey();
Laundry laundry = new Laundry(id, name_laundry, var_category_description);
databaseLaundry.child(id).setValue(laundry);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
category.java
public class Laundry {
private String laundry_id;
private String laundry_nama;
private String laundry_alamat;
private String kategori_nama;
public Laundry() {
}
public Laundry(String laundry_id, String laundry_name, String category_description {
this.laundry_id = laundry_id;
this.laundry_nama = laundry_nama;
this.category_description = category_description;
}
public String getLaundry_id() {
return laundry_id;
}
public String getLaundry_name() {
return laundry_name;
}
public String getLaundry_alamat() {
return laundry_alamat;
}
public String getCategory_description() {
return category_description;
}
}
非常感谢你的帮助:))
答案 0 :(得分:0)
如果要在多个位置保存相同的数据,在上传数据时,需要在每个async function getMoviesFromApi() {
try {
let response = await fetch('https://facebook.github.io/react-native/movies.json');
let responseJson = await response.json();
return responseJson.movies;
} catch(error) {
console.error(error);
}
}
对象上执行相同的操作。所以为了达到这个目的,请使用这两个参考文献:
DatabaseReference
和
DatabaseReference idCateoryRef = FirebaseDatabase.getInstance().getReference("laundry").child(id_category);
代码中的错误是您初始化相同的DatabaseReference descriptionCategoryRef = FirebaseDatabase.getInstance().getReference("laundry").child(description_category);
两次。如上所述,您需要为每个操作设置不同的DatabaseReference
。
希望它有所帮助。