Hi I'm trying to display certain data on my listview based on item selected from a spinner. This is how the display page currently looks. It's reading all the data from the node. Because "Running" is selected on the spinner above, I want the list view to only show Running.
Here is the code i'm using to read all the data.
#Override
protected void onStart() {
super.onStart();
//reading data to listview, every time its saved
currentUserDB2.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
cardiosList.clear();
for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){
Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class);
cardiosList.add(cardio);
}
CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
ListViewCardioHistory.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Working code
spinnerCardioHistory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String name = spinnerCardioHistory.getSelectedItem().toString();
currentUserDB2.orderByChild("detailCategory").equalTo(name).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
cardiosList.clear();
for (DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){
Cardio cardio = cardioHistorySnapshot.getValue(Cardio.class);
cardiosList.add(cardio);
}
CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
ListViewCardioHistory.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
If you want only the word Running to be displayed then do the following:
String text = spinner.getSelectedItem().toString();
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference currentUserDB2=FirebaseDatabase.getInstance().getReference().child("Cardio").child(user.getUid());
currentUserDB2.orderByChild("detailCategory").equalTo(text).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
cardiosList.clear();
for(DataSnapshot cardioHistorySnapshot : dataSnapshot.getChildren()){
String category=cardioHistorySnapshot.child("detailCategory").getValue().toString();
cardiosList.add(category);
}
CardioHistoryList adapter = new CardioHistoryList(Cardio_History.this, cardiosList);
ListViewCardioHistory.setAdapter(adapter);
First you get the select texted that is in the spinner String text = spinner.getSelectedItem().toString();
then you add it in the query orderByChild("detailCategory").equalTo(text) and then retrieve from the database and it will only add the word that is selected in the spinner which is "Running".
Related
I'm working on a project where there's an activity which contains 3 spinners( Nested). I need the 2nd spinner to have the data based on the selected item of the 1st spinner and the data is retrieved from Firebase
The 1st spinner is going to have all "Gouv" values , the 2nd one is going to have the "Deleg" values where "Gouv" value is equal to the selected item "Gouv" of the 1st spinner .
Here is the code that i tried and i keep get in the log Null .
public class RechCode extends AppCompatActivity {
DatabaseReference spinnerRef;
Query spinnerRefG;
Spinner spinnerG;
Spinner spinnerD;
Spinner spinnerL;
ArrayAdapter<String> adapterG;
ArrayAdapter<String> adapterL;
ArrayAdapter<String> adapterD;
ArrayList<String> spinnerDList;
ArrayList<String> spinnerGList;
ArrayList<String> spinnerLOList;
Query spinnerRefD;
private String ChoixG;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rech_code);
spinnerG = findViewById(R.id.SpinnerGouv);
spinnerD = findViewById(R.id.SpinnerDeleg);
spinnerL = findViewById(R.id.SpinnerLoc);
spinnerRef = FirebaseDatabase.getInstance().getReference().child("CodePostale");
//Query queryD = spinnerG.("state", "CA");
spinnerGList = new ArrayList<>();
spinnerDList = new ArrayList<>();
spinnerLOList = new ArrayList<>();
adapterD= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerDList);
adapterL= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerLOList);
adapterG= new ArrayAdapter<String>( RechCode.this, android.R. layout. simple_spinner_dropdown_item,spinnerGList);
spinnerG.setAdapter(adapterG);
spinnerD.setAdapter(adapterD);
spinnerL.setAdapter(adapterL);
ShowdataGouv();
spinnerRefG= FirebaseDatabase.getInstance().getReference("CodePostale").child("Deleg").orderByChild("Gouv").equalTo(ChoixG);
ShowdataDeleg();
spinnerG.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
ChoixG = spinnerGList.get(i);
Toast.makeText(getApplicationContext(), "Gouvernorat:" + ChoixG, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
Button retour = (Button) findViewById(R.id.return_btn);
retour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RetourMenu();
}
});
}
private void ShowdataLoc(String text) {
spinnerRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
for (DataSnapshot item:snapshot1.getChildren()){
spinnerLOList.add(item.child("Loc").getValue().toString());
}
adapterD.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private void ShowdataDeleg() {
spinnerRefG.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot2) {
for (DataSnapshot item:snapshot2.getChildren()){
spinnerDList.add(item.child("Deleg").getValue().toString());
}
Log.d("Value","BBBBBBBB"+ snapshot2);
adapterD.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private void ShowdataGouv() {
spinnerRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot3) {
for (DataSnapshot item:snapshot3.getChildren()){
spinnerGList.add(item.child("Gouv").getValue().toString());
Log.d("Value","AAAAAAAAA"+ snapshot3);
}
adapterG.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private void RetourMenu() {
Intent intent = new Intent(getApplicationContext(), Menus.class);
startActivity(intent);
}
}
The problem is in the way you construct the query here:
spinnerRefG= FirebaseDatabase.getInstance()
.getReference("CodePostale")
.child("Deleg")
.orderByChild("Gouv")
.equalTo(ChoixG);
You're asking to load child nodes of /CodePostale/Deleg that have a Gouv property with a value of ChoixG. But if we check the screenshot of the data, there is no /CodePostale/Deleg, so no data is returned.
What you want instead is to check the child nodes of /CodePostale and return the ones where their Deleg/Gouv property has a value of ChoixG, which you can do with:
spinnerRefG= FirebaseDatabase.getInstance()
.getReference("CodePostale")
.orderByChild("Deleg/Gouv")
.equalTo(ChoixG);
I am trying to fill a spinner with categories, I manage to iterate every category in my database and add it to the spinner but I need to fetch only distinct values, see this image;
This is my code;
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
final List<String> categorySpinner = new ArrayList<>();
for (DataSnapshot childSpinner : snapshot.getChildren()) {
String spinnerCat = childSpinner.child("category").getValue(String.class);
categorySpinner.add(spinnerCat);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(LineChartActivity.this, R.layout.spinner_item, categorySpinner);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner_2.setAdapter(arrayAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
It's simple, simply change the line:
categorySpinner.add(spinnerCat);
to
if (!categorySpinner.contains(spinnerCat)) {
categorySpinner.add(spinnerCat);
}
Happy coding.
This is my firebase database:
I want to display the value of the auto-generated id given by the push method
this is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_recycler_view);
FirebaseApp.initializeApp(this);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String key = newRef.getKey();
preRef.child(key).get();
list = new ArrayList<>();
myAdapter = new MyAdapter(this, list);
recyclerView.setAdapter(myAdapter);
preRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Calendar calendar = dataSnapshot.getValue(Calendar.class);
list.add(calendar);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
I cant display the value of the Uid can someone please help me?
To display the list of dates, you can do:
DatabaseReference actlogRef = FirebaseDatabase.getInstance().getReference("ActLog");
actlogRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.child("date").getChildren()) {
String date = dataSnapshot.getValue(String.class);
list.add(date);
}
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
For this, list will have to be a list of strings, as that's what you store in the database. If you must use a list of Date objects, have a look at questions about parsing a data from a string in java.
I'm trying to get the key of a certain child value by allowing the user to choose from a Spinner.
The Spinner has "Product_Name" values as its choices. By choosing one, the program should get its key and use it to get another child value for other uses.
Example:
PRODUCTS->
-LoUXnfUCEj4k4A3dkte->
Product_Name:"Steak"
By choosing "Steak" in the Spinner, I have to get "-LoUXnfUCEj4k4A3dkte"
databaseRefSelectItem = FirebaseDatabase.getInstance().getReference("PRODUCTS");
final DatabaseReference mDatabase = databaseRefSelectItem;
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull final DataSnapshot dataSnapshot) {
//We create an array list to hold the values brought from the database and show them in the spinner
final List<String> titleList = new ArrayList<String>();
for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
titleProduct = snapshot.child("Product_Name").getValue(String.class);
//populate the spinner with that array list
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AddTransactionActivity.this, android.R.layout.simple_spinner_item, titleList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectProduct.setAdapter(arrayAdapter);
titleList.add(titleProduct);
//Click event for each spinner element
selectProduct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//pass the reference from that value into another snapshot in order to query those values, here you need to get your node id and inside just get your number , name and so on
selectItem = titleList.get(i);
mDatabase.child(selectItem).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(final DataSnapshot dataSnapshot2) {
key = dataSnapshot2.getKey();
currentItemStock = dataSnapshot2.child(key).child("Current_Stock").getValue(String.class);
currentStk.setText(key);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
How can I get "-LoUXnfUCEj4k4A3dkte"?
Notes:
-LoUXnfUCEj4k4A3dkte is randomly generated.
Use .getKey() to get the key of the snapshot like:
for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.child("Product_Name").getValue(String.class).equals("Steak")){
String theKey = snapshot.getKey(); //This will return -LoUXnfUCEj4k4A3dkte
}
}
Will return key of the snapshot at that reference.
Found a solution.
Thank you as well to Yash Krishan for the code snippet :).
databaseRefSelectItem = FirebaseDatabase.getInstance().getReference("PRODUCTS");
final DatabaseReference mDatabase = databaseRefSelectItem;
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull final DataSnapshot dataSnapshot) {
//We create an array list to hold the values brought from the database and show them in the spinner
final List<String> titleList = new ArrayList<String>();
for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
titleProduct = snapshot.child("Product_Name").getValue(String.class);
//populate the spinner with that array list
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AddTransactionActivity.this, android.R.layout.simple_spinner_item, titleList);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectProduct.setAdapter(arrayAdapter);
titleList.add(titleProduct);
//Click event for each spinner element
selectProduct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//pass the reference from that value into another snapshot in order to query those values, here you need to get your node id and inside just get your number , name and so on
selectItem = titleList.get(i);
if (titleProduct.equals(selectItem)){
key = dataSnapshot.child(selectItem).getKey(); //This will return -LoUXnfUCEj4k4A3dkte
}
mDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull final DataSnapshot dataSnapshot2) {
for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.child("Product_Name").getValue(String.class).equals(selectItem)){
key = snapshot.getKey().toString();
}
keyholder = dataSnapshot.child(key).child("Current_Stock").getValue(String.class);
}
currentStk.setText(keyholder);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Now im trying to create my first Android app.
I'm currently working on a view name Bands which is supposed to create a list (imported from Firebase Database) and link it to a ListView.
As you can see, i have a link to print out the whole bandsList, that way i know the list imported well, and it is...but the ListView is not updated according to the bandsList, so im guessing the problem is in the adapter.
public class Bands extends AppCompatActivity {
ListView listView;
ArrayList<String> bandsList = new ArrayList<>();
ArrayAdapter<String> bandsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bands);
listView = findViewById(R.id.bandsListView);
bandsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, bandsList);
listView.setAdapter(bandsAdapter);
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Bands");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot band : dataSnapshot.getChildren()) {
String bandName = band.child("Name").getValue(String.class);
bandsList.add(bandName);
System.out.println("bandsList :" + bandsList);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//String BandName =dataSnapshot.getValue(String.class);
for (DataSnapshot band : dataSnapshot.getChildren()) {
String bandName = band.child("Name").getValue(String.class);
System.out.println(bandName);
bandsList.add(bandName);
System.out.println(bandsList);}
}
#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) {
}
});
}
}
Just add bandsAdapter.notifyDataSetChanged() after the Firebase-Callback:
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot band : dataSnapshot.getChildren()) {
String bandName = band.child("Name").getValue(String.class);
bandsList.add(bandName);
System.out.println("bandsList :" + bandsList);
}
bandsAdapter.notifyDataSetChanged();
}
You need to tell the adapter to update the list in both
onDataChange() and onChildAdded() using bandsAdapter.notifyDataSetChanged();