我正在制作一个使用Firebase作为后端的出勤应用程序。我有一个微调器Semester
,应在此基础上填充另一个微调器类。
我可以从Firebase提取数据并填充Semester微调器,但是基于它的选择,Class微调器不起作用。
这是我的尝试:
package info.androidhive.firebase;
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.view.View;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewParent;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.List;
/*
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
*/
public class TakeAttendanceMenu extends AppCompatActivity implements OnItemSelectedListener{
String baseDb = "Courses";
String selectedSem;
String semClassDb;
Spinner semSpinner,classSpinner,subjectSpinner;
private static final String TAG = "TakeAttendanceMenu";
FirebaseFirestore db = FirebaseFirestore.getInstance();
List<String> semSpinnerList = new ArrayList<String>();
List<String> ClassSpinnerList = new ArrayList<String>();
public void getAllSemesters()
{
db.collection(baseDb)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for(DocumentSnapshot document: task.getResult()){
Log.d(TAG,document.getId() + "=>" + document.getData());
semSpinnerList.add(document.getId());
}
}
Toast.makeText(getApplicationContext(), "Please chal ja bc", Toast.LENGTH_SHORT).show();
}
});
}
public void getSemesterClasses()
{
semClassDb = baseDb + "/" + selectedSem;
db.collection(semClassDb).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if(task.isSuccessful()){
for(DocumentSnapshot documentSnapshot: task.getResult()){
ClassSpinnerList.add(documentSnapshot.getId());
}
}
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_attendance_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
semSpinner = (Spinner)findViewById(R.id.semester_spinner);
classSpinner = (Spinner)findViewById(R.id.class_spinner);
subjectSpinner = (Spinner)findViewById(R.id.subject_spinner);
setSupportActionBar(toolbar);
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();
}
}
);
getAllSemesters(); // calling this method to instantiate the first spinner(semester spinner) with the values of firestore
ArrayAdapter<String> semAdapter= new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, semSpinnerList);
semAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
semSpinner.setAdapter(semAdapter);
/*Adapter for the Semester -> Class*/
ArrayAdapter<String> classAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,ClassSpinnerList);
classAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
classSpinner.setAdapter(classAdapter);
semSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Item Selected "+position, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(getApplicationContext(), "Please chal ja bc", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
您好像错过了这一行:
classspinner.setOnItemSelectedListener(this);
在之后写下:
/*Adapter for the Semester -> Class*/
ArrayAdapter<String> classAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,ClassSpinnerList);
classAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
classSpinner.setAdapter(classAdapter);
semSpinner.setOnItemSelectedListener(this);
..... (Write Here)
下一步:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (view.getId()) {
case R.id.first_spinner:
Toast.makeText(getApplicationContext(), "Item Selected "+position, Toast.LENGTH_SHORT).show();
break;
case R.id.second_spinner:
Toast.makeText(getApplicationContext(), "Item Selected "+position, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
希望它应该能工作。
答案 1 :(得分:0)
代码中的问题是,您正在使用尚不可用的结果来创建和设置适配器。换句话说,在设置适配器时,从数据库获取的列表尚不可用。为了解决这个问题,在两种情况下,我都建议您在回调中创建并设置适配器。请在for循环后立即将每行代码移动到相应方法中。