gpt4 book ai didi

java - 从检索到的 firebase json 对象中提取单个数据值

转载 作者:行者123 更新时间:2023-12-02 02:23:30 24 4
gpt4 key购买 nike

在我的 android 应用程序中,我只想从 firebase json 对象的所有节点中检索单个值并检查...我的 firebase 数据库看起来像 -> enter image description here

现在我正在使用此代码来完成我的任务,但我每次都得到 null 作为输出..

public class optChos extends AppCompatActivity {
DatabaseReference ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_opt_chos);

final FirebaseDatabase database = FirebaseDatabase.getInstance();
ref = database.getReference("vendorDtl");
}

public void fetchkey(View view) {
EditText etx=findViewById(R.id.etkey);
String keysrch=etx.getText().toString();
Log.i("optionchoose","fetchkeycalled");
Toast.makeText(getApplicationContext(),"Key finfing",Toast.LENGTH_SHORT).show();


ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot vst:dataSnapshot.getChildren()) {
vendor vn = vst.getValue(vendor.class);
Toast.makeText(getApplicationContext(), "Email is-> " + vn.getNam(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "key is-> " + vn.getVky(), Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"nothing fetched",Toast.LENGTH_SHORT).show();
}
});
}



@Override
protected void onStart() {
super.onStart();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot vst:dataSnapshot.getChildren()) {
vendor vn = vst.getValue(vendor.class);
Toast.makeText(getApplicationContext(), "Email is-> " + vn.getNam(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "key is-> " + vn.getVky(), Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"nothing fetched",Toast.LENGTH_SHORT).show();
}
});
}

}

这里fetchkey(View view)是按钮的onclick函数..

I want to retrieve that vid from every node and check with a user entered value ..How can i do that?

最佳答案

要从每个节点获取 vid 字段的值,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference vendorDtlRef = rootRef.child("vendorDtl");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String vid = ds.child("vid").getValue(String.class);
Log.d("TAG", vid);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {}
};
vendorDtlRef.addListenerForSingleValueEvent(eventListener);

关于java - 从检索到的 firebase json 对象中提取单个数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48125543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com