Пример #1
0
  /**
   * 查找某一栏目下的所有博客,这里只会存上第一页的博客,因为在加载的时候,并没有存库
   *
   * @param blogType
   * @return
   */
  public synchronized List<BlogItem> loadBlog(int blogType) {
    List<BlogItem> blogs = new ArrayList<>();
    BlogItem item;
    db = helper.getReadableDatabase();

    Cursor cursor =
        db.query(
            DBInfo.Table.BLOG_TABLE_NAME,
            null,
            BlogItem.BLOGTYPE + "= ?",
            new String[] {blogType + ""},
            null,
            null,
            null);
    while (cursor.moveToNext()) { // 第一步就直接转向第一条记录,为空则退出
      item = new BlogItem();
      item.setTitle(cursor.getString(cursor.getColumnIndex(BlogItem.TITLE)));
      item.setContent(cursor.getString(cursor.getColumnIndex(BlogItem.CONTENT)));
      item.setDate(cursor.getString(cursor.getColumnIndex(BlogItem.DATE)));
      item.setImgLink(cursor.getString(cursor.getColumnIndex(BlogItem.IMG)));
      item.setLink(cursor.getString(cursor.getColumnIndex(BlogItem.LINK)));
      item.setBlogType(blogType);

      blogs.add(item);
    }
    cursor.close();
    db.close();
    return blogs;
  }
Пример #2
0
  /**
   * 插入某一个类别的所有博客
   *
   * @param list
   */
  public synchronized void insert(List<BlogItem> list) {
    db = helper.getWritableDatabase();
    values.clear(); // 清空values

    if (list == null) {
      LogUtil.i("service", "empty");
    }
    for (BlogItem item : list) { // 老规矩,不添加主键
      values.put(BlogItem.TITLE, item.getTitle());
      values.put(BlogItem.CONTENT, item.getContent());
      values.put(BlogItem.DATE, item.getDate());
      values.put(BlogItem.IMG, item.getImgLink());
      values.put(BlogItem.LINK, item.getLink());
      values.put(BlogItem.BLOGTYPE, item.getBlogType());

      db.insert(DBInfo.Table.BLOG_TABLE_NAME, null, values);
    }
    db.close();
  }