Cleaned up code

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-06 11:20:12 -04:00
parent 0eac47a347
commit 7ec5e1fa35
8 changed files with 131 additions and 117 deletions

View File

@@ -48,8 +48,9 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
setContentView(R.layout.activity_forum_list);
new Thread(new Runnable(){
public void run(){
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
new ForumDatabase(ForumListActivity.this).saveForums(true);
}

View File

@@ -65,10 +65,10 @@ public class ForumListFragment extends ListFragment {
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ForumContent.ITEMS.size() == 0){
// new Thread(new Runnable() {
// @Override
// public void run() {
if (ForumContent.ITEMS.size() == 0) {
// new Thread(new Runnable() {
// @Override
// public void run() {
ForumContent.getForum(getActivity());
getActivity().runOnUiThread(new Runnable() {
@Override
@@ -76,10 +76,9 @@ public class ForumListFragment extends ListFragment {
setListAdapter(new ForumAdapter(getActivity()));
}
});
// }
// }).start();
}
else
// }
// }).start();
} else
setListAdapter(new ForumAdapter(getActivity()));
}

View File

@@ -107,10 +107,10 @@ public class ForumContent {
ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getForums();
final List<String> list = db.getForums();
int index = 0;
for(String item : list)
for (final String item : list)
addItem(new ForumItem(Integer.valueOf(index++).toString(), db.getForum(item)));
}
}

View File

@@ -90,10 +90,10 @@ public class ThreadContent {
ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getThreads();
final List<String> list = db.getThreads();
int index = 0;
for(String item : list)
for (final String item : list)
addItem(new ThreadItem(Integer.valueOf(index++).toString(), db.getThread(item)));
}

View File

@@ -130,7 +130,7 @@ public class TopicAdapter extends BaseAdapter {
holder.title.setText((String) getItem(position).get("topic_title"));
holder.user.setText((String) getItem(position).get("topic_author_name"));
holder.lastestPost.setText((String) getItem(position).get("short_content"));
holder.newPosts.setVisibility((Boolean.parseBoolean((String) getItem(position).get("new_post")) ? View.VISIBLE : View.GONE));
holder.newPosts.setVisibility(Boolean.parseBoolean((String) getItem(position).get("new_post")) ? View.VISIBLE : View.GONE);
return convertView;
}

View File

@@ -88,10 +88,10 @@ public class TopicContent {
ITEM_MAP.clear();
final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getTopics();
final List<String> list = db.getTopics();
int index = 0;
for(String item : list)
for (final String item : list)
addItem(new TopicItem(Integer.valueOf(index++).toString(), db.getTopic(item)));
}
}

View File

@@ -20,7 +20,8 @@
package org.RickBarrette.osj.forum.database;
/**
* This interface will be used to listen to see when the database events are complete
* This interface will be used to listen to see when the database events are
* complete
*
* @author ricky barrette
*/
@@ -32,8 +33,8 @@ public interface DatabaseListener {
public void onDatabaseUpgradeComplete();
public void onRestoreComplete();
public void onDeletionComplete();
public void onRestoreComplete();
}

View File

