gpt4 book ai didi

java - Firebase 数据到 Spinner

转载 作者:行者123 更新时间:2023-12-02 12:01:36 27 4
gpt4 key购买 nike

尝试访问的数据 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>

最佳答案

您可以通过以下方式创建一个方法来设置微调器中的数据:

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
}

关于java - Firebase 数据到 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47210718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com