I am making a social media app, I want to add a comment section for each post posted on firebase. Now I'm having difficulties in making the comment section. I've tried the below code but it keeps posting the comments on a single row and then crash the firebase. It is not creating a separate row for a new comment. After 1st comment is added the firebase data base looks like that:
but after I add another comment the "comments" value overrides instead of creating a new child post key.
This is how I'm sending comments to firebase:
final String message=myComment.getText().toString();
if (!TextUtils.isEmpty(message))
{
DatabaseComment.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("comments").setValue(message);
hDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("othersName")
.setValue(dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("name").getValue());
DatabaseComment.child(mPostKey).child(mAuth.getCurrentUser().getUid()).child("othersDP")
.setValue(dataSnapshot.child(mAuth.getCurrentUser().getUid()).child("DP").getValue());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
And this is my onStart Method where I'm making recyclerAdapter:
mAuth.addAuthStateListener(mAuthListener);
FirebaseRecyclerAdapter<Profile,ProfileViewHolder> firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Profile, ProfileViewHolder>(
Profile.class,
R.layout.commentttt_row,
ProfileViewHolder.class,
DatabaseCommenttttt
) {
#Override
protected void populateViewHolder(final ProfileViewHolder viewHolder, final Profile model, int position) {
//viewHolder.setName(model.getName());
viewHolder.setComments(model.getComments());
viewHolder.setOthersName(model.getOthersName());
viewHolder.setOthersDP(model.getOthersDP());
}
};
mProfileList.setLayoutManager(mLayoutManager);
mProfileList.setAdapter(firebaseRecyclerAdapter);
And this is my RecyclerView class:
public static class ProfileViewHolder extends RecyclerView.ViewHolder
{
View mView;
LinearLayout mLinearName;
TextView userName;
TextView mComment;
CircleImageView mSetupImageButton;
FirebaseAuth mAuth;
TextView post_name;
private String comments;
private String othersName;
private String othersDP;
public ProfileViewHolder(View itemView) {
super(itemView);
mView=itemView;
userName=(TextView) mView.findViewById(R.id.post_name);
mLinearName=(LinearLayout) mView.findViewById(R.id.layout_name);
mComment=(TextView) mView.findViewById(R.id.comment);
}
public void setImage(final Context ctx, final String image)
{
final ImageView post_image=(ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(ctx).load(image).into(post_image);
}
});
}
public void setComments(String comments) {
TextView post_phone=(TextView) mView.findViewById(R.id.others_comments);
post_phone.setText(comments);
}
public void setOthersName(String othersName) {
TextView post_phone=(TextView) mView.findViewById(R.id.others_name);
post_phone.setText(othersName);
}
public void setOthersDP(String othersDP) {
CircleImageView mSetupImageButton = (CircleImageView) mView.findViewById(R.id.accountImageButton);
Picasso.with(mView.getContext()).load(othersDP).into(mSetupImageButton);
}
}
Assuming you have a Comment class with comments, otherDP, otherName. now add a new member userId.
final String message=myComment.getText().toString();
if (!TextUtils.isEmpty(message))
{
DatabaseComment.child(mPostKey).addValueEventListener(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArryList<Comment> comments= (ArryList<Comment>)
dataSnapshot.getValue();
Comment comment = new Comment();
comment.setName("Name");
comment.setOtherDP("OtherDP");
comment.setComment("Comment");
comment.setUserID("UserID");
comments.add(comment);
DatabaseComment.child(mPostKey).setValue(comments);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
this will create an arrylist of comments inside everypost .. one user can comment multiple time.
Edit: When I use this
final String message=myComment.getText().toString();
if (!TextUtils.isEmpty(message))
{
DatabaseComment.child(mPostKey).addValueEventListener(new
ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Profile> comments= (ArrayList<Profile>)
dataSnapshot.getValue();
Profile comment = new Profile();
comment.setOthersName("Name");
comment.setOthersDP("OtherDP");
comment.setComments("Comment");
comment.setUid("UserID");
comments.add(comment);
DatabaseComment.child(mPostKey).setValue(comments);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
lOGCAT GIVE ME THIS: java.lang.NullPointerException: Attempt to invoke interface method 'void android.view.IWindowSession.remove(android.view.IWindow)' on a null object reference
Related
The problem that I am having is that when a user adds a comment the entire list refreshes because I have notifyDataSetChanged(); set on the CommentAdapter. Everything jumps, and refreshes and I want it to be smoother, so I decided to use notifyItemInserted(); instead of notifyDataSetChanged();, but it isn't doing anything different.
notifyItemInserted(); should only update the last item or the newest item added to the list, so why is everything being refreshed/updated...?
Can someone tell me how to fix this? Only want last item added to list to be "added"/"updated"/whatever, not the entire list because if many people start commenting everything is always reloading...
In my readComments(); what I have now is mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);, and what I had before was mCommentAdapter.notifyDataSetChanged();, but they are having the same effect. How can I fix this?
CommentsActivity
public class CommentsActivity extends AppCompatActivity {
private CommentAdapter mCommentAdapter;
private List<Comment> mCommentList;
private RecyclerView mRecyclerView;
EditText mAddComment;
ImageView mImageProfile;
TextView mPost;
String mPostId;
String mPublisherId;
String mNotificationId;
FirebaseUser mFirebaseUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(linearLayoutManager);
mCommentList = new ArrayList<>();
mCommentAdapter = new CommentAdapter(this, mCommentList, mPostId);
mRecyclerView.setAdapter(mCommentAdapter);
mAddComment = findViewById(R.id.add_comment);
mImageProfile = findViewById(R.id.image_profile);
mPost = findViewById(R.id.post_comment);
mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
mPost.setOnClickListener(v -> {
if (mAddComment.getText().toString().equals("")) {
Toast.makeText(CommentsActivity.this, "Can't send empty comments", Toast.LENGTH_SHORT).show();
} else {
addCommentNotification(mPublisherId, mPostId);
}
});
getImage();
readComments();
}
private void getImage() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(mFirebaseUser.getUid());
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
if (user != null)
Glide.with(getApplicationContext()).load(user.getImageurl()).into(mImageProfile);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
private void readComments() {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments").child(mPostId);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mCommentList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Comment comment = snapshot.getValue(Comment.class);
mCommentList.add(comment);
}
mCommentAdapter = new CommentAdapter(CommentsActivity.this, mCommentList, mPostId);
mRecyclerView.setAdapter(mCommentAdapter);
mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
I am guessing you should be using a ChildEventListener:
Your readComments() must be like this:
private void readComments() {
//your reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Comments").child(mPostId);
//the child listener
ChildEventListener listener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
// A new comment has been added
Comment comment = dataSnapshot.getValue(Comment.class);
mCommentList.add(comment);
//Notify adapter
mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
//attach the listener
ref.addChildEventListener(listener);
}
I have created an Android chat application which incorporates Firebase and created a fragment with a current users messages. The error occurs when a user sends a message to more than one person and the following image link shows how the first user is displayed whereas the most recent one who I have sent a message to is invisible. Does anyone know why this would be? I will include a few of my classes below.
My firebase is displayed as the following with messages containing first the user IDs, recipient IDs and then the unique message ID followed by the actual message and who sent it.
Thanks for your help.
https://i.stack.imgur.com/TrFUg.png
Here on the link below you will see two tabs, the first is not displaying and the second is. Both messages are to different recipients.
https://i.stack.imgur.com/M5LXa.png
Fragment Class
public class ChatFragment extends Fragment {
private RecyclerView convList;
private FirebaseAuth auth;
private DatabaseReference userDatabase, messageDatabase;
private View view;
public ChatFragment() {
// Required empty public constructor
}
// Chat Fragment initialised
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_chat, container, false);
convList = (RecyclerView) view.findViewById(R.id.conv_list);
convList.setHasFixedSize(true);
convList.setLayoutManager(new LinearLayoutManager(getContext()));
auth = FirebaseAuth.getInstance();
String currentUser;
currentUser = auth.getCurrentUser().getUid();
messageDatabase = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUser);
messageDatabase.keepSynced(true);
userDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
userDatabase.keepSynced(true);
return view;
}
// Use FirebaseRecyclerAdapter to populate each user of which messages have been sent between, the latest message sent between each user displayed, ordering the list by most recent message and the user's online status
#Override
public void onStart() {
super.onStart();
Query conversationQuery = messageDatabase.orderByChild("time");
FirebaseRecyclerAdapter<UserConversation, ConvViewHolder> friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<UserConversation, ConvViewHolder>(
UserConversation.class,
R.layout.activity_user_list,
ConvViewHolder.class,
conversationQuery
) {
#Override
protected void populateViewHolder(final ConvViewHolder friendsViewHolder, final UserConversation friends, int i) {
final String user_id = getRef(i).getKey();
Query lastMessageQuery = messageDatabase.child(user_id).limitToLast(1);
// Retrieving data stored in Firebase for each message and its information
lastMessageQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
ConvViewHolder.setMessage(data);
String time = dataSnapshot.child("time").getValue().toString();
ConvViewHolder.setTime(time);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
userDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
String online = dataSnapshot.child("online").getValue().toString();
friendsViewHolder.setName(userName);
friendsViewHolder.setUserImage(userThumb, getContext());
friendsViewHolder.setUserOnline(online);
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), Messenger.class);
intent.putExtra("user_id", user_id);
intent.putExtra("user_name", userName);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Log.i("Activity Not Found", "Error");
}
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
convList.setAdapter(friendsRecyclerViewAdapter);
}
public static class ConvViewHolder extends RecyclerView.ViewHolder {
static View mView;
public ConvViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public static void setMessage(String message){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(message);
}
public void setName(String name) {
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setUserImage(String thumb_image, Context ctx) {
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
public static void setTime (String time){
TextView test = (TextView) mView.findViewById(R.id.time_sent);
test.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", Long.parseLong(time)));
}
public void setUserOnline(String online) {
ImageView userOnlineView = (ImageView) mView.findViewById(R.id.user_single_online_icon);
if(online.equals("true")){
userOnlineView.setVisibility(View.VISIBLE);
} else {
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
}
I have created an Android chat application which incorporates Firebase and created a fragment with a current users messages. The error occurs when a user sends a message to more than one person and the following image link shows how the first user is displayed whereas the most recent one who I have sent a message to is invisible. Does anyone know why this would be? I will include a few of my classes below.
My firebase is displayed as the following with messages containing first the user IDs, recipient IDs and then the unique message ID followed by the actual message and who sent it.
Thanks for your help.
https://i.stack.imgur.com/TrFUg.png
https://i.stack.imgur.com/M5LXa.png
Fragment Class
public class ChatFragment extends Fragment {
private RecyclerView convList;
private FirebaseAuth auth;
private DatabaseReference userDatabase, messageDatabase;
private View view;
public ChatFragment() {
// Required empty public constructor
}
// Chat Fragment initialised
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_chat, container, false);
convList = (RecyclerView) view.findViewById(R.id.conv_list);
convList.setHasFixedSize(true);
convList.setLayoutManager(new LinearLayoutManager(getContext()));
auth = FirebaseAuth.getInstance();
String currentUser;
currentUser = auth.getCurrentUser().getUid();
messageDatabase = FirebaseDatabase.getInstance().getReference().child("Messages").child(currentUser);
messageDatabase.keepSynced(true);
userDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
userDatabase.keepSynced(true);
return view;
}
// Use FirebaseRecyclerAdapter to populate each user of which messages have been sent between, the latest message sent between each user displayed, ordering the list by most recent message and the user's online status
#Override
public void onStart() {
super.onStart();
Query conversationQuery = messageDatabase.orderByChild("time");
FirebaseRecyclerAdapter<UserConversation, ConvViewHolder> friendsRecyclerViewAdapter = new FirebaseRecyclerAdapter<UserConversation, ConvViewHolder>(
UserConversation.class,
R.layout.activity_user_list,
ConvViewHolder.class,
conversationQuery
) {
#Override
protected void populateViewHolder(final ConvViewHolder friendsViewHolder, final UserConversation friends, int i) {
final String user_id = getRef(i).getKey();
Query lastMessageQuery = messageDatabase.child(user_id).limitToLast(1);
// Retrieving data stored in Firebase for each message and its information
lastMessageQuery.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String data = dataSnapshot.child("message").getValue().toString();
ConvViewHolder.setMessage(data);
String time = dataSnapshot.child("time").getValue().toString();
ConvViewHolder.setTime(time);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
userDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
String online = dataSnapshot.child("online").getValue().toString();
friendsViewHolder.setName(userName);
friendsViewHolder.setUserImage(userThumb, getContext());
friendsViewHolder.setUserOnline(online);
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), Messenger.class);
intent.putExtra("user_id", user_id);
intent.putExtra("user_name", userName);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Log.i("Activity Not Found", "Error");
}
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
convList.setAdapter(friendsRecyclerViewAdapter);
}
public static class ConvViewHolder extends RecyclerView.ViewHolder {
static View mView;
public ConvViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public static void setMessage(String message){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(message);
}
public void setName(String name) {
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setUserImage(String thumb_image, Context ctx) {
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
public static void setTime (String time){
TextView test = (TextView) mView.findViewById(R.id.time_sent);
test.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", Long.parseLong(time)));
}
public void setUserOnline(String online) {
ImageView userOnlineView = (ImageView) mView.findViewById(R.id.user_single_online_icon);
if(online.equals("true")){
userOnlineView.setVisibility(View.VISIBLE);
} else {
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
}
In FirebaseRecyclerAdapter I can use the getRef(position).getKey() but I am using a RecyclerView.Adapter and I want get the key whenever the user click the checkbox. Here is my code in onBindViewHolder():
#Override
public void onBindViewHolder(#NonNull UsersViewHolders holder, final int position) {
holder.setName(mUserNameList.get(position));
holder.setDesc(mUserDescList.get(position));
holder.setImage(mUserPicList.get(position));
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//TODO: get the key
}
});
}
Is it possible to get the key in RecyclerView.Adapter?
I manage to solved my own question just by reconstructing my code.
1. In the adapter class, I created an interface for itemClickListener
public interface RecyclerViewItemClick {
public void OnItemClickListener(UsersViewHolders holder, int position);
}
and create a global
private RecyclerViewItemClick mListener;
add it to constructor
public MessageListAdapter(RecyclerViewItemClick listener) {
this.mListener = listener;
}
2. In onBindViewHolder() I passed the holder and the position
#Override
public void onBindViewHolder(#NonNull final UsersViewHolders holder, final int position) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.OnItemClickListener(holder, position);
}
});
}
3. In your activity, add this code. Declare a global variable for ArrayList to store the keys and call the addListenerForSingleValueEvent to your DatabaseReference
//mUserDatabase is a DatabaseReference
mUserDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String uid = datas.getKey();
//mUserKey is an ArrayList<String>
mUserKey.add(uid);
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
4. Now add this to get the key for every position
mAdapter = new MyAdapter(new MyAdapter.RecyclerViewItemClick() {
#Override
public void OnItemClickListener(PickMemberAdapter.UsersViewHolders holder, int position) {
String userKey = mUserKey.get(position);
if (!holder.mCheckBox.isChecked()) {
holder.mCheckBox.setChecked(true);
Toast.makeText(MainActivity.this, userKey, Toast.LENGTH_LONG).show();
} else {
holder.mCheckBox.setChecked(false);
Toast.makeText(MainActivity.this, userKey, Toast.LENGTH_LONG).show();
}
}
});
I'm building an app which shows data from firebase database in ListView adapter, but the listview doesn't fill with data. Database works. I tried this with textview.setText method on specially created layout.
public class MainActivityListFragment extends ListFragment {
private ArrayList<typ> typy;
private typAdapter typAdapter;
String tytul1, rodzaj1, godzina1, typ1, kurs1;
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mmecz1 = mRootRef.child("mecz1");
DatabaseReference mtytul1 = mmecz1.child("tytul1");
DatabaseReference mrodzaj1 = mmecz1.child("rodzaj1");
DatabaseReference mgodzina1 = mmecz1.child("godzina1");
DatabaseReference mtyp1 = mmecz1.child("typ1");
DatabaseReference mkurs1 = mmecz1.child("kurs1");
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
typy = new ArrayList<typ>();
typy.add(new typ(tytul1, rodzaj1, godzina1, typ1, kurs1));
typy.add(new typ("Portugalia - Islandia", "wygrana drużyny", "21:00", "1", "1.50"));
typAdapter = new typAdapter(getActivity(), typy);
setListAdapter(typAdapter);
getListView().setDivider(ContextCompat.getDrawable(getActivity(), android.R.color.black));
getListView().setDividerHeight(0);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
#Override
public void onStart() {
super.onStart();
mtytul1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text1 = dataSnapshot.getValue(String.class);
tytul1 = text1;
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(),"No internet connection",Toast.LENGTH_SHORT).show();
}
});
mrodzaj1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text2 = dataSnapshot.getValue(String.class);
rodzaj1=text2;
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mgodzina1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text3 = dataSnapshot.getValue(String.class);
godzina1 = text3;
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mtyp1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text4 = dataSnapshot.getValue(String.class);
typ1 = text4;
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
mkurs1.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String text5 = dataSnapshot.getValue(String.class);
kurs1 = text5;
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
You need to use a FirebaseListAdapter.
Add this dependency to your project:
compile 'com.firebaseui:firebase-ui-database:0.4.0'
Then follow these instruction from their Github readme:
https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md
This isn't working because the values in your Strings are null when the typy.add() is executed.
For achieving what you want, you have to figure out a way to add your strings in typy.add() inside your .addValueEventListener and calling typAdapter.notifyDataSetChanged() later.
You can also use Firebase UI, which has FirebaseListAdapters, if your ListView is simple.