我创建了fire base实时数据库项目并尝试在Recycler View中显示学生列表,数据通常出现在fire base console中但是没有出现在模拟器中?
这是Recycler View类代码:
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.StudentHolder> {
private Context context;
private List<Student> studentList;
public StudentAdapter(Context context, List<Student> studentList) {
this.context = context;
this.studentList = studentList;
}
@Override
public StudentHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_row, parent, false);
return new StudentHolder(row);
}
@Override
public void onBindViewHolder(StudentHolder holder, int position) {
final Student student = studentList.get(position);
holder.studentNameTv.setText(student.getName());
holder.studentAvgTv.setText(String.valueOf(student.getAverage()));
holder.editStudentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, EditStudentActivity.class);
intent.putExtra("name", student.getName());
context.startActivity(intent);
}
});
holder.deleteStudentBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();
Query query = ref.child("students").orderByChild("name").equalTo(student.getName());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
snapshot.getRef().removeValue();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
@Override
public int getItemCount() {
return studentList.size();
}
class StudentHolder extends RecyclerView.ViewHolder {
TextView studentNameTv, studentAvgTv;
Button deleteStudentBtn, editStudentBtn;
public StudentHolder(View itemView) {
super(itemView);
studentNameTv = (TextView) itemView.findViewById(R.id.student_name);
studentAvgTv = (TextView) itemView.findViewById(R.id.student_avg);
deleteStudentBtn = (Button) itemView.findViewById(R.id.delete_student);
editStudentBtn = (Button) itemView.findViewById(R.id.edit_student);
}
}
}
以下是主要活动类代码:
public class MainActivity extends AppCompatActivity {
List<Student> studentList = new ArrayList<>();
StudentAdapter adapter;
DatabaseReference ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FirebaseDatabase database = FirebaseDatabase.getInstance();
ref = database.getReference();
ref.child("students").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
studentList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Student student = snapshot.getValue(Student.class);
studentList.add(student);
adapter.notifyDataSetChanged();
}
Collections.reverse(studentList);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);
adapter = new StudentAdapter(MainActivity.this, studentList);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
recyclerView.setAdapter(adapter);
SearchView searchView = (SearchView) findViewById(R.id.search_view);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Query fireQuery = ref.child("students").orderByChild("name").equalTo(query);
fireQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) {
Toast.makeText(MainActivity.this, "ERROR", Toast.LENGTH_SHORT).show();
} else {
List<Student> searchList = new ArrayList<Student>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Student student = snapshot.getValue(Student.class);
searchList.add(student);
adapter = new StudentAdapter(MainActivity.this, searchList);
recyclerView.setAdapter(adapter);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
adapter = new StudentAdapter(MainActivity.this, studentList);
recyclerView.setAdapter(adapter);
return false;
}
});
}
}