如何传递List< NameOfClassObject>从活动到碎片 [英] how to pass List<NameOfClassObject> from activity to fragment

查看:51
本文介绍了如何传递List< NameOfClassObject>从活动到碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,用于使用databasehelper类从数据库检索数据

here is my code for retrieving data from database using databasehelper class

 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<>传递给片段

and here is my code in main activity to pass List<> to 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

I got 2nd arguments in putParcelableArrayList() - Wrong 2nd argument type. Found: 'java.util.List', required: 'java.util.ArrayList

如何解决该错误?

推荐答案

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

Wrong 2nd argument type. Found: 'java.util.List', required: 'java.util.ArrayList

首先将Covert LIST 转换为 ARRAYLIST .

At first Covert LIST to 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 更好.

AFAIK Using Parcelable better than Serializable .

  • 可拆分 耗时且容易出错的过程 .

其实例可以写入和还原的类的接口从包裹中.实现Parcelable接口的类还必须具有一个称为CREATOR的非空静态字段,该字段的实现类型Parcelable.Creator界面.

Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a non-null static field called CREATOR of a type that implements the Parcelable.Creator interface.

public class Hospitals implements Parcelable {

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

Finally Clean-Rebuild-Run . Hope this helps .

这篇关于如何传递List&lt; NameOfClassObject&gt;从活动到碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