com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为com.example.blueweb.Blueweb类型

问题描述

public void onChildAdded(@NonNull DataSnapshot snapshot,@Nullable String prevIoUsChildName) {
    Blueweb obj4 = snapshot.getValue(Blueweb.class);
    adapter.add(obj4);
}

第二行出现错误。告诉我这段代码有什么问题以及如何解决

MyAdapterCode:

public class BluewebAdapter extends ArrayAdapter<Blueweb> {
    public BluewebAdapter(@NonNull Context context,int resource,List<Blueweb> bluewebList) {
        super(context,resource);
    }

    @NonNull
    @Override
    public View getView(int position,@Nullable View convertView,@NonNull ViewGroup parent) {
        if(convertView ==null){
            convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.message_item,parent,false);
        }
        ImageView photoImageView = convertView.findViewById(R.id.photoImageView);
        TextView textTextView = convertView.findViewById(R.id.textTextView);
        TextView nameTextView = convertView.findViewById(R.id.nameTextView);

        Blueweb obj = getItem(position);
        boolean isText = obj.getimageURl()==null;
        if(isText){
            textTextView.setVisibility(View.VISIBLE);
            photoImageView.setVisibility(View.INVISIBLE);
            nameTextView.setText(obj.getText());
        }
        else{
            textTextView.setVisibility(View.GONE);
            photoImageView.setVisibility(View.VISIBLE);
            Glide.with(photoImageView.getContext()).
                    load(obj.getimageURl()).
                    into(photoImageView);
        }
        nameTextView.setText(obj.getName());
                return convertView;
    }
}

解决方法

snapshot.getValue(Blueweb.class)

返回您尝试转换为Blueweb类型的字符串类型

请尝试以下代码:

String obj4 = snapshot.getValue(String.class);