Firebase Listview not displayed until open and close an Activity - android

I have a problem with my ListView that uses an adapter and is filled with Firebase content. But it does not show up at the launch of the activity until I open an activity and return to the activity.
here is my FirebaseHelper code
public class FirebaseHelper {
DatabaseReference db;
QuestionAdapter adapter;
View v;
ArrayList<Questions> questionEntries =new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db=db;
}
/*private void fetchData (DataSnapshot dataSnapshot){
questionEntries.clear();
Questions questions = dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);}*/
public ArrayList<Questions> retreive (){
db.orderByChild("question_date").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Questions questions =dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);
}
#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) {
}
});
return questionEntries;
}
}
here is the activity.
I tried to use Visibility to display only when there is content but it does not always work
this is my activity:
public class Actu extends Fragment {
private DatabaseReference db;
FirebaseHelper helper;
QuestionAdapter adapter;
ListView lv;
View v;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_actu, container, false);
lv = (ListView) v.findViewById(R.id.actu_rv);
progressBar = (ProgressBar) v.findViewById(R.id.progress_bar);
no_connection =(TextView) v.findViewById(R.id.no_connection);
db = FirebaseDatabase.getInstance().getReference("Questions");
db.limitToLast(100).orderByKey();
helper = new FirebaseHelper(db);
adapter = new QuestionAdapter(getContext(),helper.retreive());
lv.setAdapter(adapter);
return v;
}
EDIT:
Please Help me.
update
public QuestionAdapter(Context c, ArrayList<Questions> questionEntries) {
this.c = c;
this.questionEntries = questionEntries;
}
#Override
public int getCount() {
return questionEntries.size();
}
#Override
public Object getItem(int position) {
return questionEntries.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View contentView, ViewGroup parent) {
if (contentView==null){
contentView= LayoutInflater.from(c).inflate(R.layout.actu_list_item,parent,false);
}
TextView asker = (TextView) contentView.findViewById(R.id.asker_name);
TextView date = (TextView) contentView.findViewById(R.id.date);
TextView title = (TextView) contentView.findViewById(R.id.question_title);
TextView content = (TextView) contentView.findViewById(R.id.question_content);
Button answer = (Button) contentView.findViewById(R.id.answer);
final EditText answerET= (EditText) contentView.findViewById(R.id.answer_edittext);
posit = this.getItem(position);
answer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
answer_text = answerET.getText().toString();
if(answer_text.length()<20){
Toast.makeText(c,"Vous devez entrer au moins 20 cgharactères",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(c,"Post en cours...",Toast.LENGTH_LONG).show();
Answer answerClass = new Answer();
FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseAuth firebaseAuth1 = FirebaseAuth.getInstance();
FirebaseUser firebaseUser= firebaseAuth1.getCurrentUser();
qe= (Questions) posit;
SimpleDateFormat dateFormat=new SimpleDateFormat("HH:mm dd-MM-yyyy");
String date= dateFormat.format(new Date(System.currentTimeMillis()));
assert firebaseUser !=null;
DatabaseReference Asker = database.getReference(firebaseUser.getUid()+"/name");
Asker.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
name = dataSnapshot.getValue(String.class);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
answerClass.setAnswer(answer_text);
answerClass.setUsername(name);
answerClass.setDate(date);
DatabaseReference AnswerRef =database.getReference("Questions/"+qe.getTag_id()+"/");
AnswerRef.push().setValue(answerClass).addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(c,"Reponse postée",Toast.LENGTH_LONG).show();
}
});
}
}
});
qe= (Questions) this.getItem(position);
//There is the error
// It's look like qe is empty
asker.setText(qe.getQuestion_username());
date.setText(qe.getQuestion_date());
title.setText(String.format("Q: %s", qe.getQuestion_title()));
content.setText(qe.getQuestion_content());
contentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(c,qe.getQuestion_username(),Toast.LENGTH_LONG).show();
}
});
this.notifyDataSetChanged();
return contentView;
}

