Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

How to show single value if multiple duplicate values exists?


I am using firebase database for storing data and firebase recycler adapter for showing the data.

RecyclerView recyclerView;
    static String SelectedCode;
    DatabaseReference root;

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

    Firebase.setAndroidContext(this);

    recyclerView=(RecyclerView)findViewById(R.id.recycler);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    root = FirebaseDatabase.getInstance().getReference();
    Query query = root.child("download").orderByChild("subjectCode");
    FirebaseRecyclerAdapter<CourseStorage, GetData.MessageViewHolder> adapter = new FirebaseRecyclerAdapter<CourseStorage, GetData.MessageViewHolder>(
            CourseStorage.class,
            android.R.layout.two_line_list_item,
            GetData.MessageViewHolder.class, query
    ) {
        @Override
        protected void populateViewHolder(MessageViewHolder viewHolder, final CourseStorage model, int position) {
            viewHolder.textView.setText(model.getSubjectCode());
        }
    };
    recyclerView.setAdapter(adapter);
}


public static class MessageViewHolder extends RecyclerView.ViewHolder{
    TextView textView;
    View mview;
    public MessageViewHolder(View itemView) {
        super(itemView);
        textView=(TextView)itemView.findViewById(android.R.id.text1);
        mview=itemView;

    }
}

The getSubjectCode function in CourseStorage class is

 public String getSubjectCode () {
    return subjectCode;
}

My database is Database structure

I am getting output as enter image description here

I dont want same subjectCode to be displayed twice. How can i achieve it?


Solution

  • There is no GROUP BY clause in the Firebase Database query language. As is quite often the case when using a NoSQL database, the solution is to store the data in the way your app wants to access it.

    In your case, since you're showing a list of subject codes, I'd store a list of subject codes in the database (in addition to your current data). For example:

    latestItemKeyPerSubjectCode
      "COEG301": "-KM...38N"
      "COEG341": "-KM...vd-"
      ...
    

    The exact value that you store really depends on your needs. In this case I stored the key of the most recent item with that subject code, but you could also store the file name (if you'd like to display that in your app) or simply true if you have no need for a meaningful value.