如何从Activity传递List <NameOfClassObject> 到Fragment

7

这是我使用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'

如何解决这个错误?

3个回答

7

错误的第二个参数类型。找到:'java.util.List',需要: 'java.util.ArrayList'

首先将LIST转换为ARRAYLIST

 List<Hospitals> result = databaseHelper.getHospitals(this);
 ArrayList<Hospitals> al_HOSPITAL = new ArrayList<>(result.size());
 al_HOSPITAL.addAll(result);

然后

Bundle bundle = new Bundle();
bundle.putSerializable("valuesArray", al_HOSPITAL);

您应该使用Parcelable

据我所知,使用Parcelable比使用Serializable更好。

  • Parcelable是一个耗时且容易出错的过程

接口用于可以被写入和从Parcel中恢复的类。实现Parcelable接口的类还必须有一个非空静态字段,该字段称为Parcelable.Creator接口实现类型的CREATOR。

public class Hospitals implements Parcelable {

最后,进行清理-重建-运行操作。希望这可以帮到你。

5

像这样在您的模型中实现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;
    }
  ....
}

并将可序列化对象放入 bundle 中:

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

添加 implements Serializable - IntelliJ Amiya
1
@IntelliJAmiya 谢谢您。 - FarshidABZ
2
List<Hospitals> 不起作用,需要 ArrayList<Hospitals>。是吗? - Zia Ur Rahman

0

你的类应该首先实现Parcelable接口:

public class Hospitals implement Parcelable {

然后实现 Parcelable 的方法。

为了将列表传递给片段,在您的活动中编写以下代码,即将结果简单地转换为 ArrayList<Hospitals>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接