I tried inputing the data of Income but it only shows 1 data and its the newest. Why the other data not showing on the graph?
and this is what my code is like
gIncomeDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
int index = 0;
for (DataSnapshot mysnapshot : snapshot.getChildren()) {
Data data = mysnapshot.getValue(Data.class);
//String date = data.getDate();
//month = data.getDate();
ArrayList<BarEntry> values1 = new ArrayList<>();
//for (int i = 0)
values1.add(new BarEntry(1f, data.getAmount()));
BarDataSet dataSet = new BarDataSet(values1, "Amount");
BarData barData = new BarData(dataSet);
barinc.setData(barData);
barinc.invalidate();
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
How can I put all data on graph?
Related
my data stored in arraylist only if all of the data match "if condition".
suppose any data that match should be stored in arraylist.
searchDB = FirebaseDatabase.getInstance().getReference("Users");
geofenceDB = FirebaseDatabase.getInstance().getReference("Geo-fence");
searchList.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
searchList.setLayoutManager(layoutManager);
String userID = FirebaseAuth.getInstance().getUid();
geofenceDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
final double userLat = snapshot.child(userID).child("Latitude").getValue(Double.class);
final double userLng = snapshot.child(userID).child("Longitude").getValue(Double.class);
final double userRadius = snapshot.child(userID).child("Radius").getValue(Double.class);
for (DataSnapshot tutorSnapshot : snapshot.getChildren()){
String fetchID = tutorSnapshot.getKey();
double tutorLat = tutorSnapshot.child("Latitude").getValue(Double.class);
double tutorLng = tutorSnapshot.child("Longitude").getValue(Double.class);
//Get all Tutor data from Database
searchDB.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
tutorArrayList = new ArrayList<>();
//tutorArrayList.clear();
for (DataSnapshot key : snapshot.getChildren()) {
User tutor = key.getValue(User.class);
String typeData = tutor.getType().toLowerCase(Locale.ROOT);
if (calculateDistanceInKilometer(userLat,userLng,tutorLat,tutorLng) <= userRadius){
if (typeData.equals("tutor")){
tutorArrayList.add(tutor);
}
}
}
//Toast.makeText(getContext(), "fetchID: "+fetchID ,Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Radius: "+userRadius ,Toast.LENGTH_SHORT).show();
//Toast.makeText(getContext(), "Distance: "+calculateDistanceInKilometer(userLat,userLng,tutorLat,tutorLng) ,Toast.LENGTH_SHORT).show();
adapter = new SearchAdapter(getActivity(), tutorArrayList);
searchList.setAdapter(adapter);
adapter.notifyDataSetChanged();
if (adapter.getItemCount()==0){
searchList.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
}else {
searchList.setVisibility(View.VISIBLE);
empty.setVisibility(View.GONE);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
Example Output of calculation:
Radius set to = 10 and 300
dataset
1- 0.0
2- 6.0
3- 290.0
Result
if Radius is 10 no data stored in Array
if Radius is 300 all data stored in array
Expected output to be:
If Radius set to 10, data1 and data2 stored
if Radius set to 300, all data stored
how to get that imageUrl list into imageSlider..
It retrieve the first two images instead of all the images ?!!
Here is my Database
Slider
Slider2
imageUrl0:
"https://firebasestorage.googleapis.com/v0/b/sho..."
imageUrl1:
"https://firebasestorage.googleapis.com/v0/b/sho..."
imageUrl2:
"https://firebasestorage.googleapis.com/v0/b/sho..."
imageUrl3:
"https://firebasestorage.googleapis.com/v0/b/sho..."
imageUrl4:
"https://firebasestorage.googleapis.com/v0/b/sho..."
sliderName:
"Slider2"
And here is my code ( denzcoskun library)
imageSlider = findViewById(R.id.image_slider);
final List<SlideModel> slideModels = new ArrayList<>();
FirebaseDatabase.getInstance().getReference().child("Slider")
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
int k = (int) snapshot.getChildrenCount();
for (DataSnapshot data : snapshot.getChildren()){
int i = 0;
while (i <= k) {
slideModels.add(new SlideModel(data.child("imageUrl" + i).getValue().toString(),
data.child("sliderName").getValue().toString(), ScaleTypes.FIT));
imageSlider.setImageList(slideModels, ScaleTypes.FIT);
i++;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
I want to ask about how to limit retrieve data in Android Firebase. Here is my code:
private void lookingforHelp(){
DatabaseReference dbref = FirebaseDatabase.getInstance().getReference("Locations");
dbref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
int limitUser = 0;
ModelUsersLocation mUserLoct = ds.getValue(ModelUsersLocation.class);
LatLng yourLatLng = new LatLng(latitude_user, longitude_user);
LatLng polisiLatLng = new LatLng(mUserLoct.getLatitude(), mUserLoct.getLongitude());
if (mUserLoct.getPengguna().equals("polisi")){
if (SphericalUtil.computeDistanceBetween(yourLatLng, polisiLatLng) < 700) {
if (limitUser <= 5){
if (mUserLoct.getJangkauan().isEmpty()) {
DatabaseReference dbref = FirebaseDatabase.getInstance().getReference("Locations").child(mUserLoct.getUser().getUid());
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("jangkauan", fuser.getUid());
dbref.updateChildren(hashMap).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
writeNewOrder();
}
});
}
}
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
I tried to retrieve data that i want is only less than 5 users, using int limitUser = 0; and it's still not working to retrieve data only less than 5 users.
PROBLEM SOLVED, I try to create markeroptions and make it into invisible, and then add it into arraylist, if (markers.size() <= 5) {do something}
thank u guys for your help btw i really apreciate it :))
public MutableLiveData<ArrayList<Vendor>> getVendorsByLimit(int limit) {
ArrayList<Vendor> arrayList = new ArrayList<>();
Query query = FirebaseDatabase.getInstance().getReference().child("VENDOR_NODE").limitToLast(limit);
query.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() > 0) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.getChildrenCount() > 0) {
//String value = (String) snapshot.getValue(); //Get all child data
//OR
//String singleValue = (String) snapshot.child("fullName").getValue(); //Get single child data
Vendor model = snapshot.getValue(Vendor.class);
String key = snapshot.getKey();
arrayList.add(model);
}
}
data.setValue(arrayList);
} else {
data.setValue(null);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.e(TAG, "" + databaseError.getMessage());
data.setValue(null);
}
});
}
Query query = FirebaseDatabase.getInstance().getReference().child("VENDOR_NODE").limitToLast(limit);
You can sort them by using timeStamp like
-----------------------------------------
FirebaseDatabase.getInstance().getReference()
.child("Conversation").child(conversationID)
.limitToLast(100)
.orderByChild("timestamp")
.startAt(dateToStart) // pass timestamp from when you want to start filtering
.endAt(dateToEnd); // pass timestamp till when you want to apply filtering
I want to make a filtering method in my application. So, I want to display all the city from the firebase database inside my spinner without display the same city. For your information, the city is in the EditText field.
My coding:-
private void loadFireBaseDataList(final String referenceKey)
{
databaseReference = FirebaseDatabase.getInstance().getReference(referenceKey);
databaseReference.addValueEventListener(new ValueEventListener()
{
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
final List<String> identity2 = new ArrayList<String>();
identity2.add(0, "Location");
for (DataSnapshot snapshot : dataSnapshot.getChildren())
{
for (DataSnapshot dataSnapshot2 : snapshot.getChildren())
{
identity2.add(dataSnapshot2.child("adstuitioncity").getValue(String.class));
}
}
ArrayAdapter<String> nameAdapter2 = new ArrayAdapter<String>(FilterTuitionCentre.this, android.R.layout.simple_spinner_item, identity2);
nameAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spLocation.setAdapter(nameAdapter2);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
My database:-
My interface output:-
You could use Set<String> which contains no duplicates:
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Set<String> cities = new HashSet<String>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
// ...
cities.add(value)
}
List<String> identity2 = new ArrayList<String>(cities);
identity2.add(0, "Location")
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FilterTuitionCentre.this, android.R.layout.simple_spinner_item, identity2);
// ...
}
after taking data with takeModel in OnDataChange() I want to put them into arraylist, which is resposinble for creating activity.
final String[] takeModel = null;
....
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i=0;
int lenght = (int) dataSnapshot.getChildrenCount();
String[] takeModel = new String[lenght];
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
TvAtributes tvnowe = postSnapshot.getValue(TvAtributes.class);
takeModel[i]=tvnowe.getModel(); // taking data here
i++;
}
}
......
ArrayList<TvAtributes> tv_models = new ArrayList<>();
for(int i=0;i<android_version_names.length;i++){
TvAtributes tvModele = new TvAtributes();
tvModele.setModel(takeModel[i]); //this is crashing
tvModele.setImgaddres(image_urls[i]);
tv_models.add(tvModele);
I have no clue what to do.
Thanks in advance
The answer is
final ArrayList<TvAtributes> tv_models = new ArrayList<>();
.....
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
int i=0;
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
TvAtributes tvnowe = postSnapshot.getValue(TvAtributes.class);
tvnowe.getModel();
tvnowe.setImgaddres(image_urls[i]);
tv_models.add(tvnowe);
i++;
}
...
return tv_models;
Thanks for help