我正在尝试使用ReyclerView和CardView从FireBase检索数据。当我尝试运行系统时,每次都会崩溃。我认为这与数据类型问题有关。但是我找不到解决方案。 FirebaseDatabaseHelper.java
package com.example.second_msma;
import androidx.annotation.NonNull;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class FirebaseDatabaseHelper {
private FirebaseDatabase mDatabase;
private DatabaseReference mRefferencevessel;
private List<VesselBook> vessel = new ArrayList<>();
public interface DataStatus{
void DataIsLoaded(List<VesselBook> vessel, List<String> keys);
void DataIsInserted();
void DataIsUpdated();
void DataIsDeleted();
}
public FirebaseDatabaseHelper(){
mDatabase = FirebaseDatabase.getInstance();
mRefferencevessel = mDatabase.getReference("Bookingvessel");
}
public void readVesselBook(final DataStatus dataStatus){
mRefferencevessel.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
vessel.clear();
List<String> keys = new ArrayList<>();
for (DataSnapshot keyNode : dataSnapshot.getChildren()){
keys.add(keyNode.getKey());
VesselBook vesselBook = keyNode.getValue(VesselBook.class);
vessel.add(vesselBook);
}
dataStatus.DataIsLoaded(vessel,keys);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我总是在这里出现错误VesselBook vesselBook = keyNode.getValue(VesselBook.class);
这是我的VesselBook.class
package com.example.second_msma;
public class VesselBook {
private String date;
private String destination;
private String origin;
private String type;
private String weight;
private String id;
public VesselBook() {
}
public VesselBook(String destination, String origin, String type, String weight) {
this.destination = destination;
this.origin = origin;
this.type = type;
this.weight = weight;
}
public VesselBook(String destination, String origin, String type) {
this.destination = destination;
this.origin = origin;
this.type = type;
}
public VesselBook(String date, String destination, String origin, String type, String weight, String id) {
this.date = date;
this.destination = destination;
this.origin = origin;
this.type = type;
this.weight = weight;
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
这是我的RecyclerViewed_Config.java
package com.example.second_msma;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecyclerViewed_Config {
private Context mContext;
private BooksAdapter mBookAdapter;
public void setConfig(RecyclerView recyclerView, Context context, List<VesselBook> vesselBook, List<String> keys){
mContext = context;
mBookAdapter = new BooksAdapter(vesselBook,keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(mBookAdapter);
}
class BookItemView extends RecyclerView.ViewHolder{
private TextView mTitle;
private TextView mAuthor;
private TextView mISBN;
private TextView mCategory;
private String key;
public BookItemView(ViewGroup parent){
super(LayoutInflater.from(mContext).inflate(R.layout.user_list, parent, false));
mTitle = (TextView) itemView.findViewById(R.id.tvName);
mAuthor = (TextView) itemView.findViewById(R.id.tvPhone);
mCategory = (TextView) itemView.findViewById(R.id.tvAddress);
mISBN = (TextView) itemView.findViewById(R.id.tvISBN);
}
public void bind (VesselBook vesselBook, String key){
mTitle.setText(vesselBook.getOrigin());
mAuthor.setText(vesselBook.getDestination());
mCategory.setText(vesselBook.getWeight());
mISBN.setText(vesselBook.getType());
this.key = key;
}
}
class BooksAdapter extends RecyclerView.Adapter<BookItemView>{
private List<VesselBook> mBookList;
private List<String> mKeys;
public BooksAdapter(List<VesselBook> mBookList, List<String> mKeys) {
this.mBookList = mBookList;
this.mKeys = mKeys;
}
@NonNull
@Override
public BookItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new BookItemView(parent);
}
@Override
public void onBindViewHolder(@NonNull BookItemView holder, int position) {
holder.bind(mBookList.get(position), mKeys.get(position));
}
@Override
public int getItemCount() {
return mBookList.size();
}
}
}
这是将我的数据插入Firebase的地方
public class BookingVessel extends AppCompatActivity {
EditText etDate,weight;
DatabaseReference reff;
BookngGetter bookingvessel;
Button book;
FirebaseDatabase db;
int maxid =0;
Spinner mySpinner;
ArrayAdapter<String> myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.booking_vessel);
book = findViewById(R.id.book);
mySpinner = findViewById(R.id.spinner);
final Spinner mySpinner1 = findViewById(R.id.spinner2);
final Spinner mySpinner2 = findViewById(R.id.spinner3);
weight = findViewById(R.id.weight);
bookingvessel= new BookngGetter();
reff = db.getInstance().getReference().child("Bookingvessel");
myAdapter = new ArrayAdapter<String>(BookingVessel.this,
android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.origin) );
myAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mySpinner.setAdapter(myAdapter);
ArrayAdapter<String> myAdapter1 = new ArrayAdapter<String>(BookingVessel.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.destination));
myAdapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mySpinner1.setAdapter(myAdapter1);
ArrayAdapter<String> myAdapter2 = new ArrayAdapter<String>(BookingVessel.this,
android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.shipment));
myAdapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
mySpinner2.setAdapter(myAdapter2);
etDate = findViewById(R.id.et_date);
Calendar calendar = Calendar.getInstance();
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
etDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datePickerDialog = new DatePickerDialog(
BookingVessel.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month + 1;
String date = day + "/" + month + "/" + year;
etDate.setText(date);
}
}, year, month, day);
datePickerDialog.show();
}
});
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
maxid = (int) dataSnapshot.getChildrenCount();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
book.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String origin = mySpinner.getSelectedItem().toString().trim();
String destination = mySpinner1.getSelectedItem().toString().trim();
String type = mySpinner2.getSelectedItem().toString().trim();
String date = etDate.getText().toString().trim();
bookingvessel.setOrigin(mySpinner.getSelectedItem().toString());
bookingvessel.setDestination(destination);
bookingvessel.setDate(date);
bookingvessel.setType(type);
bookingvessel.setWeight(weight.getText().toString().trim());
Toast.makeText(BookingVessel.this,"Booking Accepted", Toast.LENGTH_LONG).show();
reff.child(String.valueOf(maxid+1)).setValue(bookingvessel);
openFirst();
}
});
}
public void openFirst(){
Intent intent = new Intent(this, First.class);
startActivity(intent);
}
}