You cannot return questionEntries ArrayList in the way you do. By the time you are using this line of code:
return questionEntries;
Firebase did not finished getting the data from your database. This is happening because of the asyncronious behaviour of onChildAdded() method. So to solve this, please use the following code:
db.orderByChild("question_date").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ArrayList<Questions> questionEntries = new ArrayList<>();
Questions questions = dataSnapshot.getValue(Questions.class);
questionEntries.add(questions);
Log.d("TAG", questionEntries);
}
#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) {}
});
Edit1: Another approach will be to use ValueEventListener like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference db = rootRef.child("Question");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<Questions> questionEntries = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Questions questions = ds.getValue(Questions.class);
questionEntries.add(questions);
}
Log.d("TAG", questionEntries);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
db.addListenerForSingleValueEvent(eventListener);
Edit2: Assuming that Question node is a direct child of your Firebase database root, another approach will be this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference db = rootRef.child("Question");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> questionEntries = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String question_title = ds.child("question_title").getValue(String.class);
questionEntries.add(question_title);
}
Log.d("TAG", questionEntries);
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
db.addListenerForSingleValueEvent(eventListener);

The problem appears to be with your questionEntries list.

Related

OnChild event Listener is being called multiple times

I am currently using Value Event Listener in my Code to show messages and it does not shows any error but my only problem is when whenever i add a new send or receive a new message my whole list gets updated all the previous messages are called and it uses a lot of memory currently i am taking care of it my clearing recyclerview every time the value event listener is called but now i want to add Child Event Listener in place of value event listener to save memory but whenever i add child event listener my messages are shown multiple times in recyclerview but after i restart the activity there is only one message in recyclerview.
MyCode
MessageActivity.java
public class MessageActivity extends AppCompatActivity {
CircleImageView profile_image;
TextView user_name;
FirebaseFirestore fstore;
FirebaseAuth fAuth;
FirebaseUser fuser;
DatabaseReference reference;
Toolbar toolbar;
ImageButton imageButton;
EditText textsend;
String userID;
MessageAdapter messageAdapter;
List<Chat> mChat;
RecyclerView recyclerView;
String TAG = "MyTag";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
fAuth=FirebaseAuth.getInstance();
fstore=FirebaseFirestore.getInstance();
userID = fAuth.getCurrentUser().getUid();
profile_image=findViewById(R.id.profile_image);
user_name=findViewById(R.id.username);
imageButton=findViewById(R.id.btn_send);
textsend=findViewById(R.id.text_send);
recyclerView=findViewById(R.id.recycler_view2);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager= new LinearLayoutManager(getApplicationContext());
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
toolbar=findViewById(R.id.myToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Intent intent=getIntent();
fuser=FirebaseAuth.getInstance().getCurrentUser();
String userid=intent.getStringExtra("userid");
String username= intent.getStringExtra("UserName");
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
user_name.setText(user.getFirst());
if (user.getImageURL().equals("default")){
profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
//and this
Glide.with(getApplicationContext()).load(user.getImageURL()).into(profile_image);
}
readMessages(fuser.getUid(), userid, user.getImageURL());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg=textsend.getText().toString();
textsend.setText("");
if (!msg.equals("")){
sendMessage(fuser.getUid(),userid,msg);
}
else {
Toast.makeText(MessageActivity.this,"Empty messages cant be send",Toast.LENGTH_SHORT).show();
}
}
});
}
private void sendMessage(String sender, final String receiver, String message) {
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", sender);
hashMap.put("receiver", receiver);
hashMap.put("message", message);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
String key = reference.push().getKey();
reference.child(key).setValue(hashMap);
}
private void readMessages(String myid,String userid,String imageurl){
mChat=new ArrayList<>();
reference=FirebaseDatabase.getInstance().getReference("Chats");
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Chat chat= dataSnapshot.getValue(Chat.class);
assert chat!=null;
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid)||
chat.getReceiver().equals(userid)&&chat.getSender().equals(myid)){
chat.setMessageId(dataSnapshot.getKey());
mChat.add(chat);
Log.d(TAG,"msg "+chat.getMessage());
}
messageAdapter=new MessageAdapter(MessageActivity.this,mChat, imageurl);
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
Chat chat=dataSnapshot.getValue(Chat.class);
chat.setMessageId(dataSnapshot.getKey());
mChat.remove(chat);
messageAdapter.notifyDataSetChanged();
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
/*
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
mChat.clear();
for (DataSnapshot snapshot:dataSnapshot.getChildren()) {
Chat chat=snapshot.getValue(Chat.class);
assert chat != null;
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid)||
chat.getReceiver().equals(userid)&&chat.getSender().equals(myid)){
mChat.add(chat);
Log.d(TAG,"msg "+chat.getMessage());
}
messageAdapter=new MessageAdapter(MessageActivity.this,mChat, imageurl);
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
*/
}
}
MessageAdapter.java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {
public static final int MSG_TYPE_LEFT = 0;
public static final int MSG_TYPE_RIGHT = 1;
private Context mContext;
private List<Chat> mChat;
String imageurl;
FirebaseUser fuser;
public MessageAdapter(Context mContext, List<Chat> mChat,String imageurl) {
this.mContext = mContext;
this.mChat = mChat;
this.imageurl=imageurl;
}
#NonNull
#Override
public MessageAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == MSG_TYPE_RIGHT) {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_right, parent, false);
return new MessageAdapter.ViewHolder(view);
}
else {
View view = LayoutInflater.from(mContext).inflate(R.layout.chat_item_left, parent, false);
return new MessageAdapter.ViewHolder(view);
}
}
#Override
public void onBindViewHolder(#NonNull MessageAdapter.ViewHolder holder, int position) {
Chat chat=mChat.get(position);
holder.show_message.setText(chat.getMessage());
if (imageurl.equals("default")){
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(imageurl).into(holder.profile_image);
}
holder.show_message.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
String message_id=chat.getMessageId();
Task<Void> task = Utils.removeUser(message_id);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(mContext,"Message Deleted",Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
}
#Override
public int getItemCount() {
return mChat.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView show_message;
public ImageView profile_image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
show_message=itemView.findViewById(R.id.show_message);
profile_image=itemView.findViewById(R.id.profile_image);
}
}
#Override
public int getItemViewType(int position) {
fuser= FirebaseAuth.getInstance().getCurrentUser();
if (mChat.get(position).getSender().equals(fuser.getUid())){
return MSG_TYPE_RIGHT;
}
else {
return MSG_TYPE_LEFT;
}
}
}
Use below code. Remove ValueEventListener calls. You should add your logic inside onChildAdded
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
Chat chat= dataSnapshot.getValue(Chat.class);
assert chat!=null;
if (chat.getReceiver().equals(myid) && chat.getSender().equals(userid)||
chat.getReceiver().equals(userid)&&chat.getSender().equals(myid)){
mChat.add(chat);
Log.d(TAG,"msg "+chat.getMessage());
}
messageAdapter=new MessageAdapter(MessageActivity.this,mChat, imageurl);
recyclerView.setAdapter(messageAdapter);
messageAdapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How to get data from Firebase and display it in a recyclerview? [duplicate]

