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;
}
And Here is my Class Hospital
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;
}
}
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();
I got 2nd arguments in putParcelableArrayList() - Wrong 2nd argument type. Found: 'java.util.List', required: 'java.util.ArrayList
how to solve that error?
Wrong 2nd argument type. Found: 'java.util.List', required: 'java.util.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);
Then
Bundle bundle = new Bundle();
bundle.putSerializable("valuesArray", al_HOSPITAL);
You should use Parcelable
.
AFAIK Using Parcelable better than Serializable .
time-consuming and error-prone process
.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 {
Finally Clean-Rebuild-Run
. Hope this helps .
Implement Serializable in your model like this:
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;
}
....
}
and put serializable into 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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With