ListItem缺少一个没有参数的构造函数

时间:2017-03-01 21:02:19

标签: android database firebase firebase-realtime-database android-recyclerview

我遇到了问题。我正在尝试了解一下Firebase等。今天我制作了RecyclerView,它应该显示我的Firebase数据库的项目。但如果我运行它我会收到此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.andrejulia.painting, PID: 16777
              com.google.firebase.database.DatabaseException: Class com.example.andrejulia.painting.ListItem is missing a constructor with no arguments
                  at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
                  at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
                  at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
                  at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
                  at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.example.andrejulia.painting.RoomActivity$2.onChildChanged(RoomActivity.java:87)
                  at com.google.android.gms.internal.zzblz.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                  at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:158)
                  at android.app.ActivityThread.main(ActivityThread.java:7229)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我已经检查了五次以上,但即使在谷歌的帮助下我也无法解决这个问题所以我很难用我的代码回答有什么问题我得到了这个错误?

这是我的适配器:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

private List<ListItem> listItems;

public RecyclerAdapter(List<ListItem> listItems) {
    this.listItems = listItems;
}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.room_items, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(final RecyclerAdapter.ViewHolder holder, int position) {
    ListItem listItem = listItems.get(position);

    holder.text_desc.setText(listItem.desc);
    holder.text_Title.setText(listItem.head);

    holder.itemView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            menu.add(holder.getAdapterPosition(), 0, 0, "test");
            menu.add(holder.getAdapterPosition(), 1, 0, "test2");
        }
    });
}

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

public class ViewHolder extends RecyclerView.ViewHolder{

    TextView text_Title;
    TextView text_desc;

    public ViewHolder(View itemView) {
        super(itemView);

        text_Title = (TextView) itemView.findViewById(R.id.room_title);
        text_desc = (TextView) itemView.findViewById(R.id.room_describtion);
    }
}

这是我的ListItem类:

public class ListItem {

String head;
String desc;
String key;

public ListItem(String head, String desc, String key) {
    this.head = head;
    this.desc = desc;
    this.key = key;
}

最后是我的MainActivity

public class RoomActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private List<ListItem> result;

private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
private DatabaseReference mMainReference;


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

    mMainReference = mDatabase.getReference("Room");
    settingRecyclerView();


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fabCreateRoom);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    updateList();
}

private void settingRecyclerView(){
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    result = new ArrayList<>();



    mAdapter = new RecyclerAdapter(result);
    recyclerView.setAdapter(mAdapter);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case 0:
            break;
        case 1:
            break;
    }

    return super.onContextItemSelected(item);
}

private void updateList(){
    mMainReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            result.add(dataSnapshot.getValue(ListItem.class));
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            ListItem listItem = dataSnapshot.getValue(ListItem.class);

            int index = getItemIndex(listItem);
            result.set(index, listItem);
            mAdapter.notifyItemChanged(index);

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            ListItem listItem = dataSnapshot.getValue(ListItem.class);

            int index = getItemIndex(listItem);
            result.remove(index);
            mAdapter.notifyItemRemoved(index);
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

private int getItemIndex(ListItem list){
    int index = -1;

    for(int i = 0; i < result.size(); i++){
        if(result.get(i).key.equals(list.key)){
            index = i;
            break;
        }
    }
    return index;
}

我希望你们能帮助我。这是我测试的Firebase实时数据库JSON代码:

  {
  "Room" : [ null, {
    "desc" : "Test2",
    "head" : "Test1"
  }, {
    "desc" : "Test4",
    "head" : "Test3"
  } ]

如果有人可以帮助我,我会非常高兴我想谢谢花时间尝试解决这个问题。

1 个答案:

答案 0 :(得分:2)

正如错误消息所示,您缺少Firebase所需的空构造函数。

我建议你加一个:

public ListItem() {
    this("", "", "");
}