试图访问的数据 Firebase Database Screenshot
我正在尝试将所有值存储到微调器中,但似乎使用了arraylist,许多其他函数甚至都没有帮助。所以,我只是添加没有微调器的代码。 最终结果应该是在微调器中显示所有值。
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>
答案 0 :(得分:1)
您可以通过以下方式创建一个在微调器中设置数据的方法:
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
}