无法转换类型为java.lang.String的对象

时间:2018-07-03 14:20:50

标签: java android firebase

所以我在Firebase中使用了RecyclerView,我得到了这个错误代码

  

07-03 21:12:07.669 7153-7153 / pipapp.com.pipile E / AndroidRuntime:FATAL   例外:主要       程序:pipapp.com.pipile,PID:7153       com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为pipapp.com.pipile.Model.Item类型           com.google.android.gms.internal.zzear.zzb(未知来源:259)           位于com.google.android.gms.internal.zzear.zza(未知来源:0)           位于com.google.firebase.database.DataSnapshot.getValue(未知来源:10)           在pipapp.com.pipile.MainActivity $ 1.onDataChange(MainActivity.java:93)           在com.google.android.gms.internal.zzduz.zza(未知来源:13)           在com.google.android.gms.internal.zzdwu.zzbvb(未知来源:2)           在com.google.android.gms.internal.zzdxa.run(未知来源:71)           在android.os.Handler.handleCallback(Handler.java:873)           在android.os.Handler.dispatchMessage(Handler.java:99)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我的MainActivity.java

mDatabase.child("User").child(uid).child("Database").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot Snapshot) {
                list.clear();

                for (DataSnapshot dataSnapshot : Snapshot.getChildren()) {
                    Item item = dataSnapshot.getValue(Item.class);


                    list.add(item);
                }
                Collections.sort(list, (l1, l2) -> l1.getNama().compareTo(l2.getNama())); //buat sort alphabetically
                itemAdapter = new ItemAdapter(MainActivity.this, list);
                recyclerView.setAdapter(itemAdapter);
                progressDialog.dismiss();
            }

我的Item.java

public class Item {
    private String nama, harga, stok, satuan, limit;

    public Item() {

    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public String getHarga() {
        return harga;
    }

    public void setHarga(String harga) {
        this.harga = harga;
    }

    public String getStok() {
        return stok;
    }

    public void setStok(String stok) {
        this.stok = stok;
    }

    public String getSatuan() {
        return satuan;
    }

    public void setSatuan(String satuan) {
        this.satuan = satuan;
    }

    public String getLimit() {
        return limit;
    }

    public void setLimit(String limit) {
        this.limit = limit;
    }
}

和我的ItemAdapter.java

package pipapp.com.pipile.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import pipapp.com.pipile.Menu.MainHoldMenu;
import pipapp.com.pipile.Menu.MainTouchMenu;
import pipapp.com.pipile.Model.Item;
import pipapp.com.pipile.R;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> implements Filterable {

    Context context;
    List<Item> itemList;
    List<Item> itemTempList;
    int limit, stok;

    public ItemAdapter(Context context, List<Item> TempList) {
        this.itemList = TempList;
        this.context = context;

    }


    @NonNull
    @Override
    public ItemAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_row, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemAdapter.ViewHolder holder, int position) {
        Item item = itemList.get(position);
        holder.qty.setText(item.getStok());
        holder.nama.setText(item.getNama());
        holder.harga.setText("Rp. " + item.getHarga());
        holder.satuan.setText(item.getSatuan());

        limit = Integer.parseInt(item.getLimit());
        stok = Integer.valueOf(item.getStok());

        int statuscolor = 0;
        if (stok <= limit) {
            statuscolor = Color.parseColor("#f44242");
        }
        holder.onklik.setBackgroundColor(statuscolor);

        holder.onklik.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, MainTouchMenu.class);
                i.putExtra("nama", item.getNama().toString());
                i.putExtra("stok", item.getStok().toString());
                i.putExtra("harga", item.getHarga().toString());
                i.putExtra("satuan", item.getSatuan().toString());
                i.putExtra("limit", item.getLimit().toString());
                context.startActivity(i);
            }
        });


        holder.onklik.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Intent i = new Intent(context, MainHoldMenu.class);
                i.putExtra("nama", item.getNama().toString());
                i.putExtra("stok", item.getStok().toString());
                i.putExtra("harga", item.getHarga().toString());
                i.putExtra("satuan", item.getSatuan().toString());
                i.putExtra("limit", item.getLimit().toString());
                context.startActivity(i);
                return false;
            }
        });


    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final List<Item> results = new ArrayList<Item>();
                if (itemTempList == null) {
                    itemTempList = itemList;
                }
                if (constraint != null) {
                    if (itemTempList != null & itemTempList.size() > 0) {
                        for (final Item item : itemTempList) {
                            if (item.getNama().toLowerCase().contains(constraint.toString())) {
                                results.add(item);
                            }
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                itemList = (List<Item>) results.values;
                notifyDataSetChanged();

            }
        };
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView nama, harga, qty, satuan;
        RelativeLayout onklik;

        public ViewHolder(View itemView) {
            super(itemView);
            nama = itemView.findViewById(R.id.nama);
            harga = itemView.findViewById(R.id.harga);
            qty = itemView.findViewById(R.id.qty);
            onklik = itemView.findViewById(R.id.onklik);
            satuan = itemView.findViewById(R.id.satuan);
        }
    }
}

这是Firebase结构

{
  "User" : {
    "NFZmA3cypLZproSk5yE2hEu1dx43" : {
      "Database" : {
        "harga" : "123123",
        "limit" : "123123",
        "nama" : "asdasd",
        "satuan" : "asd",
        "stok" : "12312"
      },
      "username" : "test"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:-

mDatabase.child("User").child(uid).child("Database").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot Snapshot) {
                list.clear();

                    Item item = dataSnapshot.getValue(Item.class);

                    list.add(item);

                Collections.sort(list, (l1, l2) -> l1.getNama().compareTo(l2.getNama())); //buat sort alphabetically
                itemAdapter = new ItemAdapter(MainActivity.this, list);
                recyclerView.setAdapter(itemAdapter);
                progressDialog.dismiss();
            }