Home > Software engineering >  I want to get the qna list through firebase, but I can't get the list
I want to get the qna list through firebase, but I can't get the list

Time:09-30

This is a list of qna in the firebase. I want to print this out.

But my output list doesn't show anything.

https://i.stack.imgur.com/6ctRB.png

QNA Activity

public class QnaActivity extends AppCompatActivity {
    private RecyclerView qnaRv;
    private ArrayList<ModelQna> qnaList;
    private AdapterQna adapterQna;
    private ImageButton writeBtn;
    private ImageButton backbtn;
    private TextView tabQnaTv;
    private RelativeLayout QnaRl;
    private FirebaseAuth firebaseAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qna);

        getSupportActionBar().hide();

        qnaRv = findViewById(R.id.qnaRv);

        tabQnaTv = findViewById(R.id.tabQnaTv);

        QnaRl = findViewById(R.id.QnaRl);

        backbtn = findViewById(R.id.backBtn);

        writeBtn = findViewById(R.id.writeBtn);

        firebaseAuth = FirebaseAuth.getInstance();

        loadAllQna();
        showQnaUI();


        backbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        writeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), AddQnaActivity.class);
                startActivity(intent);
            }
        });
        tabQnaTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //load products
            }
        });
    }

    private void loadAllQna() {
        qnaList = new ArrayList<>();
        adapterQna = new AdapterQna(this, qnaList);
        qnaRv.setAdapter(adapterQna);

        //get all products
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Employees");
        reference.child(firebaseAuth.getUid()).child("Qna").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                //before getting reset List
                qnaList.clear();
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    ModelQna modelQna = dataSnapshot.getValue(ModelQna.class);
                    qnaList.add(modelQna);
                }
            }

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

            }
        });
    }
    private void showQnaUI() {
        //show orders ui and hide products ui
        QnaRl.setVisibility(View.GONE);
    }
}

Model

public class ModelFaq {
    private String faqId,faqTitle,faqContent, timestamp,uid,faqCategory;

    public ModelFaq() {


    }
    public ModelFaq(String faqId, String faqTitle, String faqContent, String timestamp, String uid, String faqCategory) {
        this.faqId = faqId;
        this.faqTitle = faqTitle;
        this.faqContent = faqContent;
        this.timestamp = timestamp;
        this.uid = uid;
        this.faqCategory = faqCategory;
    }

    public String getFaqId() {
        return faqId;
    }

    public void setFaqId(String faqId) {
        this.faqId = faqId;
    }

    public String getFaqTitle() {
        return faqTitle;
    }

    public void setFaqTitle(String faqTitle) {
        this.faqTitle = faqTitle;
    }

    public String getFaqContent() {
        return faqContent;
    }

    public void setFaqContent(String faqContent) {
        this.faqContent = faqContent;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getFaqCategory() {
        return faqCategory;
    }

    public void setFaqCategory(String faqCategory) {
        this.faqCategory = faqCategory;
    }
}

Qna Adapter

public class AdapterQna extends  RecyclerView.Adapter<AdapterQna.HolderQna> {

    private Context context;
    public ArrayList<ModelQna> qnaList;

    public AdapterQna(Context context, ArrayList<ModelQna> qnaList) {
        this.context = context ;
        this.qnaList = qnaList ;
    }

    @NonNull
    @Override
    public HolderQna onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //inflate layout
        View view = LayoutInflater.from(context).inflate(R.layout.qna_item, parent, false);
        return new HolderQna(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HolderQna holder, int position) {

             ModelQna modelQna = qnaList.get(position);
        String id = modelQna.getQnaId();
        String uid = modelQna.getUid();
        String qnaContent = modelQna.getQnaContent();
        String qnaTitle = modelQna.getQnaTitle();
        String timestamp = modelQna.getTimestamp();
        //set data
        holder.titleTextView.setText(qnaTitle);
        holder.ContentTextView.setText(qnaContent);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //handle item clicks, show item details
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //handle item clicks, show item details
            }
        });



    }
    @Override
    public int getItemCount() {
        return qnaList.size();
    }

   class HolderQna extends RecyclerView.ViewHolder{
        /*holds views of recyclerview*/


        private TextView titleTextView, ContentTextView;

        public HolderQna(@NonNull View itemView) {
            super(itemView);

            titleTextView = itemView.findViewById(R.id.item_post_title);
            ContentTextView = itemView.findViewById(R.id.item_post_content);
        }
    }
}

CodePudding user response:

Can you please change the code in your HolderQna to:

   class HolderQna extends RecyclerView.ViewHolder{
        /*holds views of recyclerview*/


        private TextView titleTextView, ContentTextView;

        public HolderQna(@NonNull View itemView) {
            super(itemView);

            titleTextView = itemView.findViewById(R.id.item_post_title);
            ContentTextView = itemView.findViewById(R.id.item_post_content);
        }

        public void bind(qna: ModelQna){
        
        titleTextView.setText(qna.qnaTitle);
        ContentTextView.setText(qna.qnaContent);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //handle item clicks, show item details
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //handle item clicks, show item details
            }
        });
        }
    }

And your onBindViewHolder override of the AdapterQna to:

@Override
    public void onBindViewHolder(@NonNull HolderQna holder, int position) {

             ModelQna modelQna = qnaList.get(position);
        String id = modelQna.getQnaId();
        String uid = modelQna.getUid();
        String qnaContent = modelQna.getQnaContent();
        String qnaTitle = modelQna.getQnaTitle();
        String timestamp = modelQna.getTimestamp();
        //set data
        holder.bind(modelQna);
    }

Finally change your loadQna() function declaration as follows:

 private void loadAllQna() {
        qnaList = new ArrayList<>();

        //get all products
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Employees");
        reference.child(firebaseAuth.getUid()).child("Qna").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                //before getting reset List
                qnaList.clear();
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                    ModelQna modelQna = dataSnapshot.getValue(ModelQna.class);
                    qnaList.add(modelQna);
                }
                adapterQna = new AdapterQna(this, qnaList);
                qnaRv.setAdapter(adapterQna);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseerror) {
            Log.e("QnaActivity", databaseError.toString());

            }
        });
    }

CodePudding user response:

As far as I can see in your screenshot, the Qna node it's not nested under any UID, but directly under the Employees node. So when you attach a listener to the following node:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Employees");
reference.child(firebaseAuth.getUid()).child("Qna").addValueEventListener(/*...*/);

You will not be able to get any results, because such a reference doesn't exist. To solve this, you either create a reference that points exactly to "Qna":

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference qnaRef = db.child("Employees").child("Qna");
qnaRef.addValueEventListener(/*...*/);

Or you move the "Qna" node under the UID of the user, and leave the code unchanged.

Besides that, there is also another problem. The name of the fields in your ModelFaq class are different than the name of the properties in your database. You have in your ModelFaq class four fields called faqId, faqTitle, faqContent, faqCategory, all starting with faq while in the database I see that the names are different, qnaId, qnaTitle, qnaContent, qnaCategory, all are starting wiht qna, and this is not correct. So in order to be able to map a node into an object of type ModelFaq, the names must match. The only two fields that match are the timestamp and the uid.

In this case, you have two solutions. The first one would to change the name of your fieds in the ModelFaq class according to what it already exists in the database or you can use the PropertyName annotation in front of the getters like this:

@PropertyName("qnaId")
public String getFaqId() {
    return faqId;
}
  • Related