Android 如何通过列表<;类别对象的名称>;从活动到碎片
下面是我使用databasehelper类从数据库检索数据的代码Android 如何通过列表<;类别对象的名称>;从活动到碎片,android,Android,下面是我使用databasehelper类从数据库检索数据的代码 public List<Hospitals> getHospitals(Context context){ Hospitals hospitals = null; List<Hospitals> hospitalList = new ArrayList<>(); openDatabase(context); Cursor cursor = database.ra
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;
}
}
这是我在主活动中的代码,用于将列表传递给片段
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();
List result=databaseHelper.getHospitals(this);
Bundle=新Bundle();
bundle.putParcelableArrayList(“valuesArray”,结果);
GmapFragment=新的GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout,gmapFragment.commit();
putParcelableArrayList()中有第二个参数-第二个参数类型错误。找到:“java.util.List”,必需:“java.util.ArrayList”
如何解决该错误?在您的模型中实现Serializable,如下所示:
public class Hospitals implements Serializable {
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;
}
....
}
并将serializable放入bunle中:
List<Hospitals> result = databaseHelper.getHospitals(this);
Bundle bundle = new Bundle();
bundle.putSerializable("valuesArray", result);
GmapFragment gmapFragment = new GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout, gmapFragment).commit();
List result=databaseHelper.getHospitals(this);
Bundle=新Bundle();
bundle.putSerializable(“valuesArray”,结果);
GmapFragment=新的GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout,gmapFragment.commit();
错误的第二个参数类型。找到:“java.util.List”,必需:
'java.util.ArrayList
首先,将列表转换为数组列表
List<Hospitals> result = databaseHelper.getHospitals(this);
ArrayList<Hospitals> al_HOSPITAL = new ArrayList<>(result.size());
al_HOSPITAL.addAll(result);
你应该使用
AFAIK使用可打包优于可序列化
- 可包裹是一个耗时且容易出错的过程
最后,
清理重建运行
。希望这能有所帮助。您的班级应该首先实现Parcelable:
public class Hospitals implement Parcelable {
然后实现Parcelable
的方法
要将列表传递到片段,请在活动中编写此代码,即只需将结果强制转换到ArrayList
:
List result=databaseHelper.getHospitals(this);
Bundle=新Bundle();
bundle.putParcelableArrayList(“valuesArray”,(ArrayList)结果);
GmapFragment=新的GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout,gmapFragment.commit();
addimplements Serializable
@IntelliJAmiya感谢列表不工作,需要ArrayList。不是吗?
Bundle bundle = new Bundle();
bundle.putSerializable("valuesArray", al_HOSPITAL);
public class Hospitals implements Parcelable {
public class Hospitals implement Parcelable {
List<Hospitals> result = databaseHelper.getHospitals(this);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("valuesArray", (ArrayList<Hospitals>)result);
GmapFragment gmapFragment = new GmapFragment();
gmapFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.mainLayout, gmapFragment).commit();