问题
This Firebase database structure:
enter image description here
Here is my Device Class:
public class Device {
private String title;
private Map<String,Counters> CounterNumber;
private Counters counters;
public void setCounter(Map<String,Counters> CounterNumber){
this.CounterNumber=CounterNumber;
}
public Map<String,Counters> getCounter(){
return CounterNumber;
}
public Counters getCounters(){
return counters;
}
public void setCounters(Counters counters){
this.counters=counters;
}
public Device(){
}
public Device(String title){
this.title=title;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title=title;
}
}
Second class is my nested object I think
Second class Counters Class My Problem is I don't know how to read
Counterobject from the database withFirebaseUIandRecycleView.
public class Counters implements Serializable {
private String countername;
private String counternumber;
public Counters (){
}
public Counters(String counternumber,String countername){
this.counternumber=counternumber;
this.countername=countername;
}
public String getCounterName() {
return countername;
}
public void setCounterName(String counterName) {
this.countername = countername;
}
public String getCounterNumber() {
return counternumber;
}
public void setCounterNumber(String counternumber) {
this.counternumber = countername;
}
}
This My HomeActivity Here read from firebase objects
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView=(RecyclerView)view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
firebaseDatabase=FirebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference();
mDevice=new ArrayList<Device>();
mdevices=new Device();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
try {
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {
ArrayList<String> counterse= (ArrayList<String>) eventSnapshot.getValue();
Log.e(TAGE, counterse.toString());
Device device = eventSnapshot.getValue(Device.class);
String id = eventSnapshot.getKey();
Counters counters = eventSnapshot.child(id).getValue(Counters.class);
device.setCounters(counters);
mDevice.add(device);
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
displayDevices();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
private void displayDevices(){
options=
new FirebaseRecyclerOptions.Builder<Device>()
.setQuery(databaseReference.child("devices").orderByChild("user_id").equalTo(userid),Device.class).build();
adapter=
new FirebaseRecyclerAdapter<Device, MyRecyclerViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position, @NonNull Device model) {
mdevices=mDevice.get(position);
holder.txt_title.setText(model.getTitle());
holder.txt_counter.setText(mdevices.getCounters().getCounterNumber());
Log.i(TAG,mdevices.getCounters().getCounterNumber());
}
@NonNull
@Override
public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(getContext()).inflate(R.layout.device_list,parent,false);
return new MyRecyclerViewHolder(itemView);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
@Override
public void onStart() {
adapter.startListening();
super.onStart();
}
@Override
public void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
Here is my Error Code enter image description here
回答1:
You are getting the following error:
can't convert object of type java.util.ArrayList to type com.example.smartappfirebase.Counters
Because you getting an ArrayList object and not a Counters object and there is no way in Java to create such a cast. Your counters node that exists within the cRVh ... NHeT node is actually an array. When you try to read that array in code, it is read as an ArrayList, hence that error. As I see in your screenshot, that array contains Counters objects. Unfortunately, you cannot simply map that array of Counters objects to an ArrayList<Counters>. The counters node is actually an ArrayList of HashMaps. So if you need to have an ArrayList<Counters>, then you should iterate and create that List yourself. Besides that, the name of the properties in your Counters class do not match the names in the database. See, countername vs name and counternumber vs counter. All properties must match.
回答2:
You should not try to cast HashMap to ArrayList.
Though, you may try to get keys() or values() of the HashMap.
And you should post full exception log, not as image.
回答3:
I try to change
Arraylist
package com.example.smartappfirebase;
import java.util.ArrayList;
public class Device{
private String title;
private ArrayList<counters> counters;
public ArrayList<counters> getCounter() {
return counters;
}
public void setCounter(ArrayList<counters> counters) {
this.counters = counters;
}
public Device(){}
public Device(String title,ArrayList<counters> counters) {
this.title = title;
this.counters=counters;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Result is:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
来源:https://stackoverflow.com/questions/60409260/cant-convert-object-of-type-java-util-arraylist-read-from-firebase-realtim-datab