我不完全知道如何处理嵌套数据。这是一个测验应用程序。
我具有以下数据库结构: The quiz has one or more questions and each question has one or more answers.
我正在尝试在Java类中执行以下操作:
public class Questionario implements Serializable, Parcelable {
@PrimaryKey
private String id;
private String titulo;
private String admin;
private Pergunta pergunta;`
public class Pergunta implements Serializable, Parcelable {
@PrimaryKey
private String id;
private String descricao;
private Resposta resposta;`
public class Resposta implements Serializable {
@PrimaryKey
private String id;
private String descricao;
private String dica;`
现在仅是一个Questionario( quiz )对象。然后,我需要创建Pergunta(问题)对象,然后创建Resposta( answer )对象。
我需要在RecyclerView和CardView中显示对象。
关于新的Pergunta和Resposta:我正在考虑创建不同的屏幕,然后以某种方式链接它们(也许与Context: 当我在具有Pergunta对象的卡片视图中单击“ Cadastrar Resposta”按钮时,它将转到一个表单,以创建一个新的Resposta对象,例如this,而当我单击“ Listar Respostas”按钮时,它将转到另一个RecyclerView和CardView,其中列出了Resposta对象,例如this。其他按钮( excluir和editar )分别用于删除和编辑对象。
我尝试过这样的事情:
DAO
public class ConfiguraFirebase<T> {
private static DatabaseReference reference = null;
private static FirebaseStorage storage = null;
public static DatabaseReference getNoRaiz(){
return FirebaseDatabase.getInstance().getReference();
}
public static DatabaseReference getNo(String no){
return FirebaseDatabase.getInstance().getReference(no);
}
public static FirebaseStorage getStorage(){
return FirebaseStorage.getInstance();
}
}
适配器
LinhaConsultaPerguntaAdapter.class
public class LinhaConsultaPerguntaAdapter extends RecyclerView.Adapter<LinhaConsultaPerguntaAdapter.MyViewHolder> {
Context origem;
private List<Pergunta> perguntas = new ArrayList<>();
public LinhaConsultaPerguntaAdapter(Context origem, List<Pergunta> perguntas) {
this.perguntas = perguntas;
this.origem = origem;
}
private void flatten(List<Pergunta> perguntas) {
for (Pergunta pergunta : perguntas) {
perguntas.add(pergunta);
}
}
@Override
public int getItemCount() {
return perguntas.size();
}
@Override
public void onBindViewHolder(@NonNull final LinhaConsultaPerguntaAdapter.MyViewHolder myViewHolder, final int position) {
Pergunta p = perguntas.get(position);
myViewHolder.txtDescricao.setText(p.getDescricao());
myViewHolder.buttonExcluir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeItem(position);
}
});
myViewHolder.buttonEditar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pergunta pergunta = perguntas.get(position);
Intent intent = new Intent(origem, EditPergunta.class);
intent.putExtra("id", pergunta.getId());
myViewHolder.context.startActivity(intent);
}
});
myViewHolder.btnCadResposta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pergunta pergunta = perguntas.get(position);
Intent intent = new Intent(origem, CadResposta.class);
intent.putExtra("pergunta", (Parcelable) pergunta);
myViewHolder.context.startActivity(intent);
}
});
myViewHolder.btnListarRespostas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pergunta pergunta = perguntas.get(position);
Intent intent = new Intent(origem, ListResposta.class);
intent.putExtra("pergunta", (Parcelable) pergunta);
myViewHolder.context.startActivity(intent);
}
});
}
private void removeItem(int position) {
String mensagem = "Pergunta excluída com sucesso!";
final Pergunta p = perguntas.get(position);
final DatabaseReference reference = ConfiguraFirebase.getNo("Pergunta");
perguntas.remove(position);
reference.child(p.getId()).removeValue();
notifyItemRemoved(position);
notifyItemRangeChanged(position, perguntas.size());
}
@NonNull
@Override
public LinhaConsultaPerguntaAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemList = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.adapter_card_pergunta, viewGroup, false);
return new MyViewHolder(itemList);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Context context;
TextView txtDescricao;
Button buttonExcluir;
Button buttonEditar;
Button btnCadResposta;
Button btnListarRespostas;
public MyViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
txtDescricao = itemView.findViewById(R.id.txtDescricaoPergunta);
buttonExcluir = itemView.findViewById(R.id.btnExcluir);
buttonEditar = itemView.findViewById(R.id.btnEditar);
btnCadResposta = itemView.findViewById(R.id.btnCadResposta);
btnListarRespostas = itemView.findViewById(R.id.btnListarRespostas);
}
}
}
查看
CadPergunta.class
public class CadPergunta extends AppCompatActivity{
private DatabaseReference reference = ConfiguraFirebase.getNoRaiz();
Resposta resposta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cad_pergunta);
Intent intent = getIntent();
resposta = intent.getParcelableExtra("resposta");
Log.d("Position", "Posição = " + resposta);
}
public void cadPergunta(View view){
String descricao = ((EditText)findViewById(R.id.editTextDescricaoPergunta)).getText().toString();
Pergunta perg = new Pergunta(descricao, resposta);
DatabaseReference perguntas = reference.child("Pergunta");
perguntas.push().setValue(perg).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(CadPergunta.this, "Sucesso ao cadastrar pergunta!", Toast.LENGTH_SHORT).show();
limparCampos();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(CadPergunta.this, "Erro ao cadastrar pergunta!", Toast.LENGTH_SHORT).show();
}
});
}
@SuppressLint("SetTextI18n")
private void limparCampos(){
((EditText)findViewById(R.id.editTextDescricaoPergunta)).setText("Descricao");
}
}
ListPergunta.class
public class ListPergunta extends AppCompatActivity {
RecyclerView recyclerViewPergunta;
private List<Pergunta> perguntas = new ArrayList<>();
LinhaConsultaPerguntaAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_pergunta);
recyclerViewPergunta = findViewById(R.id.recyclerViewPergunta);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewPergunta.setLayoutManager(layoutManager);
DatabaseReference reference = ConfiguraFirebase.getNo("Pergunta");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String id = ds.getKey();
String descricao = ds.child("pergunta").getValue(String.class);
perguntas.add(new Pergunta(id, descricao));
}
myAdapter = new LinhaConsultaPerguntaAdapter(getApplicationContext(), perguntas);
recyclerViewPergunta.setAdapter(myAdapter);
recyclerViewPergunta.setHasFixedSize(true);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
但这是行不通的。似乎我需要编写每个单独的属性,例如this。