我有一个如下所示的迭代代码,用于我项目的不同位置:
List<NewsItem> thisitem = Select.from(NewsItem.class).where(Condition.prop("_id").eq(item.get_id())).list();
if (thisitem.size() > 0) {
thisitem.get(0).delete();
image_bookmark.setImageResource(R.drawable.ic_bookmark_normal);
} else {
item.save();
image_bookmark.setImageResource(R.drawable.ic_bookmarkfill);
}
我想编写一个包含上面代码的方法,但是对于类扩展SugarOrmItem
类的每个对象都有效。我写下面但显然不是真的:
public static void insert_bookmark(String prop,SugarOrmItem record ,ImageView imageView)
{
List<SugarOrmItem> thisitem = Select.from(SugarOrmItem.class).where(Condition.prop(prop).eq(record.get_id())).list();
if (thisitem.size() > 0) {
thisitem.get(0).delete();
imageView.setImageResource(R.drawable.ic_bookmark_normal);
} else {
imageView.setImageResource(R.drawable.ic_bookmarkfill);
}
}
我该怎么办?
修改:
这是我的SugarItem类:
import com.orm.SugarRecord;
public abstract class SugarOrmItem extends SugarRecord {
public abstract int get_id();
}
答案 0 :(得分:2)
我认为需要这样:
public static <T extends SugarOrmItem> void insert_bookmark(
String prop, T record, ImageView imageView, Class<T> clazz)
{
List<T> thisitem = Select.from(clazz).
where(Condition.prop(prop).eq(record.get_id())).list();
if (thisitem.size() > 0) {
thisitem.get(0).delete();
imageView.setImageResource(R.drawable.ic_bookmark_normal);
} else {
imageView.setImageResource(R.drawable.ic_bookmarkfill);
}
}