From e6a7798031946a78b68d7242559084772cbf7ac3 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Fri, 14 Sep 2012 10:34:53 -0400 Subject: [PATCH] I can now display posts Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 5 +- .../res/layout/activity_forum_twopane.xml | 22 ++- OSJ Forum/res/layout/post_item.xml | 4 +- .../osj/forum/ForumDetailActivity.java | 12 +- .../osj/forum/ForumListActivity.java | 33 +++-- .../osj/forum/TopicDetailActivity.java | 11 +- .../osj/forum/TopicDetailFragment.java | 93 ++++++++++-- .../osj/forum/TopicListActivity.java | 32 ++-- .../osj/forum/TopicListFragment.java | 8 +- .../osj/forum/content/ThreadAdapter.java | 140 ++++++++++++++++++ .../osj/forum/content/ThreadContent.java | 107 +++++++++++++ .../osj/forum/dummy/DummyContent.java | 58 -------- 12 files changed, 414 insertions(+), 111 deletions(-) create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadAdapter.java create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadContent.java delete mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/dummy/DummyContent.java diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index 0277911..6dfe1f4 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ + + tools:context=".ForumListActivity" > - + android:layout_weight="1" > - + + + - + \ No newline at end of file diff --git a/OSJ Forum/res/layout/post_item.xml b/OSJ Forum/res/layout/post_item.xml index ff02045..39ef8d6 100644 --- a/OSJ Forum/res/layout/post_item.xml +++ b/OSJ Forum/res/layout/post_item.xml @@ -19,7 +19,9 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" - android:layout_alignParentTop="true" /> + android:layout_alignParentTop="true" + android:src="@drawable/ic_launcher" + /> (getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, DummyContent.ITEMS)); + setListAdapter(new TopicAdapter(getActivity())); } @Override @@ -73,7 +73,7 @@ public class TopicListFragment extends ListFragment { @Override public void onListItemClick(final ListView listView, final View view, final int position, final long id) { super.onListItemClick(listView, view, position, id); - mCallbacks.onItemSelected(this, DummyContent.ITEMS.get(position).id); + mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position).id); } @Override diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadAdapter.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadAdapter.java new file mode 100644 index 0000000..a53a09d --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadAdapter.java @@ -0,0 +1,140 @@ +/** + * ThreadAdapter.java + * @date Sep 14, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum.content; + +import java.util.HashMap; + +import android.content.Context; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.QuickContactBadge; +import android.widget.TextView; + +import com.RickBarrette.osj.forum.R; + +/** + * + * @author ricky barrette + */ +public class ThreadAdapter extends BaseAdapter { + + class ViewHolder { + TextView title; + TextView user; + TextView post; + QuickContactBadge badage; + } + + private static final String TAG = "TopicAdapter"; + + + private LayoutInflater mInflater; + + /** + * + * @author ricky barrette + */ + public ThreadAdapter(Context context) { + mInflater = LayoutInflater.from(context); + } + + /* + * (non-Javadoc) + * + * @see android.widget.Adapter#getCount() + */ + @Override + public int getCount() { + return ThreadContent.ITEMS.size(); + } + + /* + * (non-Javadoc) + * + * @see android.widget.Adapter#getItem(int) + */ + @Override + public HashMap getItem(int position) { + return (HashMap) ThreadContent.ITEMS.get(position).content; + } + + /* + * (non-Javadoc) + * + * @see android.widget.Adapter#getItemId(int) + */ + @Override + public long getItemId(int position) { + return position; + } + + /* + * (non-Javadoc) + * + * @see android.widget.Adapter#getView(int, android.view.View, + * android.view.ViewGroup) + */ + @Override + public View getView(int position, View convertView, ViewGroup parent) { + // A ViewHolder keeps references to children views to avoid unnecessary + // calls to findViewById() on each row. + ViewHolder holder; + + // When convertView is not null, we can reuse it directly, there is no + // need + // to reinflate it. We only inflate a new View when the convertView + // supplied + // by ListView is null. + if (convertView == null) { + convertView = mInflater.inflate(R.layout.post_item, null); + + // Creates a ViewHolder and store references to the two children + // views + // we want to bind data to. + holder = new ViewHolder(); + holder.title = (TextView) convertView.findViewById(R.id.textView1); + holder.user = (TextView) convertView.findViewById(R.id.textView2); + holder.post = (TextView) convertView.findViewById(R.id.textView3); + holder.badage = (QuickContactBadge) convertView.findViewById(R.id.quickContactBadge1); + + convertView.setTag(holder); + } else + // Get the ViewHolder back to get fast access to the TextView + // and the ImageView. + holder = (ViewHolder) convertView.getTag(); + + /* + * Bind the data efficiently with the holder. Remember that you should + * 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("post_title"))); + holder.user.setText(new String((byte[]) getItem(position).get("post_author_name"))); + holder.post.setText(new String((byte[]) getItem(position).get("post_content"))); + + for (Object key : getItem(position).keySet()) + Log.d(TAG, key.toString()); + return convertView; + } + +} diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadContent.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadContent.java new file mode 100644 index 0000000..1075056 --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/ThreadContent.java @@ -0,0 +1,107 @@ +/** + * ThreadContent.java + * @date Sep 14, 2012 + * @author ricky barrette + * + * Copyright 2012 Richard Barrette + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ +package com.RickBarrette.osj.forum.content; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.xmlrpc.android.XMLRPCClient; +import org.xmlrpc.android.XMLRPCException; + +import android.content.Context; +import android.util.Log; + +import com.RickBarrette.osj.forum.Constraints; + +/** + * + * @author ricky barrette + */ +public class ThreadContent { + + /** + * This Forum Object + * @author ricky barrette + */ + public static class ThreadItem { + private static final String TAG = "TopicItem"; + public String id; + public HashMap content; + + /** + * Creates a new Topic Item + * @param id + * @param content + * @author ricky barrette + */ + public ThreadItem(final String id, final HashMap content) { + this.id = id; + this.content = content; + + for(Object key: content.keySet()) + Log.d(TAG, key.toString()); + } + } + + public static final List ITEMS = new ArrayList(); + public static final Map ITEM_MAP = new HashMap(); + + /** + * Adds a new Forum Item to the list + * @param item + * @author ricky barrette + */ + private static void addItem(final ThreadItem item) { + ITEMS.add(item); + ITEM_MAP.put(item.id, item); + } + + /** + * This downloads a thread for a specific topic + * @param topicId + * @param startNumber + * @param lastNumber + * @param context + * @author ricky barrette + */ + public static void getThread(String topicId, int startNumber, int lastNumber, final Context context){ + final XMLRPCClient client = Constraints.getClient(context); + + ITEMS.clear(); + ITEM_MAP.clear(); + + Object[] result = null; + try { + result = (Object[]) ((HashMap) client.call("get_thread", topicId, startNumber, lastNumber)).get("posts"); + } catch (XMLRPCException e) { + e.printStackTrace(); + } + + if(result!= null){ + for(int i = 0; i < result.length; i++){ + HashMap contentHash = (HashMap) result[i]; + addItem(new ThreadItem(Integer.valueOf(i).toString(), contentHash)); + } + } + } + +} diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/dummy/DummyContent.java b/OSJ Forum/src/com/RickBarrette/osj/forum/dummy/DummyContent.java deleted file mode 100644 index d82edea..0000000 --- a/OSJ Forum/src/com/RickBarrette/osj/forum/dummy/DummyContent.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * DummyContent - * @date Aug 27, 2012 - * @author ricky barrette - * - * Copyright 2012 Richard Barrette - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ -package com.RickBarrette.osj.forum.dummy; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class DummyContent { - - public static class DummyItem { - - public String id; - public String content; - - public DummyItem(final String id, final String content) { - this.id = id; - this.content = content; - } - - @Override - public String toString() { - return content; - } - } - - public static List ITEMS = new ArrayList(); - public static Map ITEM_MAP = new HashMap(); - - static { - addItem(new DummyItem("1", "Item 1")); - addItem(new DummyItem("2", "Item 2")); - addItem(new DummyItem("3", "Item 3")); - } - - private static void addItem(final DummyItem item) { - ITEMS.add(item); - ITEM_MAP.put(item.id, item); - } -}