UploadingItemAdapter gives LogCat null value and l

2019-08-18 07:50发布

I don't know in which part i made mistake. In UploadItemAdapter.class,uploadingDetails.getTitle() and uploadingDetails.getDiscription() both gives me null in my log cat. In android listView screen textView of item is also remains blank Screenshot .My Computer Screen. My code is mentioned below. UploadItemAdapter.java

package com.example.shiva.gconnection.extendedVersion;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
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 = ((FragmentActivity) 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());
        Log.v("abcd",uploadingDetails.getTitle() +" "+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">

    <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:textColor="@android:color/black"
        android:layout_margin="5dp"/>
    <TextView
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Discription"
        android:textColor="@android:color/black"
        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:textColor="@android:color/black"
        android:layout_margin="5dp"/>
</LinearLayout>

FragmentLastView.java

package com.example.shiva.gconnection.extendedVersion;

import android.os.Bundle;
import android.support.annotation.Nullable;
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;

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");
        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

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

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

        childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                UploadingDetails updetails = dataSnapshot.getValue(UploadingDetails.class);
                uploadingDetails.add(updetails);
                mUploadItemAdapter.notifyDataSetChanged();
            }
            @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) {}
        };
        mdbRef.child(branchname).child(subjectName).child(className).child(class1Name).addChildEventListener(childEventListener);


    }
}

UploadingDetails.java

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;}
    }

2条回答
【Aperson】
2楼-- · 2019-08-18 08:14

I don't understand how it worked but when i changed following code problem was solved UploadindDetils.java

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 void setTitle(String title) {
        this. mTitle = title;
    }

    public void setDiscription(String discription) {
        this.mDiscription = discription;
    }

    public void setArrayUri(String arrayUri) {
        this.mArrayUri = arrayUri;
    }

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

    public String getArrayUri() {
        return this.mArrayUri;
    }
}
查看更多
小情绪 Triste *
3楼-- · 2019-08-18 08:21

The name of the fields in your model class must much to the name of the data on witch your want retrieve in firebase data reference node

查看更多
登录 后发表回答