【问题标题】:how to pass List<NameOfClassObject> from activity to fragment如何将 List<NameOfClassObject> 从活动传递到片段
【发布时间】:2018-03-26 22:23:40
【问题描述】:

这是我使用 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 传递给片段的代码

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

如何解决这个错误?

【问题讨论】:

    标签: android


    【解决方案1】:

    第二个参数类型错误。找到:'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

    AFAIK 使用 Parcelable 优于 Serializable

    • Parcelabletime-consuming and error-prone process

    实例可以写入和恢复的类的接口 从一个包裹。实现 Parcelable 接口的类也必须 有一个名为 CREATOR 的非空静态字段,该字段实现 Parcelable.Creator 接口。

    public class Hospitals implements Parcelable {
    

    最后 Clean-Rebuild-Run 。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

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

      并将可序列化放入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();
      

      【讨论】:

      • 添加implements Serializable
      • @IntelliJAmiya 谢谢
      • List 不起作用,需要 ArrayList。不是吗?
      【解决方案3】:

      你的类应该首先实现 Parcelable :

      public class Hospitals implement Parcelable {
      

      然后实现Parcelable的方法。

      要将列表传递给片段,请在您的活动中编写此代码,即只需将结果转换为ArrayList&lt;Hospitals&gt;

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多