从Firebase到回收者视图中检索数据时,文本字段为空

时间:2019-03-18 15:08:09

标签: android firebase-realtime-database

我正在尝试构建一个从Firebase实时数据库检索数据的android应用程序。我已经编码以片段形式显示从Firebase到recyclerview的数据。项目已成功构建,但在运行应用程序时未显示,文本字段为空,其中一些显示了我提供的文本值以供参考。我在Firebase适配器中查找了代码的任何更改,并在Internet上搜索了解决方案。

  

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:backgroundTint="@color/txtcolor"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/recycler_design"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/worker_name"
                android:textSize="16sp"
                android:text="Name:  "
                android:textColor="@color/black"/>

            <TextView
                android:id="@+id/debit_workerName"
                android:layout_width="225dp"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/debit_date"
                android:layout_width="96dp"
                android:layout_height="wrap_content"
                android:textAlignment="textEnd"
                android:textColor="@color/black"
                android:textSize="14sp" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/white"/>

        <LinearLayout
            android:layout_width="392dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/billNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Bill Number:  "
                    android:textColor="@color/black"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/debit_billNumber"
                    android:layout_width="94dp"
                    android:layout_height="match_parent"
                    android:textColor="@color/blue"
                    android:textSize="14sp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="202dp"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/commi"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Comm:  "
                    android:textColor="@color/black"
                    android:textSize="18sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/Rs"
                    android:textColor="@color/black"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/debit_commission"
                    android:layout_width="123dp"
                    android:layout_height="match_parent"
                    android:textColor="@color/selected_red"
                    android:textSize="18sp" />
            </LinearLayout>


        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/white"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:padding="3dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/billAmt"
                android:textSize="14sp"
                android:text="Bill Amount:  "
                android:textColor="@color/black"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:text="@string/Rs"
                android:textColor="@color/black"/>

            <TextView
                android:id="@+id/debit_billAmount"
                android:layout_width="280dp"
                android:layout_height="wrap_content"
                android:textColor="@color/greentxt"
                android:textSize="14sp" />

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
  

模型类

public class modelDebit {

    public String wName, bill_no,bill_amt,date,comm;

    public String getwName() {
        return wName;
    }

    public void setwName(String wName) {
        this.wName = wName;
    }

    public String getBill_no() {
        return bill_no;
    }

    public void setBill_no(String bill_no) {
        this.bill_no = bill_no;
    }

    public String getBill_amt() {
        return bill_amt;
    }

    public void setBill_amt(String bill_amt) {
        this.bill_amt = bill_amt;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getComm() {
        return comm;
    }

    public void setComm(String comm) {
        this.comm = comm;
    }

    public modelDebit(String wName, String bill_no, String bill_amt, String date, String comm) {
        this.wName = wName;
        this.bill_no = bill_no;
        this.bill_amt = bill_amt;
        this.date = date;
        this.comm = comm;
    }

    public modelDebit() {


    }
}
  

Logcat

