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

时间:2018-03-03 19:47:45

标签: android firebase-realtime-database android-arrayadapter android-fragmentactivity android-listfragment

我没有在屏幕上显示我的列表视图。以下代码用于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;}
}

1 个答案:

答案 0 :(得分:0)

您需要在数据更改时告诉Android,否则它不会刷新列表视图。为此,请从@Html.TextBox("search", new { @ViewBag.incremento }) (以及稍后更改数据的其他方法)中调用notifyDataSetChanged()

onChildAdded

你会注意到我已经移动了适配器的创建,以便更清楚它是在调用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(); } ... 之前初始化的。