I can now display posts

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-09-14 10:34:53 -04:00
parent 12f4f72729
commit e6a7798031
12 changed files with 414 additions and 111 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="com.RickBarrette.osj.forum"
android:versionCode="116"
android:versionCode="170"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk
@@ -30,6 +30,9 @@
<activity
android:name=".TopicListActivity"
android:label="@string/app_name" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ForumListActivity" />
</activity>
<activity
android:name=".TopicDetailActivity"

View File

@@ -1,21 +1,29 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ForumListActivity">
tools:context=".ForumListActivity" >
<fragment android:name="com.RickBarrette.osj.forum.ForumListFragment"
android:id="@+id/forum_list"
<FrameLayout
android:id="@+id/forum_list_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
android:layout_weight="1" >
<FrameLayout android:id="@+id/forum_detail_container"
<fragment
android:id="@+id/forum_list"
android:name="com.RickBarrette.osj.forum.ForumListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<FrameLayout
android:id="@+id/forum_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />

View File

@@ -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"
/>
<TextView
android:id="@+id/textView3"

View File

@@ -19,8 +19,11 @@
*/
package com.RickBarrette.osj.forum;
import com.RickBarrette.osj.forum.content.OnItemSelectedListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
@@ -29,7 +32,7 @@ import android.view.MenuItem;
*
* @author ricky barrette
*/
public class ForumDetailActivity extends FragmentActivity {
public class ForumDetailActivity extends FragmentActivity implements OnItemSelectedListener {
@Override
protected void onCreate(final Bundle savedInstanceState) {
@@ -56,4 +59,11 @@ public class ForumDetailActivity extends FragmentActivity {
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(Fragment listFragment, String id) {
final Intent detailIntent = new Intent(this, TopicDetailActivity.class);
detailIntent.putExtra(TopicDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}

View File

@@ -50,12 +50,19 @@ public class ForumListActivity extends FragmentActivity implements OnItemSelecte
@Override
public void onItemSelected(final Fragment listFragment, final String id) {
if(listFragment instanceof ForumDetailFragment){
final Intent detailIntent = new Intent(this, TopicListActivity.class);
detailIntent.putExtra(TopicListActivity.ARG_ITEM_ID, id);
startActivity(detailIntent);
} else
if (mTwoPane) {
if(listFragment instanceof ForumListFragment){
final Bundle arguments = new Bundle();
arguments.putString(ForumDetailFragment.ARG_ITEM_ID, id);
final ForumDetailFragment fragment = new ForumDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.forum_detail_container, fragment).commit();
}
} else {
final Intent detailIntent = new Intent(this, ForumDetailActivity.class);

View File

@@ -19,13 +19,16 @@
*/
package com.RickBarrette.osj.forum;
import com.RickBarrette.osj.forum.content.OnItemSelectedListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
public class TopicDetailActivity extends FragmentActivity {
public class TopicDetailActivity extends FragmentActivity implements OnItemSelectedListener{
@Override
protected void onCreate(final Bundle savedInstanceState) {
@@ -52,4 +55,10 @@ public class TopicDetailActivity extends FragmentActivity {
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(Fragment listFragment, String id) {
// TODO Auto-generated method stub
}
}

View File

@@ -19,36 +19,103 @@
*/
package com.RickBarrette.osj.forum;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import com.RickBarrette.osj.forum.dummy.DummyContent;
import com.RickBarrette.osj.forum.content.OnItemSelectedListener;
import com.RickBarrette.osj.forum.content.ThreadAdapter;
import com.RickBarrette.osj.forum.content.ThreadContent;
import com.RickBarrette.osj.forum.content.TopicContent;
import com.RickBarrette.osj.forum.content.TopicContent.TopicItem;
public class TopicDetailFragment extends Fragment {
public class TopicDetailFragment extends ListFragment {
public static final String ARG_ITEM_ID = "item_id";
private int mActivatedPosition = ListView.INVALID_POSITION;
private static final String STATE_ACTIVATED_POSITION = "activated_position";
DummyContent.DummyItem mItem;
TopicItem mItem;
private OnItemSelectedListener mCallbacks;
public TopicDetailFragment() {
}
/**
* (non-Javadoc)
* @see android.support.v4.app.Fragment#onAttach(android.app.Activity)
*/
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (!(activity instanceof OnItemSelectedListener))
throw new IllegalStateException("Activity must implement fragment's callbacks.");
mCallbacks = (OnItemSelectedListener) activity;
}
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID))
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
mItem = TopicContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
if(mItem == null)
throw new NullPointerException();
new Thread( new Runnable(){
@Override
public void run(){
ThreadContent.getThread((String) mItem.content.get("topic_id"), 0, 0, getActivity());
getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
setListAdapter(new ThreadAdapter(getActivity()));
}
});
}
}).start();
}
/**
* (non-Javadoc)
* @see android.support.v4.app.ListFragment#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
mCallbacks.onItemSelected(this, TopicContent.ITEMS.get(position).id);
}
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != AdapterView.INVALID_POSITION)
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_topic_detail, container, false);
if (mItem != null)
((TextView) rootView.findViewById(R.id.topic_detail)).setText(mItem.content);
return rootView;
public void onViewCreated(final View view, final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION))
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
public void setActivatedPosition(final int position) {
if (position == AdapterView.INVALID_POSITION)
getListView().setItemChecked(mActivatedPosition, false);
else
getListView().setItemChecked(position, true);
mActivatedPosition = position;
}
public void setActivateOnItemClick(final boolean activateOnItemClick) {
getListView().setChoiceMode(activateOnItemClick ? AbsListView.CHOICE_MODE_SINGLE : AbsListView.CHOICE_MODE_NONE);
}
}

View File

@@ -29,6 +29,7 @@ import com.TwentyCodes.android.exception.ExceptionHandler;
public class TopicListActivity extends FragmentActivity implements OnItemSelectedListener {
public static final String ARG_ITEM_ID = "item_id";
private boolean mTwoPane;
@Override
@@ -41,11 +42,18 @@ public class TopicListActivity extends FragmentActivity implements OnItemSelecte
if (findViewById(R.id.topic_detail_container) != null) {
mTwoPane = true;
((TopicListFragment) getSupportFragmentManager().findFragmentById(R.id.topic_list)).setActivateOnItemClick(true);
final Bundle arguments = new Bundle();
arguments.putString(TopicDetailFragment.ARG_ITEM_ID, this.getIntent().getExtras().getString(ARG_ITEM_ID));
final TopicDetailFragment fragment = new TopicDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.topic_detail_container, fragment).commit();
}
}
@Override
public void onItemSelected(final Fragment listFragment, final String id) {
if(listFragment instanceof TopicListFragment)
if (mTwoPane) {
final Bundle arguments = new Bundle();
arguments.putString(TopicDetailFragment.ARG_ITEM_ID, id);

View File

@@ -26,11 +26,11 @@ import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.RickBarrette.osj.forum.content.OnItemSelectedListener;
import com.RickBarrette.osj.forum.dummy.DummyContent;
import com.RickBarrette.osj.forum.content.TopicAdapter;
import com.RickBarrette.osj.forum.content.TopicContent;
public class TopicListFragment extends ListFragment {
@@ -61,7 +61,7 @@ public class TopicListFragment extends ListFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(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

View File

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

View File

@@ -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<ThreadItem> ITEMS = new ArrayList<ThreadItem>();
public static final Map<String, ThreadItem> ITEM_MAP = new HashMap<String, ThreadItem>();
/**
* 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));
}
}
}
}

View File

@@ -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<DummyItem> ITEMS = new ArrayList<DummyItem>();
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
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);
}
}