 --------- beginning of system
2019-03-18 20:22:10.572 1600-2206/? I/ActivityManager: START u0 {cmp=......./.Transaction} from uid 10081
2019-03-18 20:22:10.578 5039-5070/V/FA: Recording user engagement, ms: 14804
2019-03-18 20:22:10.582 5039-5070/ V/FA: Connecting to remote service
2019-03-18 20:22:10.585 5039-5070/ V/FA: Activity paused, time: 1230916
2019-03-18 20:22:10.613 5039-5070/ D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=14804, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-657156986720031714}]
2019-03-18 20:22:10.654 5039-5070/ V/FA: Connection attempt already in progress
2019-03-18 20:22:10.654 5039-5039/ V/FA: onActivityCreated
2019-03-18 20:22:10.749 5039-5070/ D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-657156986720031714, firebase_screen_class(_sc)=Transaction, firebase_screen_id(_si)=-657156986720031713}]
2019-03-18 20:22:10.793 1400-1453/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
2019-03-18 20:22:10.802 1400-1453/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
2019-03-18 20:22:10.805 5039-5070/ V/FA: Connection attempt already in progress
2019-03-18 20:22:10.807 5039-5070/ V/FA: Connection attempt already in progress
2019-03-18 20:22:10.807 5039-5070/ V/FA: Activity resumed, time: 1231049
2019-03-18 20:22:10.815 1400-1453/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496
2019-03-18 20:22:10.849 5039-5077/ D/EGL_emulation: eglMakeCurrent: 0xa17055a0: ver 2 0 (tinfo 0xa1703510)
2019-03-18 20:22:10.872 1600-1621/? I/ActivityManager: Displayed /.Transaction: +294ms
2019-03-18 20:22:10.878 5039-5070/ D/FA: Connected to remote service
2019-03-18 20:22:10.879 5039-5077/ D/EGL_emulation: eglMakeCurrent: 0xa17055a0: ver 2 0 (tinfo 0xa1703510)
2019-03-18 20:22:10.882 5039-5070/ V/FA: Processing queued up service tasks: 4
2019-03-18 20:22:10.888 5039-5077/ D/EGL_emulation: eglMakeCurrent: 0xa17055a0: ver 2 0 (tinfo 0xa1703510)
2019-03-18 20:22:10.902 5039-5046/ I/zygote: Do full code cache collection, code=243KB, data=173KB
2019-03-18 20:22:10.903 5039-5046/I/zygote: After code cache collection, code=239KB, data=134KB
2019-03-18 20:22:10.918 2208-5079/? V/FA-SVC: Logging event: origin=auto,name=user_engagement(_e),params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=14804, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-657156986720031714}]
2019-03-18 20:22:10.936 2208-5079/? V/FA-SVC: Saving event, name, data size: user_engagement(_e), 68
2019-03-18 20:22:10.937 2208-5079/? V/FA-SVC: Event recorded: Event{appId='FA......', name='user_engagement(_e)', params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=14804, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-657156986720031714}]}
2019-03-18 20:22:10.938 2208-5079/? V/FA-SVC: Upload scheduled in approximately ms: 2445285
2019-03-18 20:22:10.938 2208-5079/? V/FA-SVC: Cancelling job. JobID: 812057698
2019-03-18 20:22:10.943 2208-5079/? V/FA-SVC: Scheduling upload with GcmTaskService
2019-03-18 20:22:10.943 2208-5079/? V/FA-SVC: Scheduling task with Gcm. time: 2445285
2019-03-18 20:22:10.950 2208-5079/? V/FA-SVC: Background event processing time, ms: 33
2019-03-18 20:22:10.961 2208-5079/? V/FA-SVC: Logging event: origin=auto,name=screen_view(_vs),params=Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-657156986720031714, firebase_screen_class(_sc)=Transaction, firebase_screen_id(_si)=-657156986720031713}]
2019-03-18 20:22:10.967 2208-5079/? V/FA-SVC: Saving event, name, data size: screen_view(_vs), 96
2019-03-18 20:22:10.967 2208-5079/? V/FA-SVC: Event recorded: Event{appId='FA...', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=MainActivity, firebase_previous_id(_pi)=-657156986720031714, firebase_screen_class(_sc)=Transaction, firebase_screen_id(_si)=-657156986720031713}]}
2019-03-18 20:22:10.968 2208-5079/? V/FA-SVC: Upload scheduled in approximately ms: 2445255
2019-03-18 20:22:10.970 2208-5079/? V/FA-SVC: Cancelling job. JobID: 812057698
2019-03-18 20:22:10.973 2208-5079/? V/FA-SVC: Scheduling upload with GcmTaskService
2019-03-18 20:22:10.973 2208-5079/? V/FA-SVC: Scheduling task with Gcm. time: 2445255
2019-03-18 20:22:10.975 2208-5079/? V/FA-SVC: Background event processing time, ms: 14
2019-03-18 20:22:11.169 5039-5039/ W/ClassMapper: No setter/field for time found on class .modelCredit
2019-03-18 20:22:11.169 5039-5039/ W/ClassMapper: No setter/field for Cname found on class .modelCredit
2019-03-18 20:22:11.204 5039-5039/ W/ClassMapper: No setter/field for time found on class FA.modelDebit
2019-03-18 20:22:11.204 5039-5039/ W/ClassMapper: No setter/field for Cname found on class .modelDebit
2019-03-18 20:22:11.207 5039-5046/ I/zygote: Do partial code cache collection, code=240KB, data=149KB
2019-03-18 20:22:11.208 5039-5046/ I/zygote: After code cache collection, code=240KB, data=149KB
2019-03-18 20:22:11.208 5039-5046/ I/zygote: Increasing code cache capacity to 1024KB
2019-03-18 20:22:11.227 5039-5039/ W/ClassMapper: No setter/field for time found on class FA.modelDebit
2019-03-18 20:22:11.227 5039-5039/ W/ClassMapper: No setter/field for Cname found on class FA.modelDebit
2019-03-18 20:22:11.251 5039-5039/ W/ClassMapper: No setter/field for time found on class ......modelDebit
2019-03-18 20:22:11.251 5039-5039/... W/ClassMapper: No setter/field for Cname found on class ........modelDebit
2019-03-18 20:22:11.271 5039-5039/..... W/ClassMapper: No setter/field for time found on class ........modelDebit
2019-03-18 20:22:11.271 5039-5039/....... W/ClassMapper: No setter/field for Cname found on class .......modelDebit
2019-03-18 20:22:11.288 5039-5039/....... W/ClassMapper: No setter/field for time found on class .........modelDebit
2019-03-18 20:22:11.288 5039-5039/....... W/ClassMapper: No setter/field for Cname found on class ..........modelDebit
2019-03-18 20:22:11.303 5039-5039/......... W/ClassMapper: No setter/field for time found on class ............modelDebit
2019-03-18 20:22:11.303 5039-5039/.......... W/ClassMapper: No setter/field for Cname found on class ............modelDebit
2019-03-18 20:22:11.316 5039-5039/.......... W/ClassMapper: No setter/field for time found on class ........modelDebit
2019-03-18 20:22:11.316 5039-5039/........ W/ClassMapper: No setter/field for Cname found on class ........modelDebit
2019-03-18 20:22:11.319 5039-5046/.......... I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
2019-03-18 20:22:11.350 5039-5039/........... I/TextView Start: here it start adding values to text feild
  

