这是我使用DatabaseHelper类从数据库检索数据的代码:
public List<Hospitals> getHospitals(Context context){
Hospitals hospitals = null;
List<Hospitals> hospitalList = new ArrayList<>();
openDatabase(context);
Cursor cursor = database.rawQuery("SELECT * FROM buildings WHERE category_id = 1", null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
hospitals = new Hospitals(cursor.getInt(0), cursor.getString(1), cursor.getFloat(2), cursor.getFloat(4));
hospitalList.add(hospitals);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return hospitalList;
}
这是我的医院类
public class Hospitals {
private int id;
private String name;
private Float latitude;
private Float longhitude;
public Hospitals(int id, String name, Float latitude, Float longhitude ){
this.id = id;
this.name = name;
this.latitude = latitude;
this.longhitude = longhitude;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getLatitude() {
return latitude;
}
public void setLatitude(Float latitude) {
this.latitude = latitude;
}
public Float getLonghitude() {
return longhitude;
}
public void setLonghitude(Float longhitude) {
this.longhitude = longhitude;
}
以下是我在MainActivity中将List<>传递给Fragment的代码:
List<Hospitals> result = databaseHelper.getHospitals(this);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("valuesArray", result);
GmapFragment gmapFragment = new GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout, gmapFragment).commit();
我在putParcelableArrayList()方法的第二个参数中遇到了错误 - 错误的第二个参数类型。发现:'java.util.List',需要:'java.util.ArrayList'
如何解决这个错误?
implements Serializable
。 - IntelliJ Amiya