This question already has answers here:
How to update RecyclerView Adapter Data
(16 answers)
Closed 3 years ago.
I develop a chat app. I try to get data from firebase database and display it in a recyclerview, but it doesn't work. I searched it on Google and I tried a lot of options, but it displays nothing. What am I doing wrong?
These are the activities:
1.Main Activity(Where I want to display the data)
public class SearchActivity extends AppCompatActivity {
Toolbar sToolbar;
SearchView searchView;
RecyclerView recyclerView;
BottomNavigationView sBnv;
SearchAdapter searchAdapter;
LinearLayoutManager llm;
ArrayList<String> arrayList;
User user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
sToolbar = findViewById(R.id.sToolbar);
recyclerView = findViewById(R.id.sRecyclerView);
sBnv = findViewById(R.id.sBnv);
searchAdapter = new SearchAdapter(arrayList, getApplicationContext());
recyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(getApplicationContext());
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this, "Database error", Toast.LENGTH_SHORT)
.show();
}
});
2.Adapter class:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
ArrayList<String> friendsSearchedList = new ArrayList<>();
Context mContext;
public SearchAdapter(ArrayList<String> friendsSearchedList, Context mContext){
this.friendsSearchedList = friendsSearchedList;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_user_item,parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final String username = friendsSearchedList.get(position);
holder.text.setText(username);
}
#Override
public int getItemCount() {
return friendsSearchedList == null ? 0:friendsSearchedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text, text1, text2;
Button button;
RelativeLayout relativeLayout;
public ViewHolder(View itemView){
super(itemView);
text = itemView.findViewById(R.id.sText);
button = itemView.findViewById(R.id.addFriend);
text1 = itemView.findViewById(R.id.sText1);
text2 = itemView.findViewById(R.id.sText2);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
3.User class:
public class User {
public String email, id, password;
public String username;
public User(String email, String username, String password){
this.email = email;
this.username = username;
this.password = password;
}
public User(){}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
Initially the arraylist which your setting for the recyclerview is empty. When you are adding the data to that list you have to notify the recycler view adapter. So your code should look like this way:
searchAdapter = new SearchAdapter(arrayList, getApplicationContext());
// remove this line
/* recyclerView.setHasFixedSize(true);*/
llm = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
***searchAdapter.notifyDataSetChanged();***
}
}
If it’s a simple chat maybe a listview would be more convenient for you.
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.add(value);
}
#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) {
}
});
This should easily do the trick…
Create a method in your SearchAdapter.class like below
public void addData(ArrayList<String> list){
this.mDataSet = list;
notifyDataSetChanged();
}
And once you receive data from Firebase database, update it to the adapter like below.You need to tell the recycler view that your data set has been updated.
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
searchAdapter.addData(arrayList); // Add this line
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this, "Database error", Toast.LENGTH_SHORT)
.show();
}
});

