试图访问的数据:
如果我为" Bloating"选择值?和"腹泻"在我的应用程序中,它应该显示" Cramps"和"无法控制的肠综合征"作为textView中的输出。获取此类情况的值的函数是readDataListenerForTwo()。我该如何更正代码?
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
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.security.SecurityPermission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SymptomActivity extends AppCompatActivity {
static boolean calledAlready = false;
Integer i = 2;
private DatabaseReference mFirebaseDatabase, spinnerDatabase;
private FirebaseDatabase mFirebaseInstance;
Button button,add_button,sub_button;
TextView textView,symptom1,symptom2,symptom3;
Spinner spinner,spinner2,spinner3;
String textSymptom1,textSymptom2,textSymptom3;
@Override
public void onBackPressed() {
startActivity(new Intent(getApplicationContext(),NavigationActivity.class));
finish();
}
@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);
add_button = (Button) findViewById(R.id.add_button);
sub_button = (Button) findViewById(R.id.sub_button);
textView = (TextView) findViewById(R.id.textView2);
symptom1 = (TextView) findViewById(R.id.symptom1);
symptom2 = (TextView) findViewById(R.id.symptom2);
symptom3 = (TextView) findViewById(R.id.symptom3);
spinner = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner3.setEnabled(false);
spinner3.setVisibility(View.GONE);
symptom3.setVisibility(View.GONE);
sub_button.setEnabled(false);
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();
}
});
if (!calledAlready)
{
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
calledAlready = 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();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom1 = spinner.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom2 = spinner2.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textSymptom3 = spinner3.getItemAtPosition(i).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(i == 3)
readDataListener();
else
readDataListenerForTwo();
}
});
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spinner3.setEnabled(true);
spinner3.setVisibility(View.VISIBLE);
symptom3.setVisibility(View.VISIBLE);
add_button.setEnabled(false);
sub_button.setEnabled(true);
i++;
Log.e("Value i","Value of i: "+i);
}
});
sub_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
spinner3.setEnabled(false);
spinner3.setVisibility(View.GONE);
symptom3.setVisibility(View.GONE);
add_button.setEnabled(true);
sub_button.setEnabled(false);
i--;
Log.e("Value i","Value of i: "+i);
}
});
}
private void readDataListener() {
mFirebaseDatabase.child(textSymptom1).child(textSymptom2).child(textSymptom3).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String symptom = dataSnapshot.child("disease").getValue(String.class);
// Check for null
if (symptom == null) {
Log.e("Data", "Symptom data is null!");
return;
}
Log.e("Symptom Data", "User data is changed!"+symptom);
// Display newly updated name and email
textView.setText(symptom+"\n"+dataSnapshot.getKey());
Intent intent = new Intent(getApplicationContext(),DiseaseActivity.class);
intent.putExtra("key", symptom);
startActivity(intent);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
}
private void readDataListenerForTwo(){
mFirebaseDatabase.child(textSymptom1).child(textSymptom2).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String symptom = dataSnapshot.getValue(String.class);
// Check for null
if (symptom == null) {
Log.e("Data", "Symptom data is null!");
Log.e("Chutiya","Haila! Chutiya run ho ra hai! ._.");
return;
}
Log.e("Symptom Data", "User data is changed!"+symptom);
// Display newly updated name and email
// textView.setText(symptom+"\n"+dataSnapshot.getKey());
// Intent intent = new Intent(getApplicationContext(),DiseaseActivity.class);
// intent.putExtra("key", symptom);
// startActivity(intent);
}
@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 = dataSnapshot.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
spinner2.setAdapter(adapter);
spinner3.setAdapter(adapter);
}
}
Gradle Error
11-16 01:02:13.816 22912-22912/com.example.nishantsikri.microdoctor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nishantsikri.microdoctor, PID: 22912
com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
at com.google.android.gms.internal.zzekp.zzb(Unknown Source)
at com.google.android.gms.internal.zzekp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.nishantsikri.microdoctor.SymptomActivity$9.onDataChange(SymptomActivity.java:204)
at com.google.android.gms.internal.zzeex.zza(Unknown Source)
at com.google.android.gms.internal.zzegs.zzbwg(Unknown Source)
at com.google.android.gms.internal.zzegy.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6236)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
答案 0 :(得分:2)
要在Diarrhea
下获取这些名称,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference diarrheaRef = rootRef.child("symptomList").child("Bloating").child("Diarrhea");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.getKey();
Log.d("TAG", name);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
diarrheaRef.addListenerForSingleValueEvent(eventListener);
您的输出将是:
Cramps
Uncontrollable Bowel Syndrome