无法将ArrayList类型的Object转换为Pill类型

时间:2018-11-26 18:59:53

标签: java android firebase firebase-realtime-database

因此,我正在尝试开发一个应用,以允许您使用火库数据库记录不同的药物。启动应用程序时,我目前收到此错误。

11-26 13:48:37.056 6958-6958/com.example.andrew.pilltracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrew.pilltracker, PID: 6958
com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.example.andrew.pilltracker.Pill
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.4:423)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.4:214)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.4:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.4:212)
    at com.example.andrew.pilltracker.MainActivityController.showData(MainActivityController.java:86)
    at com.example.andrew.pilltracker.MainActivityController.access$100(MainActivityController.java:21)
    at com.example.andrew.pilltracker.MainActivityController$2.onDataChange(MainActivityController.java:71)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)`

我不知道这是什么交易。我试图让主控制器从数据库中导入有关药丸的信息,另一个活动为药丸创建数据库,但是我不知道如何将参考数据从第二个活动转移到主要活动。我也不知道为什么会收到此错误。在下面,我附加了主控制器和药丸类。感谢您的帮助!

package com.example.andrew.pilltracker;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;

public class MainActivityController extends AppCompatActivity {

private TextView mTextMessage;
private int databaseRef = 1;
private ListView mListView;

private FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRootRef;
ArrayList<String> array = new ArrayList<>();



private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;
            case R.id.navigation_dashboard:
                Intent intent = new Intent(MainActivityController.this, SecondActvty.class);
                startActivity(intent);
                return true;
            case R.id.navigation_notifications:
                mTextMessage.setText(R.string.title_notifications);
                return true;
        }
        return false;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_view);

    mListView = (ListView) findViewById(R.id.listview);

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mRootRef = mFirebaseDatabase.getReference();

    mTextMessage = (TextView) findViewById(R.id.message);
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    mRootRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
           showData(dataSnapshot);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

private void showData(DataSnapshot dataSnapshot){
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        Pill pInfo = new Pill();

        pInfo.setName(ds.getValue(Pill.class).getName());
        pInfo.setCount(ds.child(Integer.toString(databaseRef)).getValue(Pill.class).getCount());
        pInfo.setDose(ds.child(Integer.toString(databaseRef)).getValue(Pill.class).getDose());
        pInfo.setDay(ds.child(Integer.toString(databaseRef)).getValue(Pill.class).getDay());

        array.add(pInfo.getName());
        array.add("Number of Pills Left: " + pInfo.getCount());
        array.add("Number of Pills per Dose: " + pInfo.getDose());
        array.add("Number of Doses per Day: " + pInfo.getDay());
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array);
        mListView.setAdapter(adapter);

    }
}

//Database shit
@Override
protected void onStart(){
    super.onStart();


}

}

0 个答案:

没有答案