包装类

[import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
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 com.google.firebase.database.ValueEventListener;



public class Debit extends Fragment {

    private View debitView;
    private RecyclerView debitRecyletList;
    private DatabaseReference debitRef;
    private FirebaseAuth firebaseAuth;


    public Debit() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        debitView= inflater.inflate(R.layout.fragment_debit, container, false);

        firebaseAuth=FirebaseAuth.getInstance();

        debitRecyletList = (RecyclerView) debitView.findViewById(R.id.debit_recyclerView);
        debitRecyletList.setHasFixedSize(true);
        debitRecyletList.setLayoutManager(new LinearLayoutManager(getContext()));
        debitRef = FirebaseDatabase.getInstance().getReference("debit");
        debitRef.keepSynced(true);


        return debitView;
    }


    @Override
    public void onStart() {
        super.onStart();

        FirebaseRecyclerOptions options1 = new FirebaseRecyclerOptions.Builder<modelDebit>()
                .setQuery(debitRef, modelDebit.class)
                .build();

        FirebaseRecyclerAdapter<modelDebit, debitViewHolder> adapter1 = new FirebaseRecyclerAdapter<modelDebit, debitViewHolder>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final debitViewHolder holder1, int position, @NonNull modelDebit model1) {


                debitRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()) {

                            String workerNmeDebit1 = dataSnapshot.child("wName").getKey().toString();
                            String billDateDebit1 = dataSnapshot.child("date").getKey().toString();
                            String billNoDebit1 = dataSnapshot.child("bill_no").getKey().toString();
                            String billAmntDebit1 = dataSnapshot.child("bill_amt").getKey().toString();
                            String workerComsnDebit1 = dataSnapshot.child("comm").getKey().toString();

                            Log.i("TextView Start","here it start adding values to text feild");

                            holder1.bill_amtD.setText(billAmntDebit1);
                            holder1.bill_NoD.setText(billNoDebit1);
                            holder1.dateD.setText(billDateDebit1);
                            holder1.nameD.setText(workerNmeDebit1);
                            holder1.commD.setText(workerComsnDebit1);




                        }
                        else {
                            Toast.makeText(getContext(), getString(R.string.error_msg), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });


            }

            @NonNull
            @Override
            public debitViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup1, int i) {


                View view1 = LayoutInflater.from(viewGroup1.getContext()).inflate(R.layout.debitdesign, viewGroup1, false);
                debitViewHolder viewHolder1 = new debitViewHolder(view1);
                return viewHolder1;


            }
        };

        debitRecyletList.setAdapter(adapter1);
        adapter1.startListening();


    }

    public static class debitViewHolder extends RecyclerView.ViewHolder{

        TextView nameD, dateD, bill_amtD,  bill_NoD, commD;

        public debitViewHolder(@NonNull View itemView1) {
            super(itemView1);

            nameD = itemView1.findViewById(R.id.debit_workerName);
            dateD = itemView1.findViewById(R.id.debit_date);
            bill_amtD = itemView1.findViewById(R.id.debit_commission);
            bill_NoD = itemView1.findViewById(R.id.debit_billNumber);
            commD = itemView1.findViewById(R.id.debit_commission);


        }
    }
}

