com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.blueweb.Blueweb

huangapple 未分类评论70阅读模式
英文:

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type 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;
    }
}
英文:
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
    Blueweb obj4 = snapshot.getValue(Blueweb.class);
    adapter.add(obj4);
}

I'm getting Error in this second line.. tell me what's wrong in this code and how to solve it.

MyAdapterCode:

public class BluewebAdapter extends ArrayAdapter&lt;Blueweb&gt; {
    public BluewebAdapter(@NonNull Context context, int resource, List&lt;Blueweb&gt; 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;
    }
}

答案1

得分: 0

snapshot.getValue(Blueweb.class)

返回您尝试转换为Blueweb类型的String类型

尝试以下代码:

String obj4 = snapshot.getValue(String.class);
英文:
snapshot.getValue(Blueweb.class)

returns String type that you try to convert to Blueweb type

Try code below:

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

huangapple
  • 本文由 发表于 2020年8月14日 19:22:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63411817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定