标签: android网易评论盖楼效果
2014-11-15 10:32
7035人阅读
收藏
举报
上一篇文章我们分析了网易评论列表的界面效果,有了一个大概的了解后我们来为我们的评论列表做些数据,为了方便起见,就不通过服务器来做了,我们直接用SQLite来模拟一个简单的本地服务器数据库~~。经过我们前面的分析得出,我们的数据库应该有这样的三个表:
- User—>用来存放用户的基本信息,比如用户名、用户头像和用户的地理位置等
- Post—>用来存放帖子的基本信息,比如帖子的创建时间等
- Comment—>用来存放评论的基本信息,比如评论的内容等
在实际项目中我们可以通过服务器后台的一些配置来实现三者之间的关联以方便使用,但是因为我们是用的SQLite且数据库置于本地,我们就使用最原始的办法,为表之间的关联单独创建一个关联表:
Post_Comment—>用来存储Post和User之间的关联
User_Praise—>用来存储用户和点赞帖子之间的关联
User_UnPraise—>用来存储用户和被踩帖子之间的关联
User_Collect—>用来存储用户和收藏帖子之间的关联
有了这样的一个关联后我们还需要一个唯一的标识值来作为表之间关联的标识,在Android中SQLite有主键_id,这个主键字段我们不能随意更改否则在进行Cursor查询的时候会出错,而且_id是自增长的Integer类型,其在每个表中都是独立存在的,在我们进行标识的时候就有可能重复,鉴于此我们分别为User、Post和Comment表添加一个额外的flag字段,该字段的值我们在Android中使用UUID随机生成。User、Post和Comment的大致关系我在PD中花了两分钟粗略画了下(数据库不是本节重点)如下图:
PS:User和Post的多对多关系不会在实例中实现,但是会有体现
关联表的模型就不给出了,下面我们来看看SQLite中构建表的SQL语句:
User表:
- create table user(_id integer primary key autoincrement, flag varchar(16), userName varchar(16), nick integer, location varchar(32))
Post表:
- create table post(_id integer primary key autoincrement, flag varchar(16), createAt varchar(32))
Comment表:
- create table comment(_id integer primary key autoincrement, flag varchar(16), userFlag varchar(16), content varchar(1024), createAt varchar(32))
Post_Comment:
- create table post_comment(_id integer primary key autoincrement, postFlag varchar(16), commentFlag varchar(16))
User_Praise:
- create table user_praise(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))
User_UnPraise:
- create table user_unpraise(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))
User_Collect:
- create table user_collect(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))
继承SQLiteOpenHelper类打开数据库建表:
- package com.aigestudio.neteasecommentlistdemo.helper;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
-
-
-
-
-
-
-
- public class ServerDBHelper extends SQLiteOpenHelper {
- private static final String DB_NAME = "server.db";
- private static final int VERSION = 1;
-
-
-
-
- private static final String CREATE_TABLE_USER_SQL = "create table user(_id integer primary key autoincrement, flag varchar(16), userName varchar(16), nick integer, location varchar(32))";
- private static final String CREATE_TABLE_POST_SQL = "create table post(_id integer primary key autoincrement, flag varchar(16), createAt varchar(32))";
- private static final String CREATE_TABLE_COMMENT_SQL = "create table comment(_id integer primary key autoincrement, flag varchar(16), userFlag varchar(16), content varchar(1024), createAt varchar(32))";
-
-
-
-
- private static final String CREATE_TABLE_POST_AND_COMMENT_SQL = "create table post_comment(_id integer primary key autoincrement, postFlag varchar(16), commentFlag varchar(16))";
- private static final String CREATE_TABLE_USER_AND_PRAISE_SQL = "create table user_praise(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))";
- private static final String CREATE_TABLE_USER_AND_UNPRAISE_SQL = "create table user_unpraise(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))";
- private static final String CREATE_TABLE_USER_AND_COLLECT_SQL = "create table user_collect(_id integer primary key autoincrement, userFlag varchar(16), postFlag varchar(16))";
-
- public ServerDBHelper(Context context) {
- super(context, DB_NAME, null, VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_TABLE_USER_SQL);
- db.execSQL(CREATE_TABLE_POST_SQL);
- db.execSQL(CREATE_TABLE_COMMENT_SQL);
- db.execSQL(CREATE_TABLE_POST_AND_COMMENT_SQL);
- db.execSQL(CREATE_TABLE_USER_AND_PRAISE_SQL);
- db.execSQL(CREATE_TABLE_USER_AND_UNPRAISE_SQL);
- db.execSQL(CREATE_TABLE_USER_AND_COLLECT_SQL);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
- }
- }
所有对服务器数据库操作的方法都会先封装在接口IServerDAO.java中:
- package com.aigestudio.neteasecommentlistdemo.dao;
-
- import android.content.ContentValues;
-
- import java.util.List;
- import java.util.Map;
-
-
-
-
-
-
-
- public interface IServerDAO extends IDAO {
-
-
-
-
-
-
-
- public boolean add(String table, ContentValues values);
-
-
-
-
-
-
-
- public boolean add(String sql);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public List
-
-
-
-
-
-
-
- public List
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Map querySingle(String table, String[] columns, String flag, String groupBy, String having, String orderBy, String limit);
-
-
-
-
-
-
-
- public Map querySingle(String sql);
-
-
-
-
-
-
-
-
-
-
- public String queryValue(String table, String[] columns, String key, String value);
-
-
-
-
-
-
-
- public String queryValue(String sql);
- }
具体实现在ServerDAO.java:
- package com.aigestudio.neteasecommentlistdemo.dao;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
-
- import com.aigestudio.neteasecommentlistdemo.helper.ServerDBHelper;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
-
-
-
-
-
-
- public class ServerDAO implements IServerDAO {
- private ServerDBHelper dbHelper;
-
- public ServerDAO(Context context) {
- dbHelper = new ServerDBHelper(context);
- }
-
- @Override
- public boolean add(String table, ContentValues values) {
- boolean flag = false;
- SQLiteDatabase db = null;
- try {
- db = dbHelper.getWritableDatabase();
- long id = db.insert(table, null, values);
- flag = (id != -1 ? true : false);
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- return flag;
- }
-
- @Override
- public boolean add(String sql) {
-
- return false;
- }
-
- @Override
- public List
- List
- SQLiteDatabase db = null;
- Cursor cursor;
- try {
- db = dbHelper.getWritableDatabase();
- cursor = db.query(table, columns, whereClause, selectionArgs, groupBy, having, orderBy, limit);
- while (cursor.moveToNext()) {
- Map map = new HashMap();
- for (int i = 0; i < cursor.getColumnCount(); i++) {
- String columnName = cursor.getColumnName(i);
- String columnValue = cursor.getString(cursor.getColumnIndex(columnName));
- map.put(columnName, columnValue);
- }
- list.add(map);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- return list;
- }
-
- @Override
- public List> queryMulti(String sql) {
- List> list = new ArrayList>();
- SQLiteDatabase db = null;
- Cursor cursor;
- try {
- db = dbHelper.getWritableDatabase();
- cursor = db.rawQuery(sql, null);
- while (cursor.moveToNext()) {
- Map map = new HashMap();
- for (int i = 0; i < cursor.getColumnCount(); i++) {
- String columnName = cursor.getColumnName(i);
- String columnValue = cursor.getString(cursor.getColumnIndex(columnName));
- map.put(columnName, columnValue);
- }
- list.add(map);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- return list;
- }
-
- @Override
- public Map querySingle(String table, String[] columns, String flag, String groupBy, String having, String orderBy, String limit) {
- Map map = new HashMap();
- SQLiteDatabase db = null;
- Cursor cursor;
- try {
- db = dbHelper.getWritableDatabase();
- cursor = db.query(table, columns, "flag like ?", new String[]{flag}, groupBy, having, orderBy, limit);
- while (cursor.moveToNext()) {
- for (int i = 0; i < cursor.getColumnCount(); i++) {
- String columnName = cursor.getColumnName(i);
- String columnValue = cursor.getString(cursor.getColumnIndex(columnName));
- map.put(columnName, columnValue);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- return map;
- }
-
- @Override
- public Map querySingle(String sql) {
- return null;
- }
-
- @Override
- public String queryValue(String table, String[] columns, String key, String value) {
- String result = null;
- SQLiteDatabase db = null;
- try {
- db = dbHelper.getWritableDatabase();
- Cursor cursor = db.query(table, columns, key + " like ?", new String[]{value}, null, null, null, null);
- cursor.moveToNext();
- String columnName = cursor.getColumnName(0);
- result = cursor.getString(cursor.getColumnIndex(columnName));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != db) {
- db.close();
- }
- }
- return result;
- }
-
- @Override
- public String queryValue(String sql) {
-
- return null;
- }
- }
就两种方法:增加数据和查询数据,其他的未做在本例中也不需要~~
。数据层的实现完成后我们来看看如何往数据库中添加一些测试数据呢。
首先先建立不同的对象实体:
- package com.aigestudio.neteasecommentlistdemo.beans;
-
- import com.aigestudio.neteasecommentlistdemo.cons.ClsCons;
-
- import java.util.UUID;
-
-
-
-
-
-
-
- public class User {
-
-
-
- public static final String COLUMN_FLAG = "flag", COLUMN_USERNAME = "userName", COLUMN_LOCATION = "location", COLUMN_NICK = "nick";
-
- private String flag;
- private String userName;
- private String location;
- private String nick;
-
-
-
-
- public User() {
- this.flag = UUID.randomUUID().toString();
- this.userName = ClsCons.USER_NAME[(int) (Math.random() * 10)];
- this.location = ClsCons.DEFAULT_LOCATION[(int) (Math.random() * 5)];
- this.nick = ClsCons.DEFAULT_NICK_RESID[(int) (Math.random() * 3)];
- }
-
-
-
-
-
-
-
-
-
- public User(String flag, String userName, String location, String nick) {
- this.flag = flag;
- this.userName = userName;
- this.location = location;
- this.nick = nick;
- }
-
- public String getFlag() {
- return flag;
- }
-
- public void setFlag(String flag) {
- this.flag = flag;
- }
-
- public String getUserName() {
- return userName;
- }
-
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- public String getLocation() {
- return location;
- }
-
- public void setLocation(String location) {
- this.location = location;
- }
-
- public String getNick() {
- return nick;
- }
-
- public void setNick(String nick) {
- this.nick = nick;
- }
- }
- package com.aigestudio.neteasecommentlistdemo.beans;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Locale;
- import java.util.UUID;
-
-
-
-
-
-
-
-
- public class Post {
- private String flag;
- private String createAt;
- private List comments;
- private List userPraises, userUnPraises, userCollects;
- private Type type;
-
-
-
-
-
- public Post() {
-
- flag = UUID.randomUUID().toString();
-
-
- Date date = new Date();
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.getDefault());
- this.createAt = format.format(date);
- }
-
- public String getFlag() {
- return flag;
- }
-
- public void setFlag(String flag) {
- this.flag = flag;
- }
-
- public String getCreateAt() {
- return createAt;
- }
-
- public void setCreateAt(String createAt) {
- this.createAt = createAt;
- }
-
- public List getComments() {
- return comments;
- }
-
- public void setComments(List comments) {
- this.comments = comments;
- }
-
- public List getUserPraises() {
- return userPraises;
- }
-
- public void setUserPraises(List userPraises) {
- this.userPraises = userPraises;
- }
-
- public List getUserUnPraises() {
- return userUnPraises;
- }
-
- public void setUserUnPraises(List userUnPraises) {
- this.userUnPraises = userUnPraises;
- }
-
- public List getUserCollects() {
- return userCollects;
- }
-
- public void setUserCollects(List userCollects) {
- this.userCollects = userCollects;
- }
-
- public Type getType() {
- return type;
- }
-
- public void setType(Type type) {
- this.type = type;
- }
-
-
-
-
- public enum Type {
- HOTTEST, NEWEST, NORMAL
- }
- }
- package com.aigestudio.neteasecommentlistdemo.beans;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Locale;
- import java.util.UUID;
-
-
-
-
-
-
-
- public class Comment {
-
-
-
- public static final String COLUMN_FLAG = "flag", COLUMN_USERFLAG = "userFlag", COLUMN_CONTENT = "content", COLUMN_CREATEAT = "createAt";
-
- private String flag;
- private String content;
- private String createAt;
- private User user;
-
-
-
-
-
-
- public Comment(String content, User user) {
-
- flag = UUID.randomUUID().toString();
-
-
- Date date = new Date();
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss", Locale.getDefault());
- this.createAt = format.format(date);
- this.user = user;
- this.content = content;
- }
-
-
-
-
-
-
-
-
-
- public Comment(String flag, String content, String createAt, User user) {
- this.flag = flag;
- this.content = content;
- this.createAt = createAt;
- this.user = user;
- }
-
- public String getFlag() {
- return flag;
- }
-
- public void setFlag(String flag) {
- this.flag = flag;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
-
- public String getCreateAt() {
- return createAt;
- }
-
- public void setCreateAt(String createAt) {
- this.createAt = createAt;
- }
-
- public User getUser() {
- return user;
- }
-
- public void setUser(User user) {
- this.user = user;
- }
- }
然后我们得创建10个不同的用户来模拟不同用户的评论效果:
-
-
-
- for (int i = 0; i < 10; i++) {
- User user = new User();
- ContentValues values = new ContentValues();
- values.put("flag", user.getFlag());
- values.put("userName", user.getUserName());
- values.put("nick", user.getNick());
- values.put("location", user.getLocation());
- serverDAO.add("user", values);
-
-
- userFlagList.add(user.getFlag());
-
- users.add(user);
- }
创建用户后我们会把生成的用户标识flag存入一个列表用来关联一些其他的信息,而把整个User实体存入一个列表用来将其随机绑定到Comment实体中,下面创建100个帖子
-
-
-
- for (int i = 0; i < 100; i++) {
- Post comment = new Post();
- ContentValues values = new ContentValues();
- values.put("flag", comment.getFlag());
- values.put("createAt", comment.getCreateAt());
- serverDAO.add("post", values);
-
-
- postFlagList.add(comment.getFlag());
- }
同样地把生成的帖子flag标识存入列表,之后创建100条评论数据:
-
-
-
- for (int i = 0; i < 100; i++) {
- Comment comment = new Comment(Util.getRandomSimplified((int) (Math.random() * 100)), users.get((int) (Math.random() * 10)));
- ContentValues values = new ContentValues();
- values.put("flag", comment.getFlag());
- values.put("userFlag", comment.getUser().getFlag());
- values.put("content", comment.getContent());
- values.put("createAt", comment.getCreateAt());
- serverDAO.add("comment", values);
-
-
- commentFlagList.add(comment.getFlag());
- }
在生成评论时,将上面生成的User随机选取注入其构造函数完成User和Comment的绑定,最后就是随机关联各种数据了~~~~灰常简单
:
-
-
-
- for (String post : postFlagList) {
-
-
-
- for (int i = 0; i < (int) (Math.random() * 50); i++) {
- ContentValues values = new ContentValues();
- values.put("postFlag", post);
- values.put("commentFlag", commentFlagList.get((int) (Math.random() * commentFlagList.size())));
- serverDAO.add("post_comment", values);
- }
- }
-
-
-
- for (String user : userFlagList) {
-
-
-
- for (int i = 0; i < (int) (Math.random() * 10); i++) {
- ContentValues values = new ContentValues();
- values.put("userFlag", user);
-
-
- values.put("postFlag", postFlagList.get((int) (Math.random() * postFlagList.size())));
- serverDAO.add("user_praise", values);
- }
- }
-
-
-
- for (String user : userFlagList) {
-
-
-
- for (int i = 0; i < (int) (Math.random() * 10); i++) {
- ContentValues values = new ContentValues();
- values.put("userFlag", user);
-
-
- values.put("postFlag", postFlagList.get((int) (Math.random() * postFlagList.size())));
- serverDAO.add("user_unpraise", values);
- }
- }
-
-
-
- for (String user : userFlagList) {
-
-
-
- for (int i = 0; i < (int) (Math.random() * 10); i++) {
- ContentValues values = new ContentValues();
- values.put("userFlag", user);
-
-
- values.put("postFlag", postFlagList.get((int) (Math.random() * postFlagList.size())));
- serverDAO.add("user_collect", values);
- }
- }
所有的这些数据创建的业务逻辑都被封装在SQLiteDataBO.java类中:
- package com.aigestudio.neteasecommentlistdemo.bo;
-
- import android.content.ContentValues;
- import android.content.Context;
-
- import com.aigestudio.neteasecommentlistdemo.beans.Comment;
- import com.aigestudio.neteasecommentlistdemo.beans.Post;
- import com.aigestudio.neteasecommentlistdemo.beans.User;
- import com.aigestudio.neteasecommentlistdemo.dao.ServerDAO;
- import com.aigestudio.neteasecommentlistdemo.utils.Util;
-
- import java.util.ArrayList;
- import java.util.List;
-
-
-
-
-
-
-
- public class SQLiteDataBO {
- private ServerDAO serverDAO;
-
- private List userFlagList, postFlagList, commentFlagList;
- private List users;
-
- public SQLiteDataBO(Context context) {
- serverDAO = new ServerDAO(context);
- }
-
-
-
-
- public void initServerData() {
-
- userFlagList = new ArrayList();
- postFlagList = new ArrayList();
- commentFlagList = new ArrayList();
-
- users = new ArrayList();
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
其中用到的常量类ClsCons.java定义了我们的一些模型数据:
- package com.aigestudio.neteasecommentlistdemo.cons;
-
- import com.aigestudio.neteasecommentlistdemo.R;
-
-
-
-
-
-
-
- public class ClsCons {
-
-
-
- public static final String[] USER_NAME = {"斯蒂芬·爱哥", "罗罗亚·草泥马", "宇智波·逗比", "艾斯比·庄臣", "八歧大蛇", "蒙奇·D·肏飞", "漩涡·溜溜球", "蜡笔·大大新", "虚妄之诺", "风间·八神"};
-
-
-
- public static final String[] DEFAULT_LOCATION = {"网易火星网友", "网易天狼星网友", "网易太阳网友", "网易逗比网友", "网易尼麻痹网友"};
-
-
-
- public static final String[] DEFAULT_NICK_RESID = {String.valueOf(R.drawable.nick1), String.valueOf(R.drawable.nick2), String.valueOf(R.drawable.nick3)};
- }
万事俱备,当应用启动时我们先不显示任何东西,而先创建数据:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
-
-
-
-
- sqLiteDataBO = new SQLiteDataBO(this);
- sqLiteDataBO.initServerData();
- }
数据只需创建一次即可,永久使用(注:如果你clean了项目记得要重新生成数据,因为头像资源ID在clean后有可能改变与插入数据库的ID值不同),数据生成好后我们可以在
SQLiteManager中查看下生成的数据:
好了~~测试数据的创建就讲到这里,劳资都烦了越写越不晓得写了什么……囧!下一节我们将进入核心部分~~来实现这么一个评论列表~~先来两张高清无码大图养养眼: