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.
Related
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!!
Working on an Android Project using Firebase.
The project is to let users check in and display all logs in a table view.
I have set up the firebase database as follows:
I can't get the entries to show in the table view?
I would like to show the following items per list item:
userCheckIn
username
userLocation
My code:
private ListView mListView;
private DatabaseReference mDatabse;
private ArrayList<String> arrayList = new ArrayList<>();
private ArrayAdapter<String> adapter;
String Date;
Calendar calendar;
SimpleDateFormat simpleDateFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account);
mListView = (ListView) findViewById(R.id.database_list_view);
calendar = Calendar.getInstance();
simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date = simpleDateFormat.format(calendar.getTime());
FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser() ;
mDatabse = FirebaseDatabase.getInstance().getReference().child("CheckInOut").child(currentFirebaseUser.getUid()).child(Date).child("userCheckIn");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
mListView.setAdapter(adapter);
mDatabse.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);
arrayList.add(string);
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) {
}
});
}
I would appreciate any support possible.
To solve this, move the declaration of your ArrayList and ArrayAdapter inside onChildAdded() method. You need also to set the adapter inside onChildAdded() method because this method has an asynchronous behaviour. This means that if you are trying to set the adapter outside onChildAdded() method it will always be empty because at that time you haven't got all the data from the database. So all operation must take place inside that method.
I recomand you also to use FirebaseUI. It will handle all the events for you. If want to read more about how you can get the data outside an asynchronous method, take a look at my answer from this post.
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();
}
...
Im struggling a little with my code. I have searched and tried many methods. My aim is remove the previous entry that Firebase has provided to my app, the firebase data is coming from an embedded device.I want to replace the value in my android app.
Firebase is working ok, The app will display the new value provided by firebase but will not delete the previous entry.
I still have plenty to do but want to fix the basics before moving on.
So basically the OnChildChanged will display new firebase entries, i just want the previous entry deleted from the Android Application when a new value comes in from Firebase.
I have looked into removeValue and setValue with no avail
public class MainActivity extends AppCompatActivity {
DatabaseReference fDatabase;
ListView listview;
ArrayList<String> list = new ArrayList<>();
ArrayAdapter <String> adapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
listview.setAdapter(adapter);
fDatabase = FirebaseDatabase.getInstance().getReference();
fDatabase.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.remove(value);
adapter.notifyDataSetChanged();
list.add(value);
adapter.notifyDataSetChanged();
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.remove(value);
adapter.notifyDataSetChanged();
list.add(value);
adapter.notifyDataSetChanged();
}
Looks like when you assign value it's getting the new entry, so when you call list.remove it isn't removing the old entry since it's actually trying to remove any instance of the new entry.
See if there's any way you can map the old entry to the new entry so the list knows what to remove since the content of value is different between old and new.
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);
}