Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

java - retrieving values from firebasedatabase

I have a Firebase realtime database, and I add value to it, I then want to retrieve those values, but they don't show up on my screen.

here is the old code that works;

    //ChatActivity class

mDatabaseforAllChatMessages.child(mAuth.getCurrentUser().getUid()).child(allChatId).setValue(allChatMessagesGet);
            mDatabaseforAllChatMessages.child(userID).child(allChatId).setValue(allChatMessagesGet);

//AllChatMessages class
 mDatabaseRef.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener(){
 @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                AllChatMessagesGet allChatMessagesGet = postSnapshot.getValue(AllChatMessagesGet.class);



            }
            allChatMessagesAdapter = new AllChatMessagesAdapter(AllChatMessages.this, allChatMessagesGetList);

            recyclerView.setAdapter(allChatMessagesAdapter);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(AllChatMessages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

        }});

I have now changed this code to this:

//ChatActivity class
 mDatabaseforAllChatMessages.child(mAuth.getCurrentUser().getUid()).child(userID).child(uploadID).child(allChatId).setValue(allChatMessagesGet);
        mDatabaseforAllChatMessages.child(userID).child(mAuth.getCurrentUser().getUid()).child(uploadID).child(allChatId).setValue(allChatMessagesGet);

and:

//AllChatMessages class
 mDatabaseRef.child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for(DataSnapshot postSnapshot : dataSnapshot.getChildren()){
                AllChatMessagesGet allChatMessagesGet = postSnapshot.getValue(AllChatMessagesGet.class);


                allChatMessagesGetList.add(allChatMessagesGet);


            }
            allChatMessagesAdapter = new AllChatMessagesAdapter(AllChatMessages.this, allChatMessagesGetList);

            recyclerView.setAdapter(allChatMessagesAdapter);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(AllChatMessages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

So why am I not able to retrive my value from the new codes? How can I solve it?

public class AllChatMessagesAdapter extends RecyclerView.Adapter<AllChatMessagesAdapter.AllChatViewHolder> {

private Context mCtx;
private List<AllChatMessagesGet> ChatActivityList;

public AllChatMessagesAdapter(Context mctx, List<AllChatMessagesGet> chatActivityList){
    this.mCtx = mctx;
    this.ChatActivityList = chatActivityList;
}

@Override
public AllChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);

    View viewList_allChatMessagesLayout = inflater.inflate(R.layout.allchatmessages_layout_list, parent, false);


    return new AllChatViewHolder(viewList_allChatMessagesLayout);

}

@Override
public void onBindViewHolder(AllChatViewHolder holder, int position) {
    AllChatMessagesGet allChatMessagesGet = ChatActivityList.get(position);

    //from below class and Allchatmessagesget Class.
    holder.textViewTitleOfUpload.setText(allChatMessagesGet.getUploadTitleString());
    holder.textviewTitleOfMessage.setText(allChatMessagesGet.getMessageTextsent());

    Glide.with(mCtx)
            .load(allChatMessagesGet.getUploadImageURL())
            .into(holder.imageView);
    holder.imageView.setRotation(allChatMessagesGet.getUploadImageRotation());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //create new intent to start another activity
            //add extras
            //startactivity
        }
    });
}


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

public class AllChatViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public TextView textViewTitleOfUpload, textviewTitleOfMessage;

    public AllChatViewHolder(View itemView) {
        super(itemView);
        //taken from allchatmessages_layout_list.xml
        imageView = itemView.findViewById(R.id.imageofupload);
        textViewTitleOfUpload = itemView.findViewById(R.id.textViewTitle);
        textviewTitleOfMessage = itemView.findViewById(R.id.textViewmessage);
    }
}
}

I have added the Adapter class aswell now.

here is full code: PLEASE IGNORE the COMMENTS on my classes since they are not correct comments and will be deleted

public class ChatActivity extends AppCompatActivity {

private String titleString, userID, uploadID, imageURL, userOnlineString, userEmail, uniqueID;
private Boolean userOnlineBoolean;
private int imageRotation;
private TextView titleCustomBar, lastSeenCustomBar;
private ImageView imageCustomBar;
private DatabaseReference mDatabaseRefUser, mDatabaseforAllChatMessages;
private FirebaseAuth mAuth;
private EditText addMessageEditText;
private FloatingActionButton sendMessageFab;
private ImageButton addImageToMessageImageButton;
private FirebaseListAdapter<ChatMessage> adapter;
private String allChatId;



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



    //activity_chat.xml
    addImageToMessageImageButton = findViewById(R.id.add_image_id);
    addMessageEditText = findViewById(R.id.add_message_input_id);
    sendMessageFab = findViewById(R.id.send_message_fab_id);




    mAuth = FirebaseAuth.getInstance();
    mDatabaseRefUser = FirebaseDatabase.getInstance().getReference("Users");
    mDatabaseforAllChatMessages = FirebaseDatabase.getInstance().getReference("AllChatMessages");



