我想根据类别从Firebase获取数据,该类别是动态的。用户可以从定义的列表中选择一个类别。有一个类别是ALL,我的目标是,当用户选择ALL,然后必须检索所有数据时,我能够读取具有类别的数据,但是我不知道如何读取不具有该类别的数据。没有分类。
usuarios.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot usuarios : dataSnapshot.getChildren()) {
DatabaseReference user = usuarios.getRef();
String eventUserOwnerKey = user.getKey();
//Al elegir la opción all, se quitara todos los filtros y se mostrara todos los eventos
if (value!="ALL") {
System.out.println("ALALALALALALALALALALALALALALALAL");
Query eventos = usuarios.getRef().child("eventos").orderByChild("categoria").equalTo(value);
//Aqui compruebo si el adaptado tiene valor o no.
//En caso de que tenga, entonces que vacie para que cada que se ejecute la consulta con
//Diferente categoria, se vaya vaciando los resultados anteriores
if (adapter != null) {
adapter.clear();
}
eventos.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot eventos : dataSnapshot.getChildren()) {
//Recupero el objeto Evento des de la base de datos
Evento evento = eventos.getValue(Evento.class);
resultradoEventos.add(evento);//Guardo en el arraylist los eventos recuperados.
lvEventos = view.findViewById(R.id.lvEventos);
if (getContext() != null) {
adapter = new EventpAdapter(getContext(), R.layout.layout_evento, resultradoEventos);
lvEventos.setAdapter(adapter);
lvEventos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Evento evento = (Evento) parent.getItemAtPosition(position);
Intent i = new Intent(mContext, DetailEvento.class);
i.putExtra("evento", evento);
i.putExtra("eventUserOwnerKey", eventUserOwnerKey);
startActivity(i);
}
});
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}else if (value=="ALL"){
System.out.println("HOLALALALALALALALALAL");
}
}
这是我的数据
"usuarios" : {
"QnUqKgJfq2XxDYcsvp4YnJ2YFhn1" : {
"eventos" : {
"-LgIlXbocvzN3ZpbwuFR" : {
"caducidad" : "15",
"categoria" : "DAM",
"descripcion" : "mac",
"horaDelEvento" : "mac",
"imagenUrl" : "https://firebasestorage.googleapis.com/v0/b/english-app-b2170.appspot.com/o/Uploads%2F538138937?alt=media&token=7d77c944-2037-43a1-ab2b-871ba3068966",
"sitioDelEvento" : "mac",
"titol" : "mac"
},
"-LgIljoH-AsRchkQ4xbw" : {
"caducidad" : "67989",
"categoria" : "DAM",
"descripcion" : "nadim",
"horaDelEvento" : "nadim",
"imagenUrl" : "https://firebasestorage.googleapis.com/v0/b/english-app-b2170.appspot.com/o/Uploads%2F538138937?alt=media&token=7d77c944-2037-43a1-ab2b-871ba3068966",
"sitioDelEvento" : "nadim",
"titol" : "nadim"
}
}
},
"UvTCjIYMA0btE14QHz7twbdN7zT2" : {
"eventos" : {
"-LdPfYoRre18oGpd3Oda" : {
"caducidad" : "2019-04-26",
"categoria" : "SMIX",
"descripcion" : "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the",
"horaDelEvento" : "12:12",
"imagenUrl" : "https://firebasestorage.googleapis.com/v0/b/english-app-b2170.appspot.com/o/usuarios%2FUvTCjIYMA0btE14QHz7twbdN7zT2%2Fimagenes%2Fpexels-photo-160107.jpeg?alt=media&token=f0b7b573-8ca4-4fd2-a5e4-da43b967ff4b",
"sitioDelEvento" : "Aula 33",
"titol" : "Lorem Ipsum"
}
}
},
"hzqvjnkTdJcyTOPUcEMrAmvEXUo1" : {
"eventos" : {
"-LfofwABcBVf_ID29Iye" : {
"caducidad" : "12",
"categoria" : "SMIX",
"descripcion" : "Fifa",
"horaDelEvento" : "2",
"imagenUrl" : "https://firebasestorage.googleapis.com/v0/b/english-app-b2170.appspot.com/o/Uploads%2F35034?alt=media&token=382a36d6-f12a-4d9b-b67e-0e61e21a77fc",
"sitioDelEvento" : "Fif",
"titol" : "Fifaaa"
}
}
},
"pzgsylJZenYmOAUytReg1TSDNv82" : {
"eventos" : {
"-LgWUjpk8m4YHUHc4mQj" : {
"caducidad" : "2019-06-04",
"categoria" : "DAM",
"descripcion" : "Trial",
"horaDelEvento" : "15:00",
"imagenUrl" : "https://firebasestorage.googleapis.com/v0/b/english-app-b2170.appspot.com/o/usuarios%2FpzgsylJZenYmOAUytReg1TSDNv82%2Fimagenes%2Fmacbook-apple-imac-computer-39284.jpeg?alt=media&token=cb0c0c1d-dd8a-422c-82da-f8bc9ba30e0c",
"sitioDelEvento" : "Aula:73",
"titol" : "Trial Today"
}
}
}
}
这是我的类别
"categorias" : {
"-Lg3sBVLlFv5dnIkqh2t" : {
"titiolCategoria" : "SMIX"
},
"-Lg3t1hokeFkrj2SpgDH" : {
"titiolCategoria" : "DAM"
},
"-LgIaTyAnU_GbHMQLn1I" : {
"titiolCategoria" : "ALL"
},
"-LgWcNmZqpG6tHNCmXgA" : {
"titiolCategoria" : "AFI"
}
},
我想根据类别显示数据,如果用户选择类别ALL,则必须显示所有可用数据。否则,它必须显示所选类别的数据。
答案 0 :(得分:0)
听起来您有两种情况:
ALL
,则您想读取所有用户。ALL
,则只希望读取具有该类别的用户。我会这样实现:
Query eventos = usuarios.getRef().child("eventos");
if (!value.equals("ALL")) {
eventos = eventos.orderByChild("categoria").equalTo(value);
}
eventos.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot eventos : dataSnapshot.getChildren()) {
...
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // never ignore errors
}
});