database view

1 个答案:

答案 0 :(得分:0)

从数据库中,您似乎没有将Cnametime放在模型类中。

我已经更新了您的模型课程。

public class modelDebit {

private String wName;
private String bill_no;
private String bill_amt;
private String date;
private String comm;
private String Cname;
private String time;

public modelDebit() {
}

public modelDebit(String wName, String bill_no, String bill_amt, String date, String comm, String cname, String time) {
    this.wName = wName;
    this.bill_no = bill_no;
    this.bill_amt = bill_amt;
    this.date = date;
    this.comm = comm;
    Cname = cname;
    this.time = time;
}

public String getwName() {
    return wName;
}

public void setwName(String wName) {
    this.wName = wName;
}

public String getBill_no() {
    return bill_no;
}

public void setBill_no(String bill_no) {
    this.bill_no = bill_no;
}

public String getBill_amt() {
    return bill_amt;
}

public void setBill_amt(String bill_amt) {
    this.bill_amt = bill_amt;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getComm() {
    return comm;
}

public void setComm(String comm) {
    this.comm = comm;
}

public String getCname() {
    return Cname;
}

public void setCname(String cname) {
    Cname = cname;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}
}

之后,从此更改您的数据库引用:

 debitRef = FirebaseDatabase.getInstance().getReference("debit");

成为这个:

 debitRef = FirebaseDatabase.getInstance().getReference().child("debit");

接下来,将FirebaseRecyclerAdapter更改为此。

 FirebaseRecyclerAdapter<modelDebit, debitViewHolder> adapter1 = new FirebaseRecyclerAdapter<modelDebit, debitViewHolder>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final debitViewHolder holder1, int position, @NonNull modelDebit model1) {



            String workerNmeDebit1 = model1.getwName();
            String billDateDebit1 = model1.getDate();
            String billNoDebit1 = model1.getBill_no();
            String billAmntDebit1 = model1.getBill_amt();
            String workerComsnDebit1 = model1.getComm();

            holder1.bill_amtD.setText(billAmntDebit1);
            holder1.bill_NoD.setText(billNoDebit1);
            holder1.dateD.setText(billDateDebit1);
            holder1.nameD.setText(workerNmeDebit1);
            holder1.commD.setText(workerComsnDebit1);

        }
    }