屏幕没有显示列表视图,但来自 firebase 的数据快照不为空

Screen is not showing me the list View but datasnapshot from firebase is not empty

我没有在屏幕上看到我的列表视图。以下代码用于 application.when 我试过 Log.v("abcd",dataSnapshot.toString()); 回复我 logcat 中的对象作为字符串。我不明白我在哪一部分有问题。 logcat 没有显示任何错误。但是我的屏幕还是空白。

FragmentLastView.class

package com.example.shiva.gconnection.extendedVersion;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
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 com.example.shiva.gconnection.R;
import com.example.shiva.gconnection.UploadingDetails;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
import java.util.List;

public class FragmentLastView extends Fragment {
    private FirebaseDatabase mdatabase;
    private DatabaseReference mdbRef;
    private ListView mItemLV;
    private ChildEventListener childEventListener;
    private ArrayAdapter mUploadItemAdapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.uploaded_material,container,false);
        mdatabase = FirebaseDatabase.getInstance();
        mdbRef = mdatabase.getReference("College");

        final String subjectName = getArguments().getString("SubjectName");
        final String branchname = getArguments().getString("BranchName");
        final String class1Name = getArguments().getString("Class1Name");
        final String className = getArguments().getString("ClassName");


        final ArrayList<UploadingDetails> uploadingDetails = new ArrayList<>();
        mItemLV= (ListView)view.findViewById(R.id.lv_upload_item);


        childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                UploadingDetails updetails = dataSnapshot.getValue(UploadingDetails.class);
                uploadingDetails.add(updetails);
            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {}
            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        if (uploadingDetails==null){
            Log.v("abcd","this is null");
        }
        mdbRef.child(branchname).child(subjectName).child(className).child(class1Name).addChildEventListener(childEventListener);

        mUploadItemAdapter = new UploadItemAdapter(getContext(),R.layout.uploaded_material_child,uploadingDetails);
        mItemLV.setAdapter(mUploadItemAdapter);
        return view;
    }
}

UpdateItemAdapter.class

package com.example.shiva.gconnection.extendedVersion;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.shiva.gconnection.R;
import com.example.shiva.gconnection.UploadingDetails;

import java.util.List;

/**
 * Created by shiva on 3/3/18.
 */

public class UploadItemAdapter extends ArrayAdapter<UploadingDetails>{
    public UploadItemAdapter( Context context, int resource, List<UploadingDetails> objects) {
        super(context, resource, objects);
    }

    @Override
    public View getView(int position ,View convertView,ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.uploaded_material_child, parent, false);
        }
        TextView titalTV = (TextView) convertView.findViewById(R.id.title_item_upload_material);
        TextView discriptionTV= (TextView) convertView.findViewById(R.id.discription_item_upload_material);

        UploadingDetails uploadingDetails = getItem(position);
        titalTV.setText(uploadingDetails.getTitle());
        discriptionTV.setText(uploadingDetails.getDiscription());
        return convertView;
    }
}

uploaded_material.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="this is sample" />

    <ListView
        android:id="@+id/lv_upload_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/uploaded_material_child" />
</LinearLayout>

uploaded_material_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:id="@+id/title_item_upload_material"
        android:textSize="20dp"
        android:layout_margin="5dp"/>
    <TextView
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Discription"
        android:id="@+id/discription_item_upload_material"
        android:textSize="16dp" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="UploadedBy"
        android:id="@+id/uploaded_by"
        android:textSize="16dp"
        android:layout_margin="5dp"/>
</LinearLayout>

UploadingDetails.class

package com.example.shiva.gconnection;

public class UploadingDetails {
    private String mTitle;
    private String mDiscription;
    private String mArrayUri;

    public UploadingDetails(){
    }

    public UploadingDetails(String title, String discription,String arrayUri){
        this.mTitle= title;
        this.mDiscription=discription;
        this.mArrayUri=arrayUri;

    }

    public String getTitle(){
        return this.mTitle;
    }

    public String getDiscription(){
        return this.mDiscription;
    }

    public String getArrayUri(){return this.mArrayUri;}
}

你需要在数据改变时告诉Android,否则它不会刷新列表视图。为此,请从 onChildAdded 调用 notifyDataSetChanged()(以及稍后更改数据的其他方法):

mUploadItemAdapter = new UploadItemAdapter(getContext(),R.layout.uploaded_material_child,uploadingDetails);
childEventListener = new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        UploadingDetails updetails = dataSnapshot.getValue(UploadingDetails.class);
        uploadingDetails.add(updetails);
        mUploadItemAdapter.notifyDataSetChanged();
    }
    ...

您会注意到我将适配器的创建上移了,以便更清楚地表明它是在 onChildAdded() 被调用之前初始化的。