我想在sqlite数据库中保存自定义arraylist对象,当我在互联网上搜索它时,我知道我需要让我的课程序列化。但问题是我的那个班级是 Parcelable 而我是在 Parcelable 的帮助下将值从一个活动发送到另一个活动。
Medicine.class
public class Medicine implements Parcelable {
// Private Variables
int _id;
String _product_Code;
String _product_name;
String _type;
int _price;
int _quantity;
//empty constructor
public Medicine(){
}
//constructor
public Medicine(int id, String product_code,String product_name, String type, int price, int quantity){
this._id = id;
this._product_Code = product_code;
this._product_name = product_name;
this._type = type;
this._price = price;
this._quantity = quantity;
}
public Medicine(String product_code,String product_name, String type, int price, int quantity){
this._product_Code = product_code;
this._product_name = product_name;
this._type = type;
this._price = price;
this._quantity = quantity;
}
public String get_product_Code() {
return _product_Code;
}
public void set_product_Code(String _product_Code) {
this._product_Code = _product_Code;
}
public Medicine(Parcel p){
set_id(p.readInt());
set_product_name(p.readString());
set_type(p.readString());
set_price(p.readInt());
set_quantity(p.readInt());
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String get_product_name() {
return _product_name;
}
public void set_product_name(String _product_name) {
this._product_name = _product_name;
}
public String get_type() {
return _type;
}
public void set_type(String _type) {
this._type = _type;
}
public int get_price() {
return _price;
}
public void set_price(int _price) {
this._price = _price;
}
public int get_quantity() {
return _quantity;
}
public void set_quantity(int _quantity) {
this._quantity = _quantity;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(get_id());
dest.writeString(get_product_name());
dest.writeString(get_type());
dest.writeInt(get_price());
dest.writeInt(get_quantity());
}
public static final Parcelable.Creator<Medicine> CREATOR = new Creator<Medicine>() {
public Medicine createFromParcel(Parcel source) {
return new Medicine(source);
}
public Medicine[] newArray(int size) {
return new Medicine[size];
}
};
}
在此类 ShowMedicineList i中,将值保存在名为“nextValuesOf”的arraylist中,并传递给下一个活动 SelectedMedicineList
Button btnAddMedicine = (Button) findViewById(R.id.buttonAddSelectedMed);
btnAddMedicine.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ShowMedicineList.this, SelectedMedicineList.class);
//intent.putExtra("ccode", o._product_name);
//intent.putExtra("cname", o._quantity);
//intent.putExtra("caddress", amount);
intent.putParcelableArrayListExtra("myArrayL", nextValuesOf);
startActivity(intent);
}
});
这是 SelectedMedicineList类的onCreate方法,其中i,m接收意图。现在我想在我的数据库中使用 selectedProducts arraylist。请告诉我解决方案或代码
public ArrayList<Medicine> selectedProducts = new ArrayList<Medicine>();
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_medicine_info);
selectedProducts = getIntent().getParcelableArrayListExtra("myArrayL");
LinkedHashSet<Medicine> hs = new LinkedHashSet<Medicine>();
hs.addAll(selectedProducts);
selectedProducts.clear();
selectedProducts.addAll(hs);
}
答案 0 :(得分:1)
您可以使用Gson库来保存和检索任何数据结构中的数据。想法是库将对象转换(序列化和反序列化)为JSON
格式
因此,创建一个字段类型为TEXT
的数据库列,并将您的arraylist转换为JSON对象
Gson gson = new Gson();
String arrayList = gson.toJson(yourArraylist);
并将此字符串保存到数据库中。
并且在检索时从数据库中取一个字符串并将其转换为原始列表
Type listType = new TypeToken<ArrayList<YourClass>>() {
}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);
希望这个帮助