片段内的Listview显示Firebase中没有数据

时间:2016-12-19 00:50:48

标签: android listview android-fragments firebase firebase-realtime-database

public class FindPeopleFragment extends Fragment{


    public FirebaseAuth.AuthStateListener AuthListener;
    DatabaseReference db = FirebaseDatabase.getInstance().getReference();
    DatabaseReference leaderListNamesTopLevel = db.child("Names");
    ArrayList<String> namesOfLeaders = new ArrayList<>();
    ListView lview;
    ListAdapter listAdapter;
   private static String uid; //uid of the user logged in.

    // 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";

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

    private OnFragmentInteractionListener mListener;

    public FindPeopleFragment() {
        // 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 FindPeopleFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FindPeopleFragment newInstance(String param1, String param2) {
        FindPeopleFragment fragment = new FindPeopleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @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) {
        // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_find_people, container, false);
        lview = (ListView) view.findViewById(R.id.lview);
        AuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    uid = user.getUid().toString();
                    getLeaderNameList(uid);
                    System.out.println(leaderListNamesTopLevel.child(uid));

                } else {
                    Log.i("Problem:", "couldn't login.");
                }
            }
        };FirebaseAuth.getInstance().addAuthStateListener(AuthListener);

        return inflater.inflate(R.layout.fragment_find_people, container, false);

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    public void getLeaderNameList(String userID){

        leaderListNamesTopLevel.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot d: dataSnapshot.getChildren()){
                    //System.out.println(d.getKey().toString()); //prints the names(keys)/dispnames
                    namesOfLeaders.add(d.getKey().toString());
                    System.out.print(namesOfLeaders.toString() + " , ");
                    System.out.println(+ namesOfLeaders.size());
                    //Print the items from the ArrayList Then the araylistSize


                }

                onPost(namesOfLeaders);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });



    }

    public void onPost(ArrayList<String> leaderList){
        listAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, leaderList);
        lview.setAdapter(listAdapter);
    }
}

主要方法是最后两种方法。

问题是,SysOutPrintLn是完美的,它打印列表,但listview在片段中没有显示任何内容。我该怎么办?

我将列表分配给listview,并且我在OnViewCreate方法中获得了listview,但它仍然没有在listview上显示任何内容。

修改

ACTIVITY_MAIN XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/flContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"

        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

FRAGMENT XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="aakarsh.familyshop.FindPeopleFragment">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list" />

</FrameLayout>

0 个答案:

没有答案