How to get all childs data on ListView - Firebase database - android

i need to get all data in Childs and show it in ListView
Every Values in Childs!,The main Child is "XXX-Users" and other Childs is about "Model Phones",, i want get All Date of This Childs on ListView
this is my Activity:
public class xxxxxxx extends AppCompatActivity {ListView listView;
ArrayList<String> usernames = new ArrayList<>() ;
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("xxx-Users");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxxxxxxxxxxxx);
listView = (ListView) findViewById(R.id.listview00);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,usernames);
listView.setAdapter(arrayAdapter);
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
usernames.add(value);
arrayAdapter.notifyDataSetChanged();
}
#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) {
}
});
}`**
My Layout
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/listview00" />

As cricket_007 said, your code doesn't match your data structure. Your listener will fire for the nodes immediately under xxx-Users. But your actual URL values are one level deeper.
One simple way to handler this is to loop over those nested children in onChildAdded():
DatabaseReference myRef = database.getReference("xxx-Users");
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String value = childSnapshot.getValue(String.class);
usernames.add(value);
}
arrayAdapter.notifyDataSetChanged();
}
...

Related

Retrieved Data from Firebasedatabase and set into recyclierview Adapter

I want to read the data from firebasedatabase and show that into recyclerview retrieve data form FirebaseDatabase by orderbychild method. I want to read data of that child who's email match with my useremail who is currently login from app .
This is my Firebase:
I have successfully completed the first part, but I am facing problems with second part how can I pass the retrieved data to recyclerview adapter.
For example whenever user login from mobeen#gmail.com I read his location and ticket_no and show that data into recyclerview. I am facing problem with this line
programinglist.setAdapter(new myadapter(this, modl1));
where should I pass my modl1 (ArrayList<model> modl1) to recyclerview.setadapter
public class recyclierview extends AppCompatActivity {
public RecyclerView programinglist;
DatabaseReference b1, b2;
public ArrayList<model> modl1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclierview);
programinglist = findViewById(R.id.my_recycler_view);
programinglist.setLayoutManager(new LinearLayoutManager(this));
modl1 =new ArrayList<model>();
readdata(new firebasecallback() {
#Override
public void oncallback(ArrayList<model> list) {
Log.v("aliraza" , "value oncalback" + list.get(0).getTicket_no());
}
});
}
private void readdata(final firebasecallback firebasecallback)
{
b1= FirebaseDatabase.getInstance().getReference();
b2=b1.child("tasks");
b2.orderByChild("email").equalTo(MainActivity.email).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
model m = new model();
String location = dataSnapshot.child("location").getValue().toString();
String tickeno = dataSnapshot.child("ticket_no").getValue().toString();
Log.v("ALI", "family value " +location);
Log.v("ALI", "family value " + tickeno);
m.setLocation(location);
Log.v("ALI", "location " +m.getLocation());
m.setTicket_no(tickeno);
modl1.add(m);
Log.v("ALI", "family value " +modl1.get(0).getTicket_no());
firebasecallback.oncallback(modl1);
}
#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) {
}
});
programinglist.setAdapter(new myadapter(this, modl1));// where should i call this setadapter function of recyclierview.enter code here
}
private interface firebasecallback
{
void oncallback( ArrayList<model> list);
}
}
Initialise your arraylist and adapter;
public ArrayList<model> modl1=new ArrayList();
private myadapter adapter;
In onCreate() method initialize your adapter with blank list and set adapter to recyclerview.
adapter=new myadapter(this, modl1);
programinglist.setAdapter(adapter);
then create a method addItem() in your adapter.
public void addItem(model item){
list.add(item); // add item to your adapter list
notifyItemInserted(list.size());
}
changes in onChildAdded() method
modl1.add(m); //remove this line
firebasecallback.oncallback(m);
readdata(new firebasecallback() {
#Override
public void oncallback(model m) {
modl1.add(m);
adapter.add(m);
Log.v("aliraza" , "value oncalback" + list.get(0).getTicket_no());
}
});
You can remove firebasecallback method,as there is no need of it, and directly add item to adapter in onChildAdded() method. Simply call readdata() method from onCreate()
private void readdata()
{
b1= FirebaseDatabase.getInstance().getReference();
b2=b1.child("tasks");
b2.orderByChild("email").equalTo(MainActivity.email).addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
model m = new model();
String location = dataSnapshot.child("location").getValue().toString();
String tickeno = dataSnapshot.child("ticket_no").getValue().toString();
Log.v("ALI", "family value " +location);
Log.v("ALI", "family value " + tickeno);
m.setLocation(location);
Log.v("ALI", "location " +m.getLocation());
m.setTicket_no(tickeno);
modl1.add(m);
Log.v("ALI", "family value " +modl1.get(0).getTicket_no());
adapter.add(m);
}
#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) {
}
});
}
Hope this will help!!

Android With Listview and firebase

Is there any mistake in code because I am getting the only price in the listview and not the name can anyone help me out? I've tried so many methods to solve it but in last it let me post on StackOverflow to ask you people for the help, I am really stuck in it. I'll be really thankful to you for this help. Model class will also be provided if needed.
private ListView mlistView;
private ArrayList<String>list;
private ArrayAdapter<String> adapter;
private ArrayList<String>Pricelist;
private ArrayAdapter<String> Priceadapter;
private DatabaseReference ref;
private FirebaseDatabase database;
private FoodItemclass fooditem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rice_after_login);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBar.setTitle("Rice");
fooditem = new FoodItemclass();
mlistView = (ListView)findViewById(R.id.listview);
database = FirebaseDatabase.getInstance();
ref = database.getReference("Rice");
list = new ArrayList<>();
Pricelist = new ArrayList<>();
adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemNameIds,list);
Priceadapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemPriceId,Pricelist);
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
fooditem = dataSnapshot.getValue(FoodItemclass.class);
list.add(fooditem.getName());
Pricelist.add(String.valueOf(fooditem.getPrice()));
mlistView.setAdapter(adapter);
mlistView.setAdapter(Priceadapter);
}
#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) {
}
});
you can only set one adapter to your list view so thats why you get the recent assign adapter result
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
fooditem = dataSnapshot.getValue(FoodItemclass.class);
list.add(fooditem.getName()+ " "+String.valueOf(fooditem.getPrice());
mlistView.setAdapter(adapter);
}
NOTE: the space between both name and price value is a separator
Recommend This is a bad approach to do so. you should use RecyclerView to show multiple values
The answer from faiizii has the gist of the problem: you can only have one adapter on a list view, so you should only call setAdapter once. Since onChildAdded will be called for each child, you shouldn't call setAdapter in there (nor in any of the other onChild... methods).
The solution is to move the calls to setAdapter out of the listener:
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this,R.layout.list_item,R.id.ItemNameIds,list);
mlistView.setAdapter(adapter);
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
fooditem = dataSnapshot.getValue(FoodItemclass.class);
list.add(fooditem.getName()+": "+fooditem.getPrice());
adapter.notifyDataSetChanged();
}
...
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
As you'll see we now call notifyDataSetChanged() inside onChildAdded. This lets the adapter know that its data has changed, which in turn then means that it refreshes the list view.

ArrayList loses elements after onChildAdded (Android Studio & Firebase)

public void readUniversity(){
ChildEventListener uniListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//Log.d(LOG_TAG, dataSnapshot.getKey());
University uni = dataSnapshot.getValue(University.class);
universitieslist.add(uni); //Adding to an ArrayList of type University
//Log.d(LOG_TAG, universitieslist.size()+"is the size");
arrayofUniversities.add(uni.getName()); //Adding name of University to list
//Log.d(LOG_TAG, adapter.getCount() + " items");
//Log.d(LOG_TAG, arrayofUniversities.size()+" is the size");
adapter.notifyDataSetChanged();
}
#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) {
}
};
DatabaseReference uniReference = database.getReference("note-retrieve/Universities");
uniReference.addChildEventListener(uniListener);
}
The code is pretty much self explanatory. I am using arrayofUniversities a list to populate a spinner so I need an adapter. I am populating the spinner with the names of the Universities
I am storing the University Objects in universitieslist
The problem I have is when I leave the readUniversity() method I lose every element in universitieslist and arrayofUniversities even though my spinner is populated with these.
Its very strange as the list should have the objects stored.
Also, I have my adapter at the very top of the code in onCreate().
Try this way. Hope it will work .. :)
private Arrylist<University > universityList = new ArryList<>();
private Arrylist<String> universitysName = new ArryList<>();
mDatabase.getReference().child("path")
.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
University mUniversity = dataSnapshot.getValue(University
.class);
universityList.add(mUniversity);
universitysName.add(mUniversity.getName);
}
}
#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) {
}
});
The problem seems to be with the adapter, if you can see the logs but the adapter is not updating, then is because the adapter is not being notified the data set has changed
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null) {
University mUniversity = dataSnapshot.getValue(University
.class);
universityList.add(mUniversity);
universitysName.add(mUniversity.getName);
//UPDATE YOUR ADAPTER HERE
//spinnerAdapter.notifyDataSetChanged();
}
}
But I think the real problem is the origin of your current problem: You are getting each child. This is a way to do it, but for this case where you need all the data first and when that is ready show the spinner with the elements to the user, you should better get all the list elements in one query.
database.getReference("note-retrieve/Universities")..addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
University mUniversity = dataSnapshot.getValue(University
.class);
universityList.add(mUniversity);
universitysName.add(mUniversity.getName);
//ONCE THIS IS DONE, THEN PASS THE ARRAY TO THE ADAPTER
}
//The loop here is over, so you can set the adapter with the full list of data/universities
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});

How to create simple_list_item_2 List with Firebase

I can create simple_dropdown_item_1line, but I need to create 2 line list
ArrayList<String> list = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
ListView courses;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
FirebaseUser user = firebaseAuth.getCurrentUser();
final DatabaseReference database = databaseReference.child(user.getUid());
.
.
.
courses = (ListView)findViewById(R.id.courseList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
courses.setAdapter(arrayAdapter);
database.child("Lessons").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
list.add(dataSnapshot.getKey());
arrayAdapter.notifyDataSetChanged();
}
#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) {
}
});
I create simple_dropdown_item_1line listView, and Everything works. But I can not create simple_dropdown_item_1line

How to load Firebase query into ArrayList<> to pass to adapter

I have a Firebase query to show into a recyclerView, but I dont know whichValueListener` is supposed to be used for this purpose.
in order to populate my RecyclerView Adapter i need to pass an ArrayList , so how to do so ? how to add items from query into the ArrayList ?
I do not want to use FirebaseRecyclerAdapter because it is much more limted than RecyclerView.Adapter (does not meet my needs).
Thanks
FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
DatabaseReference myRef = mDatabase.getReference("imageData");//image data is my json array List
myRef.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//this will be called for every child in your list
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//this will be called for every child changed in your list
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Just query the values and get it from firebase and use a custom recycler view to populate the queried data...Then you may be looking for this..
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
recyclerList.clear();
recyclerAdapter = new RecyclerAdapter(recyclerList);
RecyclerAdapter.notifyDataSetChanged();
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
BTIsdDetails post = postSnapshot.getValue(BTIsdDetails.class);
BTIsdDetails btIsdDetails = new BTIsdDetails(post.getName(), post.getId());
isdList.add(btIsdDetails);
BTLog.d(TAG, " -------------------------- " + post.getName());
}
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(isdAdapter);
}

Categories

Resources