ActiveAndroid-含有容器的插入删除

最近在使用ActiveAndroid,发现他的确是很方便好用,不管是GitHub上关于他如何使用的介绍,还是大家乐此不疲的拷贝翻译“官方”的文档介绍而发表来介绍使用ActiveAndroid,我看到网络上百度一下,都是一大堆。这边我也像提出今天我碰到的一个问题,说出自己的一个想法,不对的地方也是希望大家能够指出,一起学习进步。废话少说,go:

package com.yzd.java;

import java.io.Serializable;
import java.util.List;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

@Table(name = "goodlists")
public class GoodList extends Model implements Serializable {

	private static final long serialVersionUID = -9120648745746063236L;
	@Column
	public List<Image> images;
	@Column
	public String l_content;
	@Column
	public int time_refund;
	@Column
	public String new_cat;
	@Column
	public int left_time;
	@Column
	public String lng;
	@Column
	public String l_price;
	@Column
	public String l_text;
	@Column
	public String product;
	@Column
	public String short_title;
	@Column
	public String distance;
	@Column
	public String is_new;
	@Column
	public String title;
	@Column
	public String price;
	@Column
	public String goods_id;
	@Column
	public String goods_type;
	@Column
	public String value;
	@Column
	public String is_sell_up;
	@Column
	public String seven_refund;
	@Column
	public int l_display;
	@Column
	public String bought;
	@Column
	public String lat;
	@Column
	public int is_appointment;
	
	@Column
	public String tab;

	public List<Image> getImage() {
		return getMany(Image.class, "goodlist");
	}

	public GoodList() {
	}

	public GoodList(String l_content, int time_refund, String new_cat,
			int left_time, String lng, String l_price, String l_text,
			String product, String short_title, String distance, String is_new,
			String title, String price, String goods_id, String goods_type,
			String value, String is_sell_up, String seven_refund,
			int l_display, String bought, String lat, int is_appointment,
			List<Image> images) {
		super();
		this.l_content = l_content;
		this.time_refund = time_refund;
		this.new_cat = new_cat;
		this.left_time = left_time;
		this.lng = lng;
		this.l_price = l_price;
		this.l_text = l_text;
		this.product = product;
		this.short_title = short_title;
		this.distance = distance;
		this.is_new = is_new;
		this.title = title;
		this.price = price;
		this.goods_id = goods_id;
		this.goods_type = goods_type;
		this.value = value;
		this.is_sell_up = is_sell_up;
		this.seven_refund = seven_refund;
		this.l_display = l_display;
		this.bought = bought;
		this.lat = lat;
		this.is_appointment = is_appointment;
		this.images = images;
	}

}

上面是我是一个类 GoodList 继承了Model  但是发现里面含有一个List<Image>,但是官方的文档或者大家都没有在提到该如何将含有容器的对象插入到数据库,我是这么做得:

package com.yzd.java;

import java.io.Serializable;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

@Table(name = "imageurl")
public class Image extends Model implements Serializable {

	private static final long serialVersionUID = -5742482117802989296L;
	
	@Column(name = "Name")
	public String image;
	@Column
	public int width;
	@Column
	public String goods_id;
	@Column
	public String tab;

	@Column(name = "goodlist")
	public GoodList goodlist;

	public Image() {
	}

	public Image(String image, int width, String goods_id, GoodList goodlist) {
		super();
		this.image = image;
		this.width = width;
		this.goods_id = goods_id;
		this.goodlist = goodlist;
	}
}
1.上面这个类就是Image  里面定义一个GoodList  与你需要的目标GoodLi这个类相同。
2.开始写插入代码
goodlist.save();
		for (int i = 0; i < goodlist.images.size(); i++) {
			Image image = new Image();
			image.image = goodlist.images.get(i).image;
			image.width = goodlist.images.get(i).width;
			image.goods_id = goodlist.goods_id;
			image.tab = tab;
			image.goodlist = goodlist; //
			image.save();
		}
先保存主表,然后循环看下主表的size,创建出Image,在一个一个的将数据传过去,然后在将image保存,这样就做到了一对多的保存。

做完了保存,下面就是删除:

new Delete().from(Image.class)
				.where("tab=? and goods_id=?", new Object[] { tab, goods_id })
				.execute();
		new Delete().from(GoodList.class)
				.where("tab=? and goods_id=?", new Object[] { tab, goods_id })
				.execute();

还有就是查询:

public static List<GoodList> query(String tab) {
		List<GoodList> goodLists = new Select().from(GoodList.class)
				.where("tab=?", tab).execute();
		for (int i = 0; i < goodLists.size(); i++) {
			GoodList goodList = goodLists.get(i);
			goodList.images = goodList.getImage();
		}
		return goodLists;
	}

不对的地方希望大家指出。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值