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);
}
}
// Dieser Teil des Codes, den ich für das Abrufen von Daten verwechsle. Ich hoffe, Sie helfen mir bei diesem Dilemma. @Override Öffentlichkeit onCreateView (LayoutInflater inflater, Viewgroup Container, Bundle savedInstanceState) { Ansicht view = inflater.inflate (R.layout.activity_profile, Container, false); fb_view = FirebaseAuth.getInstance(); fd = FirebaseDatabase.getInstance(); dr = fd.getReference(). Kind ("stdb"); mlist = (ListView) getView(). FindViewById (R.id.lv); FirebaseUser user = fb_view.getCurrentUser(); BenutzerID = user.getUid(); fbl = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged (@NonNull FirebaseAuth FirebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user! = Null) { // Der Benutzer ist angemeldet Log.d (TAG, "onAuthStateChanged: signed_in:" + user.getUid()); //Toast.makeText(Fragment_Profile.this,"Erfolgreich angemeldet mit: "+ user.getEmail(), Toast.LENGTH_SHORT). // show(); } else { // Benutzer ist abgemeldet Log.d (TAG, "onAuthStateChanged: signed_out"); //Toast.makeText(Fragment_Profile.this,"Successfully sign out. ", Toast.LENGTH_SHORT) .show(); } } }; dr.addValueEventListener (neu ValueEventListener() { @Override public void OnDataChange (DataSnapshot dataSnapshot) { showdata (dataSnapshot); }Wie lade ich Daten aus Firebase mit einem Listview-Fragment?
@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
Die mobile App funktioniert, aber wenn ich dann versuchte, die Daten abzurufen, zeigt es nicht die spezifischen Daten geschrieben von der Firebase –
was auch immer Sie taten ist etwas veraltet, versuchen Sie FIrebaseUI mit FirebaseRecycleView. Es sei denn, Sie haben einen fortgeschritteneren Anwendungsfall. Sehen Sie dieses https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md – bash