0
데이터는 내가 스피너에 데이터베이스의 모든 값을 저장하기 위해 노력하고있어중포 기지 데이터베이스 스피너 [코드 청소 도움말]에
에 액세스하려고합니다. 최종 결과는 모든 값을 회 전자에 표시해야합니다. 어쨌든 코드가 구현되어 있지만 spinnerData()
기능에서 필요한 청소가 있다고 생각됩니다.
참고 : 코드는 완벽하게 작동하지만 사실은 너무 많은 개체와 spinnerData()
의 값의 너무 많은 통과를 사용하고 그것이 좋은 것 몇 가지 간단한 코드를 달성 할 수있는 경우이다.
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
}
}
코드 재구성에 대해 질문하는 경우 https://codereview.stackexchange.com/tour를 확인하십시오. –