【问题标题】:how do I retrieve data from firebase with the use of a listview fragment?如何使用 listview 片段从 firebase 检索数据?
【发布时间】:2017-09-11 17:53:20
【问题描述】:
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) {


       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,
                             Bundle 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();
        fbl = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                    //Toast.makeText(Fragment_Profile.this,"Successfully signed in with: " + user.getEmail(),Toast.LENGTH_SHORT).
                    //show();
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged:signed_out");
                    //Toast.makeText(Fragment_Profile.this,"Successfully signed out.",Toast.LENGTH_SHORT).show();
                }
            }
        };
        dr.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                showData(dataSnapshot);
            }

            @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);

        }
    }

}

此片段用于在片段上显示列表视图,但列表视图无法从 firebase 获取数据。任何人都可以帮助我解决这个问题。提前致谢

【问题讨论】:

标签: android listview firebase navigation-drawer


【解决方案1】:

在运行时很难在列表视图中管理行列表。因此,Listview 不是显示 Firebase 数据的好选择。试试 Recyclerview。代码在这里 -> A very simple illustration here

【讨论】:

  • 它是否适用于导航抽屉中的片段?
  • @ChristopheResurreccion - 是的,你可以用 Recyclerview 替换任何 ListVIew
【解决方案2】:

您可以查看这个 github 项目,我在该项目中使用了包含 firebase 数据库数据的活动列表视图。 https://github.com/SUMIT0733/Smart-College/blob/master/app/src/main/java/com/example/star_0733/project/View_doc.java

但是要在fragment中使用它类似于activity,只需在fragment的onViewCreated()方法中添加代码即可。

【讨论】:

    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 2018-05-01
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多