如何从使用listview片段的firebase检索数据?

问题描述:

package com.resurreccion.tophe.sma; 


import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.google.firebase.auth.FirebaseAuth; 
import com.google.firebase.auth.FirebaseUser; 
import com.google.firebase.database.DataSnapshot; 
import com.google.firebase.database.DatabaseError; 
import com.google.firebase.database.DatabaseReference; 
import com.google.firebase.database.FirebaseDatabase; 
import com.google.firebase.database.ValueEventListener; 

import java.util.ArrayList; 

import static com.resurreccion.tophe.sma.R.id.lv; 

/** 
* A simple {@link Fragment} subclass. 
* Use the {@link Fragment_Profile#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class Fragment_Profile extends Fragment { 

    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 
    private static final String TAG = "Fragment_Profile"; 


    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 


    public Fragment_Profile() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment Fragment_Profile. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static Fragment_Profile newInstance(String param1, String param2) { 


`enter code here`Fragment_Profile fragment = new Fragment_Profile(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    private FirebaseDatabase fd; 
    private FirebaseAuth fb_view; 
    private DatabaseReference dr; 
    private FirebaseAuth.AuthStateListener fbl; 
    private String userID; 
    private ListView mlist; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

//我是混淆了数据的检索的这部分代码。我希望你能帮助我解决这个难题。 @Override public view onCreateView(LayoutInflater inflater,ViewGroup container, Bundled savedInstanceState){ View view = inflater.inflate(R.layout.activity_profile,container,false); fb_view = FirebaseAuth.getInstance(); fd = FirebaseDatabase.getInstance(); dr = fd.getReference()。child(“stdb”); mlist =(ListView)getView()。findViewById(R.id.lv); FirebaseUser user = fb_view.getCurrentUser(); userID = user.getUid(); (@NonNull FirebaseAuth firebaseAuth){ FirebaseUser user = firebaseAuth.getCurrentUser();其中,FirebaseAuth.AuthStateListener()是一个新的FirebaseAuth.AuthStateListener()。 如果(!用户= NULL){// 用户在 Log.d签署(TAG, “onAuthStateChanged:signed_in:” + user.getUid()); “+ user.getEmail(),Toast.LENGTH_SHORT): //Toast.makeText(Fragment_Profile.this,"Successfully与登录。 // show(); }其他{// 用户签订了 Log.d(TAG, “onAuthStateChanged:signed_out”); //Toast.makeText(Fragment_Profile.this,"Successfully签订了“,Toast.LENGTH_SHORT).show()。 } } }; dr.addValueEventListener(新ValueEventListener(){ @覆盖 公共无效onDataChange(DataSnapshot dataSnapshot){ showData(dataSnapshot);} 如何从使用listview片段的firebase检索数据?

  @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

     return view; 
    } 


    private void showData(DataSnapshot dataSnapshot) { 
     for (DataSnapshot ds : dataSnapshot.getChildren()) { 
      UserInfo uInfo = new UserInfo(); 
      uInfo.setStudent_num(ds.child(userID).getValue(UserInfo.class).getStudent_num()); //set the student_name 
      uInfo.setPassword(ds.child(userID).getValue(UserInfo.class).getPassword()); //set the password 
      uInfo.setLast_name(ds.child(userID).getValue(UserInfo.class).getLast_name()); //set the last_name 
      uInfo.setFirst_name(ds.child(userID).getValue(UserInfo.class).getFirst_name()); //set the first_name 
      uInfo.setMiddle_name(ds.child(userID).getValue(UserInfo.class).getLast_name()); //set the middle_name 


      //display all the information 
      Log.d(TAG, "showData: student_name: " + uInfo.getStudent_num()); 
      Log.d(TAG, "showData: password: " + uInfo.getPassword()); 
      Log.d(TAG, "showData: last_name: " + uInfo.getLast_name()); 
      Log.d(TAG, "showData: first_name: " + uInfo.getFirst_name()); 
      Log.d(TAG, "showData: middle_name: " + uInfo.getMiddle_name()); 


      ArrayList<String> array = new ArrayList<>(); 
      array.add(uInfo.getStudent_num()); 
      array.add(uInfo.getPassword()); 
      array.add(uInfo.getLast_name()); 
      array.add(uInfo.getFirst_name()); 
      array.add(uInfo.getMiddle_name()); 
      ArrayAdapter adapter = new ArrayAdapter(Fragment_Profile.this.getActivity(), android.R.layout. 
        simple_list_item_1, 
        array); 
      mlist.setAdapter(adapter); 

     } 
    } 

} 


this fragment is use to display listview on a fragment but I the listview can't fetch the data from the firebase. Anyone there to help me out of this. Thanks in advance 
+0

的移动应用程序是工作,但后来当我试图检索数据不显示具体数据从firebase –

+0

写什么你在做什么有点遗留,尝试使用FIrebaseUI与FirebaseRecycleView。除非你有更高级的用例。看到这个https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md – bash

这是很难管理行的在运行时列表。在列表视图所以,列表视图不从火力地堡的数据显示一个很好的选择尝试Recylerview代码在这里 - >A very simple illustration here

+0

它工作到导航抽屉的片段? –

+0

@ChristopheResurreccion - 是的,你可以用Recyclerview替换任何ListVIew –