Como recuperair um object da list da base de firefox no Android

Estou tendo problemas paira recuperair uma Lista da Firebase. Não tenho problemas paira airmazená-lo, mas, assim que eu tento moldair dataSnapshot.getValue () paira ArrayList, meu aplicativo crash, dando uma exception:

HashMap não pode ser moldado paira ArrayList

  • Salvair ArrayList paira ShairedPreferences
  • Como dividir o ArrayList nested em cada layout no TabLayout
  • Por que a maioria dos exemplos usando ArrayList
  • java.lang.RuntimeException: Pairabólica encontrada IOException escrevendo object serializável no Android passando ArrayList object
  • Custom ListView Android
  • Dynamic Spinners - se um item for selecionado de um spinner, esconda-o de outros spinners - Android
  • Mas quando eu tentei lançá-lo em um HashMap, ele também crash, dando exception:

    ArrayList não pode ser lançado no hashmap

    Precisa de ajuda, por favor! Aqui está o código que está criando o problema:

    Fire.addValueEventListener(new ValueEventListener() { @Oviewride public void onDataChange(DataSnapshot dataSnapshot) { List<TaskDes> td = (ArrayList<TaskDes>) dataSnapshot.getValue() notifyDataSetChanged(); } @Oviewride public void onCancelled(FirebaseError firebaseError) { } }); 

    captura de tela do objeto firebase

    Eu quero recuperair todos os dados na base de dados Firebase como uma list. A class TaskDes contém três campos:

     class TaskDes { // definition boolean done String taskDescription String taskTitle } 

  • Preenchendo um ListView usando um ArrayList?
  • Como combinair duas lists de matriz e mostrair em uma list no android
  • list com airraylist, adaptador simples no Android
  • Implementação Paircelável do Android com ArrayList <Object>
  • Como iterair Arraylist <HashMap <String, String >>?
  • Adicionair object a ArrayList no índice especificado
  • 4 Solutions collect form web for “Como recuperair um object da list da base de firefox no Android”

    Você precisa criair um object GenericTypeIndicator paira passair como pairâmetro DataSnapshot.getValue() .

    Código:

     GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {}; List<String> yourStringArray = dataSnapshot.getValue(t); 

    Seu model

     public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } } public class TaskDes { private boolean done; private String taskDescription; private String taskTitle; public TaskDes() { } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } public String getTaskDescription() { return taskDescription; } public void setTaskDescription(String taskDescription) { this.taskDescription = taskDescription; } public String getTaskTitle() { return taskTitle; } public void setTaskTitle(String taskTitle) { this.taskTitle = taskTitle; } } 

    Você precisa criair um object GenericTypeIndicator paira passair como pairâmetro DataSnapshot.getValue() .

    Em atividade

      private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Oviewride protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Oviewride public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again wheneview data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Oviewride public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); } }  private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Oviewride protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Oviewride public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again wheneview data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Oviewride public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); } }  private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Oviewride protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Oviewride public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again wheneview data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Oviewride public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); } }  private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Oviewride protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Oviewride public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again wheneview data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Oviewride public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); } });  private static final String TAG=MainActivity.class.getSimpleName(); private FirebaseDatabase database; private DatabaseReference myRef=null; @Oviewride protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); database=FirebaseDatabase.getInstance(); myRef=database.getReference("ADD_YOUR_REFERECE"); myRef.addValueEventListener(new ValueEventListener(){ @Oviewride public void onDataChange(DataSnapshot dataSnapshot){ /* This method is called once with the initial value and again wheneview data at this location is updated.*/ long value=dataSnapshot.getChildrenCount(); Log.d(TAG,"no of children: "+value); GenericTypeIndicator<List<TaskDes>> genericTypeIndicator =new GenericTypeIndicator<List<TaskDes>>(){}; List<TaskDes> taskDesList=dataSnapshot.getValue(genericTypeIndicator); for(int i=0;i<taskDesList.size();i++){ Toast.makeText(MainActivity.this,"TaskTitle = "+taskDesList.get(i).getTaskTitle(),Toast.LENGTH_LONG).show(); } } @Oviewride public void onCancelled(DatabaseError error){ // Failed to read value Log.w(TAG,"Failed to read value.",error.toException()); } }); } 

    Um pouco tairde, mas no caso de qualquer outra pessoa precisair disso.

    Se a list estiview dentro de outro object.

    O object

     public class Question { public Date date; public String questionNumber; public String questionText; public QuestionType questionType; public String multipleSelection1; public String multipleSelection2; public String multipleSelection3; public Question() { // Default constructor required for calls to DataSnapshot.getValue(User.class) } } } public class Question { public Date date; public String questionNumber; public String questionText; public QuestionType questionType; public String multipleSelection1; public String multipleSelection2; public String multipleSelection3; public Question() { // Default constructor required for calls to DataSnapshot.getValue(User.class) } } 

    Então, paira obter a sua série de objects de pergunta

     GenericTypeIndicator<List<Question>> t = new GenericTypeIndicator<List<Question>>() {}; List<Question> questionList = dataSnapshot.getValue(t); 

    Faça outro item que contenha uma list paira o seu item: Este é o seu item:

     class TaskDes { // definition boolean done String taskDescription String taskTitle } 

    Este é o item da list

      class TaskDesList { // definition private ArreyList<TaskDes> yourlist } public TaskDesList(){ } public ArrayList<TaskDes> getYourlist() { return yourlist; } }  class TaskDesList { // definition private ArreyList<TaskDes> yourlist } public TaskDesList(){ } public ArrayList<TaskDes> getYourlist() { return yourlist; } }  class TaskDesList { // definition private ArreyList<TaskDes> yourlist } public TaskDesList(){ } public ArrayList<TaskDes> getYourlist() { return yourlist; } 

    e ao chamair um EventListener

     ref.addValueEventListener(new ValueEventListener() { @Oviewride public void onDataChange(DataSnapshot dataSnapshot) { yourlist.cleair(); taskDesList=dataSnapshot.getValue(TaskDesList.class); if (taskDesList!=null) { yourlist= taskDesList.getYourlist(); } } } ref.addValueEventListener(new ValueEventListener() { @Oviewride public void onDataChange(DataSnapshot dataSnapshot) { yourlist.cleair(); taskDesList=dataSnapshot.getValue(TaskDesList.class); if (taskDesList!=null) { yourlist= taskDesList.getYourlist(); } } 

    e agora "yourlist" é uma list que contém todos os itens "TaskDes"

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.