From 1ed3e1728b371b9ff77cac50602d9635ce73320f Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Fri, 31 Aug 2012 02:09:25 -0400 Subject: [PATCH] Implemented topics Added a simple adapter and convince methods to display topics to the user. Signed-off-by: Ricky Barrette --- OSJ Forum/AndroidManifest.xml | 2 +- .../osj/forum/ForumDetailFragment.java | 17 ++- .../osj/forum/content/ForumContent.java | 10 ++ .../osj/forum/content/TopicAdapter.java | 121 ++++++++++++++++++ 4 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java diff --git a/OSJ Forum/AndroidManifest.xml b/OSJ Forum/AndroidManifest.xml index f305018..2526b3d 100644 --- a/OSJ Forum/AndroidManifest.xml +++ b/OSJ Forum/AndroidManifest.xml @@ -1,5 +1,5 @@ ) client.call("get_topic", this.content.get("forum_id"), 0, 0)).get("topics"); + } catch (XMLRPCException e) { + e.printStackTrace(); + } + return null; + } } public static List ITEMS = new ArrayList(); diff --git a/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java new file mode 100644 index 0000000..c8bb32c --- /dev/null +++ b/OSJ Forum/src/com/RickBarrette/osj/forum/content/TopicAdapter.java @@ -0,0 +1,121 @@ +/** + * TopicAdapter.java + * @date Aug 31, 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.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import com.RickBarrette.osj.forum.R; + +/** + * This adapter will be used to display topics to the user + * @author ricky barrette + */ +public class TopicAdapter extends BaseAdapter { + + class ViewHolder { + TextView title; + } + + private LayoutInflater mInflater; + private Object[] mTopics; + + /** + * + * @author ricky barrette + */ + public TopicAdapter(final Context context, final Object[] topics) { + mInflater = LayoutInflater.from(context); + mTopics = topics; + } + + /* (non-Javadoc) + * @see android.widget.Adapter#getCount() + */ + @Override + public int getCount() { + if(mTopics != null) + return mTopics.length; + else + return 0; + } + + /* (non-Javadoc) + * @see android.widget.Adapter#getItem(int) + */ + @Override + public HashMap getItem(int position) { + return (HashMap) mTopics[position]; + } + + /* (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.forum_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); + + 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("topic_title"))); + return convertView; + } + +}