Data Trying to access
I'm trying to store all the values of database to a spinner. Final result should be to show all values in a spinner. Code is implemented somehow but I feel that there is some cleaning required in spinnerData()
function.
Note: Code is fully working but the fact is that it is using too much objects and too many passing of values in spinnerData()
and if it can be achieved with some simpler code that would be great.
SymptomActivity.java
package com.example.nishantsikri.microdoctor;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SymptomActivity extends AppCompatActivity {
private DatabaseReference mFirebaseDatabase, spinnerDatabase;
private FirebaseDatabase mFirebaseInstance;
Button button;
TextView textView;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_symptom);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button = (Button) findViewById(R.id.button8);
textView = (TextView) findViewById(R.id.textView2);
spinner = (Spinner) findViewById(R.id.spinner2);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
// Write a message to the database
mFirebaseInstance = FirebaseDatabase.getInstance();
// store app title to 'app_title' node
mFirebaseInstance.getReference("app_title").setValue("microDoctor");
mFirebaseDatabase = mFirebaseInstance.getReference("symptomList");
spinnerDatabase = mFirebaseInstance.getReference("symptomView");
//mFirebaseDatabase.child("Headache").child("Some Value").setValue("Head");
//mFirebaseDatabase.setValue("Headache");
spinnerData();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void readDataListener() {
mFirebaseDatabase.child("Abdominal Cramps").child("Diarrhea").child("Vomiting").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Symptom symptom = dataSnapshot.getValue(Symptom.class);
// Check for null
if (symptom == null) {
Log.e("Data", "Symptom data is null!");
return;
}
Log.e("Symptom Data", "User data is changed!"+symptom.getDisease());
// Display newly updated name and email
textView.setText(symptom.symptom1+"\n"+symptom.symptom2+"\n"+symptom.symptom3+"\n"+symptom.disease+"\n"+dataSnapshot.getKey());
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
private void spinnerData() {
spinnerDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.e("Spinner Data", "Spinner data is changed!");
DataSnapshot data = dataSnapshot;
Iterable<DataSnapshot> temp = data.getChildren();
ArrayList<String> list = new ArrayList<>();
// list.add(dataSnapshot.getValue().toString());
// showDataInSpinner(list);
for (DataSnapshot lists : temp){
Log.d("ddd","Array List: "+lists.getValue().toString());
list.add(lists.getValue().toString());
textView.setText(lists.getValue().toString());
}
showDataInSpinner(list);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
public void showDataInSpinner(ArrayList<String> data) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, data
); //Create the Adapter to set the data
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //Set the layout resource to create the drop down views.
spinner.setAdapter(adapter); //Set the data to your spinner
}
}