I can now display topics from the database

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-06 11:13:41 -04:00
parent 106a4626f6
commit 73a9d679da
4 changed files with 50 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum"
android:versionCode="297"
android:versionCode="302"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk

View File

@@ -127,10 +127,10 @@ public class TopicAdapter extends BaseAdapter {
* always call setChecked() after calling setOnCheckedChangedListener.
* This will prevent the list from changing the values on you.
*/
holder.title.setText(new String((byte[]) getItem(position).get("topic_title")));
holder.user.setText(new String((byte[]) getItem(position).get("topic_author_name")));
holder.lastestPost.setText(new String((byte[]) getItem(position).get("short_content")));
holder.newPosts.setVisibility((Boolean) getItem(position).get("new_post") ? View.VISIBLE : View.GONE);
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));
return convertView;
}

View File

@@ -25,6 +25,8 @@ import java.util.List;
import java.util.Map;
import org.RickBarrette.osj.forum.Log;
import org.RickBarrette.osj.forum.content.ForumContent.ForumItem;
import org.RickBarrette.osj.forum.database.ForumDatabase;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;
@@ -85,22 +87,14 @@ public class TopicContent {
* @author ricky barrette
*/
public static void getTopics(final String forumId, final Context context) {
final XMLRPCClient client = XMLRPCClient.getClient(context);
ITEMS.clear();
ITEM_MAP.clear();
Object[] result = null;
try {
result = (Object[]) ((HashMap<?, ?>) client.call("get_topic", forumId, 0, 0)).get("topics");
} catch (final XMLRPCException e) {
e.printStackTrace();
}
final ForumDatabase db = new ForumDatabase(context);
List<String> list = db.getTopics();
if (result != null)
for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
addItem(new TopicItem(Integer.valueOf(i).toString(), contentHash));
}
int index = 0;
for(String item : list)
addItem(new TopicItem(Integer.valueOf(index++).toString(), db.getTopic(item)));
}
}

View File

@@ -376,6 +376,21 @@ public class ForumDatabase {
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 forum from the database
* @param id
@@ -383,11 +398,30 @@ public class ForumDatabase {
* @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));
}
/**
* Retrieves a topic from the database
* @param id
* @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));
}
/**
* Converts a cursor with key-value pairs into a hash map
* @param c
* @return
* @author ricky barrette
*/
private HashMap<String, String> cursorToHashMap(Cursor c){
HashMap<String, String> h = new HashMap<String, String>();
Cursor c = mDb.query(TABLE_FORUM_INFO, new String[] { KEY, KEY_VALUE }, KEY_FORUM_ID +" = "+ DatabaseUtils.sqlEscapeString(id), null, null, null, null);
if(c.moveToFirst())
do{
h.put(c.getString(0), c.getString(1));
h.put(c.getString(c.getColumnIndex(KEY)), c.getString(c.getColumnIndex(KEY_VALUE)));
} while(c.moveToNext());
return h;
}