Firebase:当我知道1个值

时间:2017-06-28 03:50:16

标签: android firebase firebase-realtime-database

我想从孩子那里获取所有其他细节,我知道孩子的1个值I would like to fetch the description and the user

请帮助。 我想获得描述和用户的值。 这是我的代码;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1,container,false);
    list = (ListView)  view.findViewById(R.id.mylistview);
    final DatabaseReference rootRef = 
FirebaseDatabase.getInstance().getReference();
    final DatabaseReference errandsRef = rootRef.child("Errands");
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
            for(final DataSnapshot ds : dataSnapshot.getChildren()) {
                final String Errand = ds.child("Errand").getValue(String.class);

                ll.add(Errand);
                ListAdapter adp = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,ll);
                //ll.clear();
                list.setAdapter(adp);
                list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String item = (String) list.getItemAtPosition(position);

请帮助。我应该在onItemClick中放入哪些代码来获取用户以及已点击的项目的代码?

1 个答案:

答案 0 :(得分:1)

您可以使用.orderByChild().equalTo()。您知道点击项目的Errand

String item = (String) list.getItemAtPosition(position);
FirebaseDatabase.getInstance().getReference().child("Errands").orderByChild("Errand").equalTo(item).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                  //this dataSnapshot is the your object you want
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });