'fething data from Firebase using listview in andriod studio [duplicate]
my app is crushing , can anyone tell what is the problem ??
** error is : 2022-05-19 11:54:40.455 6609-6609/com.example.adapterclasswitsimpleoverview E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.adapterclasswitsimpleoverview, PID: 6609 com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217) **
code: ** public class MainActivity extends AppCompatActivity {
// creating variables for our list view.
ListView coursesLV;
ArrayList<String> coursesArrayList=new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance("https://newcode-c147f-default-rtdb.firebaseio.com/");
DatabaseReference myRef = database.getReference("User");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> myArrayAdapter= new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,coursesArrayList);
coursesLV = findViewById(R.id.idl);
coursesLV.setAdapter(myArrayAdapter);
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
String value=snapshot.getValue(String.class);
coursesArrayList.add(value);
myArrayAdapter.notifyDataSetChanged();
}
enter code here
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
myArrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}**
<ListView
android:id="@+id/idl"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
