1

My database structure

enter image description here

I am trying to create an activity where I can display all the logs. Just timestamp and the log message. I have tried with firebaseUI and adapter but I can't get the data to show. Best I have done was to post same last log in all positions. This is what I have so far but no success. I am new to firebase and all I need is to display the logs in a list. It can be lisView or recyclerView. If anyone can help me with code or example. Thank you.

Database structure is | "logs" node / userId / logId / fields |

    public class LogActivity extends AppCompatActivity {
    private static final String TAG = "LogActivity";
    private static final int ACTIVITY_NUM = 3;

    //widgets
    private Context mContext = LogActivity.this;
    private RecyclerView mLogRecycleView;
    private TextView timeStamp, log;

    //firebase
    private DatabaseReference mLogDatabase;
    private FirebaseAuth mAuth;

    //adapter
    private FirebaseRecyclerAdapter adapter;

    //vars
    private String mCurrentUserID, logID;
    List<AppLogs> logsList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        Log.d(TAG, "onCreate: Started");

        mCurrentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

        mLogRecycleView = findViewById(R.id.recyclerList);
        mLogDatabase = FirebaseDatabase.getInstance().getReference().child("logs").child(mCurrentUserID);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        mLogRecycleView.setHasFixedSize(true);
        mLogRecycleView.setLayoutManager(linearLayoutManager);

        firebaseListAdapter();

        mLogRecycleView.setAdapter(adapter);

        setupBottomNavigationView();
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    private void firebaseListAdapter() {
        Log.d(TAG, "firebaseListAdapter: started");

        Query logQuery = mLogDatabase.orderByChild("time");

        FirebaseRecyclerOptions<AppLogs> options =
                new FirebaseRecyclerOptions.Builder<AppLogs>()
                .setQuery(logQuery, AppLogs.class).build();

        adapter = new FirebaseRecyclerAdapter<AppLogs, LogViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull final LogViewHolder holder, int position, @NonNull AppLogs model) {
                Log.d(TAG, "onBindViewHolder: started");

                //get the ID of the messages
                //final String logID = getRef(position).getKey();
                //Log.d(TAG, "onBindViewHolder: logID : " + logID);



                Query logQuery = mLogDatabase;


                logQuery.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        for (DataSnapshot singData : dataSnapshot.getChildren()) {
                            //AppLogs logs = dataSnapshot.getValue(AppLogs.class);
                            Log.d(TAG, "onChildAdded: log:==== " + singData.child("log").getValue());
                            //Log.d(TAG, "onChildAdded: log_ID:==== " + logs.getLog_id());


                            String log =  singData.child("log").getValue().toString();
//                        String timeStamp = Long.toString(logs.getTime());
//
                        holder.setLog(log);
//                        holder.setTimeStamp(timeStamp);
                        }

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });




            }

            @NonNull
            @Override
            public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                Log.d(TAG, "onCreateViewHolder: create users view holder: ");
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.layout_log_list_view, parent, false);
                return new LogViewHolder(view);
            }
        };

    }

    public static class LogViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public LogViewHolder(View itemView) {
            super(itemView);
            this.mView = itemView;
        }

        public void setLog(String log) {
            TextView tvLog = mView.findViewById(R.id.tvLog);
            tvLog.setText(log);
        }

        public void setTimeStamp(String timeStamp) {
            TextView tvTimeStamp = mView.findViewById(R.id.tvTimeStamp);
            tvTimeStamp.setText(timeStamp);
        }
    }



    /*
     *BottomNavigationView Setup
     */
    private void setupBottomNavigationView() {
        Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView");
        BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar);
        BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx);
        BottomNavigationViewHelper.enableNavigation(mContext, this, bottomNavigationViewEx);
        Menu menu = bottomNavigationViewEx.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);
    }
}

and my log model class

package com.logistics.alucard.socialnetwork.Models;

public class AppLogs {

private String log, log_id;
private long time;

public AppLogs(String log, String log_id, long time) {
    this.log = log;
    this.log_id = log_id;
    this.time = time;
}

public AppLogs() {
}

public String getLog() {
    return log;
}

public void setLog(String log) {
    this.log = log;
}

public String getLog_id() {
    return log_id;
}

public void setLog_id(String log_id) {
    this.log_id = log_id;
}

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jun 10 '18 at 08:54
  • There is also no need to use `mLogRecycleView.setHasFixedSize(true);`. – Alex Mamo Jun 10 '18 at 08:56
  • Thank you. The problem problem is I don't have the log id for the query, I can retrieve the user id but I have problems understanding how to loop trough the log ids and retrieve the data from each one into the `recycler view`. I am not sure how to construct the query. – Dragos Alexandru Stoian Jun 10 '18 at 17:38

1 Answers1

1

I manage to figure it out! Thank you for your help. Still a bit confusing how to build queries but I'll try to get better :)

This is my solution to the firebase retrieve data:

protected void onBindViewHolder(@NonNull final LogViewHolder holder, int position, @NonNull AppLogs model) {
            Log.d(TAG, "onBindViewHolder: started");

            //get the ID of the messages
            final String logID = getRef(position).getKey();
            //Log.d(TAG, "onBindViewHolder: logID : " + logID);



            Query logQuery = mLogDatabase;


            logQuery.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                   //AppLogs appLogs = dataSnapshot.getValue(AppLogs.class);
                    //Log.d(TAG, "onDataChange: logs:---------" + dataSnapshot.child(logID).child("log").getValue());
                    String log = dataSnapshot.child(logID).child("log").getValue().toString();
                    String timeStamp = dataSnapshot.child(logID).child("time").getValue().toString();
                    Log.d(TAG, "onDataChange: logs:--------------" + log);

                    holder.setLog(log);
                    holder.setTimeStamp(timeStamp);

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });