0

i am not getting my list view on the screen. following code is used for application.when i tried Log.v("abcd",dataSnapshot.toString()); replies me the object as string in logcat. i dont understand in which part i have problem. logcat is not showing any errors. but my screen remains blank.

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;}
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shiva.AK
  • 29
  • 8
  • @Frank van Puffelen Thank you sir.. I have seen your Video tutorials in udacity courses.... i have added method notifyDataSetChanged(); but here i have another issue my screen is showing me the blank list item. ScreenShot is here..........drive.google.com/open?id=1EbFiYAbJD1JDFI8Wvm4ak8INgVMuCn7B – Shiva.AK Mar 04 '18 at 14:57
  • logcat form UploadingItemAdapter.class shows me null values..please check this ScreenShot of computer screen.drive.google.com/open?id=1Jxt5_GCslhfs16789glze9s2CNDkGwjd – Shiva.AK Mar 04 '18 at 14:57
  • check out solution small thing that we should keep in mind https://stackoverflow.com/questions/49092492/uploadingitemadapter-gives-logcat-null-value-and-listitems-text-views-are-also – Shiva.AK Mar 06 '18 at 17:28

1 Answers1

0

You need to tell Android when the data has changed, otherwise it doesn't refresh the list view. To do so, call notifyDataSetChanged() from onChildAdded (and later other methods where you change the data):

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();
    }
    ...

You'll note that I moved the creation of the adapter up, to make it clearer that it is initialized before onChildAdded() gets called.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807