How to update a List view in real-time Firebase - Android

I have been having some trouble updating my Friends list(mListView) view in real time in the application, it only seems to update when it is launched. I know its a matter of an update function but i do not know what to use.
(I'm rather new to firebase and android, and i'm still learning about them.)
Here is the code:
private ListView mListView;
private ArrayList<String> mFriends;
private List<User> mUserList;
private UserListAdapter mAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_friends, container, false);
mSearchParam = (EditText) view.findViewById(R.id.searchFriends);
mListView = (ListView) view.findViewById(R.id.lvProfileFriends);
mPhotos = new ArrayList<>();
mUserList = new ArrayList<>();
mFriends = new ArrayList<>();
DisplayFriends();
return view;
}
private void DisplayFriends(){
getFriends();
}
private void updateUsersList(){
Log.d(TAG, "updateUsersList: updating users list");
mAdapter = new UserListAdapter(getActivity(), R.layout.layout_user_listitem, mUserList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick: selected user: " + mUserList.get(position).toString());
//navigate to profile activity
Intent intent = new Intent(getActivity(), ViewProfileFragment.class);
intent.putExtra(getString(R.string.calling_activity), getString(R.string.fragment_profile));
intent.putExtra(getString(R.string.intent_user), mUserList.get(position));
startActivity(intent);
}
});
}
private void updateFriendsList(){
Log.d(TAG, "getPhotos: getting Friend(s)");
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
for(int i = 0; i < mFriends.size(); i++){
final int count = i;
Query query = reference
.child(getString(R.string.dbname_users))
.orderByChild(getString(R.string.field_user_id))
.equalTo(mFriends.get(i));
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
mUserList.add(singleSnapshot.getValue(User.class));
}
if(count >= mFriends.size() - 1){
updateUsersList();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
private void getFriends(){
Log.d(TAG, "getFollowing: searching for FRIENDS **************************************");
mFriends.clear();
mUserList.clear();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_friends))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange: found friend----------------**: " + singleSnapshot.getValue(User.class).toString());
mFriends.add(singleSnapshot.child(getString(R.string.field_user_id)).getValue().toString());
Log.d(TAG, "onDataChange: Freinds user id: " + mFriends.toString() );
}
updateFriendsList();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Have a look at this:
DatabaseReference dref;
ListView listview;
ArrayList<String> list=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list);
listview.setAdapter(adapter);
dref=FirebaseDatabase.getInstance().getReference();
dref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
list.add(dataSnapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
list.remove(dataSnapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Whenever you receive a firebase update on friendList you can call adapter.notifyDataSetChanged() on the adapter that way adapter would know that an update has to be performed.
I would suggest you to use RecyclerView for this purpose as it is more flexible than a listview.

recyclerview dosen't appear when I type in edittext and appears when I hide keyboard in android studio

I have an activity for searching in firebase, it contains edit text for typing the text and button when I press the button The results supposed to appear in the recyclerview but when I press the button the first time nothing appears unless I hide the keyboard and when I press the second time nothing appears unless the keyboard shows. Is there a solution for this problem?
Code of activity
public class FindDataActivity extends AppCompatActivity {
DatabaseReference db;
FirebaseHelper helper;
MyAdapter adapter;
RecyclerView rv;
EditText findETxt;
Button findBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_data);
//SETUP RECYCLER
rv = (RecyclerView) findViewById(R.id.rvsearch);
rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
//INITIALIZE FIREBASE DB
db = FirebaseDatabase.getInstance().getReference("Database");
findETxt = (EditText) findViewById(R.id.findETxt);
findBtn = (Button) findViewById(R.id.find_btn);
}
public void search(View view) {
helper = new FirebaseHelper(db);
final String s = findETxt.getText().toString();
//ADAPTER
adapter = new MyAdapter(FindDataActivity.this, helper.retrieveSearch(s));
db.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
This is code of firebase
public class FirebaseHelper {
DatabaseReference db;
Boolean saved=null;
Boolean saved1=null;
ArrayList<Paints> paints=new ArrayList<>();
ArrayList<String> keys=new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//WRITE IF NOT NULL
public Boolean save(Paints paint)
{
if(paint==null)
{
saved=false;
}else
{
try
{
db.child("items").push().setValue(paint);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
// IN STATE OF UPDATE
public Boolean save1(Paints paint,String k)
{
if(paint==null)
{
saved1=false;
}else
{
try
{
db.child("items").child(k).setValue(paint);
saved1=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved1=false;
}
}
return saved1;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
paints.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Paints spacecraft=ds.getValue(Paints.class);
paints.add(spacecraft);
}
}
//READ THEN RETURN ARRAYLIST
public ArrayList<Paints> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return paints;
}
//READ THEN RETURN ARRAYLIST OF KEYS IN CASE OF ALL DATA
public ArrayList<String> retrieve1() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchDataKeys(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchDataKeys(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return keys;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST WITH ALL KEYS
private void fetchDataKeys(DataSnapshot dataSnapshot)
{
keys.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
keys.add(ds.getKey());
}
}
//READ THEN RETURN ARRAYLIST FOR SEARCH ITEMS IN CASE OF SEARCHING ABOUT ITEM
public ArrayList<Paints> retrieveSearch(final String item) {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchDataSearch(dataSnapshot,item);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchDataSearch(dataSnapshot,item);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return paints;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST WITH ALL PAINTS
private void fetchDataSearch( DataSnapshot dataSnapshot,String item)
{
paints.clear();
//sorting and searching
final DatabaseReference myRef = db.child("items");
Query query = myRef.orderByChild("type").startAt(item)
.endAt(item + "\uf8ff");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
Paints paint=messageSnapshot.getValue(Paints.class);
paints.add(paint);
//Toast.makeText(FindDataActivity.this, "found " + type + " unit: " + unit + " price " + price, Toast.LENGTH_LONG).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
//READ THEN RETURN ARRAYLIST OF KEYS IN CASE OF SEARCH ITEM
public ArrayList<String> retrieve1Search(final String item) {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchDataKeysSearch(dataSnapshot,item);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchDataKeysSearch(dataSnapshot,item);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return keys;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST WITH KEYS IN STATE OF SEARCH
private void fetchDataKeysSearch(DataSnapshot dataSnapshot,String item)
{
keys.clear();
//sorting and searching
final DatabaseReference myRef = db.child("items");
Query query = myRef.orderByChild("type").startAt(item)
.endAt(item + "\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren())
{
keys.add(messageSnapshot.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
code for Adapter
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Paints> paints=new ArrayList<Paints>();
ArrayList<String> keys = new ArrayList<String>();
public MyAdapter(Context c, ArrayList<Paints> paints) {
this.c = c;
this.paints = paints;
//this.keys=keys;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=LayoutInflater.from(c).inflate(R.layout.model,parent,false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Paints paint=paints.get(position);
//final String ss=keys.get(position);
holder.typeTxt.setText(paint.getType());
holder.unitTxt.setText(paint.getUnit());
holder.priceTxt.setText(String.valueOf(paint.getPrice()));
holder.setItemClickListener(new ItemClickListener() {
#Override
public void onItemClick(int pos) {
//OPEN DETAI ACTIVITY
openDetailActivity(paint.getType(),paint.getUnit(),paint.getPrice());
}
});
}
#Override
public int getItemCount() {
return paints.size();
}
//OPEN DETAIL ACTIVITY
private void openDetailActivity(String type,String unit, Double price)
{
Intent i=new Intent(c,DetailActivity.class);
i.putExtra("TYPE_KEY",type);
i.putExtra("UNIT_KEY",unit);
i.putExtra("PRICE_KEY", price);
//i.putExtra("K_KEY",s);
c.startActivity(i);
}
}
code for viewholder
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView typeTxt,unitTxt,priceTxt;
ItemClickListener itemClickListener;
public MyViewHolder(View itemView) {
super(itemView);
typeTxt= (TextView) itemView.findViewById(R.id.typeTxt);
unitTxt= (TextView) itemView.findViewById(R.id.unitTxt);
priceTxt= (TextView) itemView.findViewById(R.id.priceTxt);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener=itemClickListener;
}
#Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}
}

Android - Update and delete data in Firebase database

I'm trying to update and delete data in Firebase database.
SectionDetails model
public class SectionDetails {
private String sectionCode;
private String sectionSeats;
private String sectionKey;
public SectionDetails() {
}
public SectionDetails(String sectionCode, String sectionSeats) {
this.sectionCode = sectionCode;
this.sectionSeats = sectionSeats;
}
#Exclude
public String getSectionKey() {
return sectionKey;
}
public String getSectionCode() {
return sectionCode;
}
public String getSectionSeats() {
return sectionSeats;
}
}
FirebaseHelper class
public class FirebaseHelper {
DatabaseReference db;
ArrayList<SectionDetails> sectionDetailsArrayList = new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
private void fetchData(DataSnapshot dataSnapshot) {
SectionDetails sectionDetails = dataSnapshot.getValue(SectionDetails.class);
sectionDetailsArrayList.add(sectionDetails);
adapter.notifyDataSetChanged();
}
public ArrayList<SectionDetails> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
adapter.notifyDataSetChanged();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return sectionDetailsArrayList;
}
}
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
DatabaseReference updateRef;
String key;
Context c;
ArrayList<SectionDetails> sectionDetailsArrayList;
public CustomAdapter(Context c, ArrayList<SectionDetails> sectionDetailsArrayList) {
this.c = c;
this.sectionDetailsArrayList = sectionDetailsArrayList;
}
#Override
public int getCount() {
return sectionDetailsArrayList.size();
}
#Override
public Object getItem(int position) {
return sectionDetailsArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final SectionDetails sd = (SectionDetails) this.getItem(position);
updateRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(CustomAdapter.this);
d.setContentView(R.layout.section_custom_dialog);
Button btnUpdate = (Button) d.findViewById(R.id.btnUpdate);
Button btnDelete = (Button) d.findViewById(R.id.btnDelete);
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String code = "1B";
final String seats = "20";
updateRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
SectionDetails updateSD = snapshot.getValue(SectionDetails.class);
if (sd.getSectionCode().equals(updateSD.getSectionCode())) {
key = snapshot.getKey().toString();
}
}
SectionDetails newSD = new SectionDetails(code, seats);
updateRef.child(key).setValue(newSD);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
SectionDetails deleteSD = snapshot.getValue(SectionDetails.class);
if (sd.getSectionCode().equals(deleteSD.getSectionCode())) {
updateSectionRef.child(snapshot.getKey().toString()).removeValue();
break;
}
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
d.show();
}
});
return convertView;
}
}
MainActivity class
public class MainActivity extends AppCompatActivity {
DatabaseReference mRef;
FirebaseHelper helper;
CustomAdapter adapter;
ListView lvSectionsListOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvSectionsListOne = (ListView) findViewById(R.id.lvSectionsList);
mRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
helper = new FirebaseHelper(mRef);
adapter = new CustomAdapter(this, helper.retrieve());
lvSectionsListOne.setAdapter(adapter);
}
}
The data is deleted from database as expected, but the data that gets deleted remains inside the listview. I added adapter.notifyDataSetChanged() but still the listview is not updating.
The data is also updated as expected, but when update button is clicked, the data is updated infinitely. I can see the listview as well as the database keep on appending the data, and can only be stopped by closing the app.
I have tried to move SectionDetails newSD = new SectionDetails(code, seats) and updateRef.child(key).setValue(newSD) to outside for loop but the data doesn't get updated because the key is not passed to the path outside the for loop.
I haven't thoroughly looked at all the code you posted and may not understand your processing completely. There are two things that might be causing some of the problems you described.
In FirebaseHelper, method onChildChanged() calls fetchData(), which adds the changed section details to the array list. Shouldn't you be updating the existing section details instead of adding them again? Also, in onChildRemoved(), the section details are not removed from the array list. Don't they need to be removed?
In CustomAdapter the click listeners for your buttons add anonymous ValueEventListeners. Because they are anonymous, you have no way of removing them when they are no longer needed. ValueEventListeners added with addValueEventListener() remain active until removed. If your goal is to get the data once, use addListenerForSingleValueEvent().

Categories

Resources