Implemented topics

Added a simple adapter and convince methods to display topics to the
user.

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-08-31 02:09:25 -04:00
parent 1c8da79a6e
commit 1ed3e1728b
4 changed files with 146 additions and 4 deletions

View File

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

View File

@@ -28,7 +28,7 @@ import android.widget.ListView;
import com.RickBarrette.osj.forum.content.ForumContent;
import com.RickBarrette.osj.forum.content.ForumContent.ForumItem;
import com.RickBarrette.osj.forum.content.SubForumAdapter;
import com.RickBarrette.osj.forum.content.TopicAdapter;
/**
* This fragment will be used to display information about the forum
@@ -51,8 +51,19 @@ public class ForumDetailFragment extends ListFragment {
if (getArguments().containsKey(ARG_ITEM_ID))
mItem = ForumContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
if(mItem != null)
this.setListAdapter(new SubForumAdapter(getActivity(), Integer.parseInt(getArguments().getString(ARG_ITEM_ID))));
new Thread( new Runnable(){
@Override
public void run(){
final Object[] result = mItem.getTopics(getActivity());
getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
setListAdapter(new TopicAdapter(getActivity(), result));
}
});
}
}).start();
}
@Override

View File

@@ -55,6 +55,16 @@ public class ForumContent {
public Object[] getChildren(){
return (Object[]) this.content.get("child");
}
public Object[] getTopics(final Context context){
final XMLRPCClient client = new XMLRPCClient((context.getString(R.string.server)));
try {
return (Object[]) ((HashMap<?,?>) client.call("get_topic", this.content.get("forum_id"), 0, 0)).get("topics");
} catch (XMLRPCException e) {
e.printStackTrace();
}
return null;
}
}
public static List<ForumItem> ITEMS = new ArrayList<ForumItem>();

View File

@@ -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;
}
}