Julia Falinska Julia Falinska -4 years ago 184
Android Question

ListItem is missing a constructor with no arguments

I got a problem. I am trying to learn a bit about Firebase etc. Today I made RecyclerView which should show Items of my Firebase Database. But if i run it i get this error:

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)


I already checked it more than five times but i couldnt solve it not even with the help of Google so I poorly need to answer What is wrong with my code that i get this error?

Here is my Adapter:

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);
}
}


Here is my ListItem class:

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;
}


And finally my 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;
}


I hope u guys can help me if it matters here is my test JSON Code of Firebase Realtime Database:

{


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

I would be very happy if someone can help me and i wanna thank you for taking the time and try to solve this problem !

Answer Source

As the error message says, you are missing an empty constructor needed by Firebase.

I suggest you add one:

public ListItem() {
    this("", "", "");
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download