    //Log.e("Lol", userOnlineString);

    mDatabaseRefUser.child(mAuth.getCurrentUser().getUid()).child("Online").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            userOnlineBoolean = dataSnapshot.getValue(Boolean.class);
            //if(userOnlineBoolean==true){
              //  userOnlineString = "Online";
                //setCustomOnlineOffline();
            //}

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ChatActivity.this, "some error", Toast.LENGTH_SHORT).show();
        }
    });


    Intent intent = getIntent();
    titleString = intent.getStringExtra("TITLE");
    userID = intent.getStringExtra("USERID");
    uploadID = intent.getStringExtra("UPLOADID");
    imageRotation = intent.getIntExtra("IMAGEROTATION", 0);
    imageURL = intent.getStringExtra("IMAGEURL");
    userEmail = intent.getStringExtra("USEREMAIL");




    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowCustomEnabled(true);

    LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View customActionBar = layoutInflater.inflate(R.layout.chat_custombar, null);

    actionBar.setCustomView(customActionBar);

    titleCustomBar = findViewById(R.id.title_textview_id);
    lastSeenCustomBar = findViewById(R.id.last_seen_textview_id);
    imageCustomBar = findViewById(R.id.custom_image_id);

    titleCustomBar.setText(titleString);

    //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Glide.with(this).load(imageURL)
            .into(imageCustomBar);
    imageCustomBar.setRotation(imageRotation);

    //lastSeenCustomBar.setText(userOnlineString);

    /*mDatabaseRefUser.child(mAuth.getCurrentUser().getUid()).child("Chats").child(userID);
    HashMap<String, String> userMap = new HashMap<>();
    userMap.put("name", mAuth.getCurrentUser().getEmail());
    userMap.put("Chat with;", userEmail);
    userMap.put("image", uploadID);*/


    //mDatabaseRefUser.setValue(userMap);

    //uniqueID = mDatabaseRefUser.push().getKey();

    displayChatMessage();


}

private void setCustomOnlineOffline() {
    lastSeenCustomBar.setText(userOnlineString);
}

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

    if(mAuth.getCurrentUser() == null){
        Intent mainActivityIntent = new Intent(this, MainActivity.class);
        startActivity(mainActivityIntent);
        finish();
    }
    else {
        mDatabaseRefUser.child(mAuth.getCurrentUser().getUid()).child("Online").setValue(true);

    }
}


public void sendMessageToAnotherUser(View view) {
    if(addMessageEditText.getText().toString().isEmpty()){
        return;
    }
    else {
        ChatBetweenUsers chatBetweenUsers = new ChatBetweenUsers(mAuth.getCurrentUser().getUid(), userID,
                mAuth.getCurrentUser().getEmail(), userEmail, uploadID);


        ChatBetweenUsers chatBetweenUsers2 = new ChatBetweenUsers(userID, mAuth.getCurrentUser().getUid(),
                userEmail, mAuth.getCurrentUser().getEmail(), uploadID);
        //user email is null! because old uploaded pics had no useremail in constructor. add new pics for change!

        // add .child"messages" after .childuploadID ???
        mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
                .child(userID).child(uploadID).setValue(chatBetweenUsers);
        mDatabaseRefUser.child("Chat").child(userID)
                .child(mAuth.getCurrentUser().getUid()).child(uploadID).setValue(chatBetweenUsers2);



        //FirebaseDatabase.getInstance().getReference().push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
          //      FirebaseAuth.getInstance().getCurrentUser().getEmail()));



        //This codes overwrites the previous value, maybe because of above code that setValue(chatbetweenUsers),
        // must be without it
       // mDatabaseRefUser.child("Chat").child(mAuth.getCurrentUser().getUid())
               // .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
              //  mAuth.getCurrentUser().getEmail()));

        //mDatabaseRefUser.child("Chat").child(userID)
          //      .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
            //    userEmail));

        mDatabaseRefUser.child("Chatting").child(mAuth.getCurrentUser().getUid())
                .child(userID).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                mAuth.getCurrentUser().getEmail()));

        mDatabaseRefUser.child("Chatting").child(userID)
                .child(mAuth.getCurrentUser().getUid()).child(uploadID).child("Messages").push().setValue(new ChatMessage(addMessageEditText.getText().toString(),
                userEmail));




        allChatId = mDatabaseforAllChatMessages.push().getKey();

        AllChatMessagesGet allChatMessagesGet = new AllChatMessagesGet(imageURL, titleString, imageRotation, addMessageEditText.getText().toString(),
                mAuth.getCurrentUser().getEmail(), userEmail);

        mDatabaseforAllChatMessages.child(mAuth.getCurrentUser().getUid()).child(userID).child(uploadID).child(allChatId).setValue(allChatMessagesGet);
        mDatab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...