@@ -83,14 +83,15 @@ public class ForumDatabase {
* These tables will be used for storing forum, topic, and post id's
*/
db.execSQL("CREATE TABLE " + TABLE_FORUMS + "( id TEXT PRIMARY KEY)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID +" TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID + " TEXT, "+ KEY_TOPIC_ID + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT)");
/*
* These tables will be used for storing forum, topic, and post information.
* These tables will be used for storing forum, topic, and post
* information.
*/
db.execSQL("CREATE TABLE " + TABLE_FORUM_INFO + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE+ " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREAD_INFO + "( id INTEGER PRIMARY KEY, " + KEY_POST_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
}
@@ -337,6 +338,22 @@ public class ForumDatabase {
}
}
/**
* Converts a cursor with key-value pairs into a hash map
*
* @param c
* @return
* @author ricky barrette
*/
private HashMap<String, String> cursorToHashMap(final Cursor c) {
final HashMap<String, String> h = new HashMap<String, String>();
if (c.moveToFirst())
do
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE)));
while (c.moveToNext());
return h;
}
/**
* deletes a note by its row id
*
@@ -361,94 +378,88 @@ public class ForumDatabase {
}).start();
}
/**
* Retrieves a list of forum ids
* @return
* @author ricky barrette
*/
public List<String> getForums(){
ArrayList<String> list = new ArrayList<String>();
Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null);
if(c.moveToFirst())
do{
list.add(c.getString(0));
} while(c.moveToNext());
return list;
}
/**
* Retrieves a list of topic ids
* @return
* @author ricky barrette
*/
public List<String> getTopics(){
ArrayList<String> list = new ArrayList<String>();
Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null);
if(c.moveToFirst())
do{
list.add(c.getString(0));
} while(c.moveToNext());
return list;
}
/**
* Retrieves a list of thread ids
* @return
* @author ricky barrette
*/
public List<String> getThreads(){
ArrayList<String> list = new ArrayList<String>();
Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null);
if(c.moveToFirst())
do{
list.add(c.getString(0));
} while(c.moveToNext());
return list;
}
/**
* Retrieves a forum from the database
*
* @param id
* @return
* @author ricky barrette
*/
public HashMap<String, String> getForum(String id){
return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
public HashMap<String, String> getForum(final String id) {
return cursorToHashMap(mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
null));
}
/**
* Retrieves a topic from the database
* @param id
* Retrieves a list of forum ids
*
* @return
* @author ricky barrette
*/
public HashMap<String, String> getTopic(String id) {
return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
public List<String> getForums() {
final ArrayList<String> list = new ArrayList<String>();
final Cursor c = mDb.query(TABLE_FORUMS, new String[] { KEY_ID }, null, null, null, null, null);
if (c.moveToFirst())
do
list.add(c.getString(0));
while (c.moveToNext());
return list;
}
/**
* Retrieves a post from the threads table of the database
*
* @param id
* @return
* @author ricky barrette
*/
public HashMap<String, String> getThread(String id) {
return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null));
public HashMap<String, String> getThread(final String id) {
return cursorToHashMap(mDb.query(TABLE_THREAD_INFO, new String[] { KEY, KEY_VALUE }, KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
null));
}
/**
* Converts a cursor with key-value pairs into a hash map
* @param c
* Retrieves a list of thread ids
*
* @return
* @author ricky barrette
*/
private HashMap<String, String> cursorToHashMap(Cursor c){
HashMap<String, String> h = new HashMap<String, String>();
if(c.moveToFirst())
do{
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE)));
} while(c.moveToNext());
return h;
public List<String> getThreads() {
final ArrayList<String> list = new ArrayList<String>();
final Cursor c = mDb.query(TABLE_THREADS, new String[] { KEY_ID }, null, null, null, null, null);
if (c.moveToFirst())
do
list.add(c.getString(0));
while (c.moveToNext());
return list;
}
/**
* Retrieves a topic from the database
*
* @param id
* @return
* @author ricky barrette
*/
public HashMap<String, String> getTopic(final String id) {
return cursorToHashMap(mDb.query(TABLE_TOPIC_INFO, new String[] { KEY, KEY_VALUE }, KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(id), null, null, null,
null));
}
/**
* Retrieves a list of topic ids
*
* @return
* @author ricky barrette
*/
public List<String> getTopics() {
final ArrayList<String> list = new ArrayList<String>();
final Cursor c = mDb.query(TABLE_TOPICS, new String[] { KEY_ID }, null, null, null, null, null);
if (c.moveToFirst())
do
list.add(c.getString(0));
while (c.moveToNext());
return list;
}
/**
@@ -504,7 +515,7 @@ public class ForumDatabase {
/*
* save the forums
*/
if (result != null){
if (result != null) {
String id = null;
for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
@@ -578,13 +589,14 @@ public class ForumDatabase {
if (values == null)
throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
final ContentValues id = new ContentValues();
id.put(KEY_ID, forumId);
upsertTable(TABLE_FORUMS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(forumId), id);
upsertTable(TABLE_FORUMS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(forumId), id);
for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId);
upsertTable(TABLE_FORUM_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
upsertTable(TABLE_FORUM_INFO,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
}
}
@@ -616,15 +628,16 @@ public class ForumDatabase {
if (values == null)
throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
final ContentValues id = new ContentValues();
id.put(KEY_ID, postId);
id.put(KEY_FORUM_ID, forumId);
id.put(KEY_TOPIC_ID, topicId);
upsertTable(TABLE_THREADS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(postId), id);
upsertTable(TABLE_THREADS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(postId), id);
for (final ContentValues item : values) {
item.put(KEY_POST_ID, postId);
upsertTable( TABLE_THREAD_INFO, KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item);
upsertTable(TABLE_THREAD_INFO,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_POST_ID + " = " + DatabaseUtils.sqlEscapeString(postId), item);
}
}
@@ -641,10 +654,10 @@ public class ForumDatabase {
if (values == null)
throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
final ContentValues id = new ContentValues();
id.put(KEY_ID, topicId);
id.put(KEY_FORUM_ID, forumId);
upsertTable(TABLE_TOPICS, KEY_ID +" = "+ DatabaseUtils.sqlEscapeString(topicId), id);
upsertTable(TABLE_TOPICS, KEY_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), id);
for (final ContentValues item : values) {
item.put(KEY_TOPIC_ID, topicId);