2017-09-11 3 views
0
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); 
     } 
    } 

// Cette partie du code que je suis confuse pour la récupération de données. J'espère que tu m'aideras avec ce dilemme. @Override public View onCreateView (dispositif de gonflage LayoutInflater, conteneur ViewGroup, Bundle savedInstanceState) { Visualiser vue = inflater.inflate (R.layout.activity_profile, conteneur, false); fb_view = FirebaseAuth.getInstance(); fd = FirebaseDatabase.getInstance(); dr = fd.getReference(). Enfant ("stdb"); mlist = (ListView) getView(). FindViewById (R.id.lv); FirebaseUser user = fb_view.getCurrentUser(); userID = user.getUid(); FBL = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged (@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (utilisateur! = Null) { // L'utilisateur est connecté Log.d (TAG, "onAuthStateChanged: signed_in:" + user.getUid()); //Toast.makeText(Fragment_Profile.this,"Successfully connecté avec: "+ user.getEmail(), Toast.LENGTH_SHORT). // show(); } else { // L'utilisateur est déconnecté Log.d (TAG, "onAuthStateChanged: signed_out"); //Toast.makeText(Fragment_Profile.this,"Successfully signed out. ", Toast.LENGTH_SHORT) .show(); } } }; dr.addValueEventListener (nouveau ValueEventListener() { @Override OnDataChange public void (DataSnapshot dataSnapshot) { showData (dataSnapshot);} Comment puis-je récupérer des données depuis Firebase à l'aide d'un fragment ListView?

  @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

L'application mobile fonctionne mais quand j'ai essayé de récupérer les données, il n'affiche pas les données spécifiques écrit à partir de la base de feu –

+0

quoi que vous fassiez est un héritage de bit, essayez d'utiliser FIrebaseUI avec FirebaseRecycleView. Sauf si vous avez plus d'usecase avancé. Voir ce https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md – bash

Répondre

1

Il est difficile de gérer la liste des lignes à exécution Listview n'est pas une bonne option pour afficher les données de Firebase.Rechercher Reclerview Code ici ->A very simple illustration here

+0

cela fonctionne-t-il à un fragment d'un tiroir de navigation? –

+0

@ChristopheResurreccion - oui, vous pouvez remplacer ListVIew par Recyclerview –