如何将List <nameofclassobject>从activity传递给fragment

时间:2017-10-15 07:21:07

标签: android

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

}

这是我在主要活动中传递List&lt;&gt;的代码片段

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()中得到了第二个参数 - 错误的第二个参数类型。找到:&#39; java.util.List&#39;,required:&#39; java.util.ArrayList

如何解决这个错误?

3 个答案:

答案 0 :(得分:3)

在您的模型中实现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();

答案 1 :(得分:1)

  

错误的第二个参数类型。找到:&#39; java.util.List&#39;,必填:   &#39; java.util.ArrayList中

首先将Covert LIST 改为 ARRAYLIST

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

,然后

time-consuming and error-prone process

您应该使用Parcelable

AFAIK使用 Parcelable 优于可序列化

  • Parcelable public class Hospitals implements Parcelable {
  

可以写入和恢复其实例的类的接口   来自一个包裹。实现Parcelable接口的类也必须   有一个名为CREATOR的非空静态字段,它实现了一种类型   Parcelable.Creator界面。

Clean-Rebuild-Run

最后 {{1}} 。希望这会有所帮助。

答案 2 :(得分: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();