问题
Data trying to access Firebase Database Screenshot
I'm trying to store all the values to a spinner but it seems like using arraylist and many other functions aren't even helping much. So, I'm just adding the code without spinner. Final result should be to show all values in a spinner.
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;
@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);
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!");
ArrayList<String> list = new ArrayList<>();
list.add(dataSnapshot.getValue().toString());
for (String lists : list){
Log.d("ddd","Array List: "+dataSnapshot.getValue().toString());
textView.setText(lists);
}
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
}
content_symptom.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.nishantsikri.microdoctor.SymptomActivity"
tools:showIn="@layout/activity_symptom">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="70dp"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="127dp"
tools:layout_editor_absoluteY="170dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
回答1:
You can create a method to set the data in the spinner in the following way:
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.
youSpinner.setAdapter(adapter); //Set the data to your spinner
}
来源:https://stackoverflow.com/questions/47210718/firebase-data-to-spinner