Skip to content
Browse files

Added comments for Q&A

  • Loading branch information...
1 parent 2e9e53c commit 2b0c8c731c5336eb62e10822b88f055cbd33758a @AndreyZaytsev AndreyZaytsev committed May 1, 2012
View
4 src/net/meiolania/apps/habrahabr/activities/QaShowActivity.java
@@ -17,6 +17,7 @@
package net.meiolania.apps.habrahabr.activities;
import net.meiolania.apps.habrahabr.R;
+import net.meiolania.apps.habrahabr.fragments.QaCommentsFragment;
import net.meiolania.apps.habrahabr.fragments.QaShowFragment;
import android.content.Intent;
import android.os.Bundle;
@@ -75,7 +76,8 @@ public void onTabSelected(Tab tab, FragmentTransaction ft){
QaShowFragment qaShowFragment = new QaShowFragment(url);
ft.replace(android.R.id.content, qaShowFragment);
}else if(tab.getTag().equals("comments")){
-
+ QaCommentsFragment qaCommentsFragment = new QaCommentsFragment(url);
+ ft.replace(android.R.id.content, qaCommentsFragment);
}
}
View
4 ...rahabr/adapters/PostsCommentsAdapter.java → ...s/habrahabr/adapters/CommentsAdapter.java
@@ -28,11 +28,11 @@
import android.widget.LinearLayout;
import android.widget.TextView;
-public class PostsCommentsAdapter extends BaseAdapter{
+public class CommentsAdapter extends BaseAdapter{
protected ArrayList<CommentsData> commentsDatas;
protected Context context;
- public PostsCommentsAdapter(Context context, ArrayList<CommentsData> commentsDatas){
+ public CommentsAdapter(Context context, ArrayList<CommentsData> commentsDatas){
this.commentsDatas = commentsDatas;
this.context = context;
}
View
10 src/net/meiolania/apps/habrahabr/fragments/PostsCommentsFragment.java
@@ -21,7 +21,7 @@
import java.util.Hashtable;
import net.meiolania.apps.habrahabr.R;
-import net.meiolania.apps.habrahabr.adapters.PostsCommentsAdapter;
+import net.meiolania.apps.habrahabr.adapters.CommentsAdapter;
import net.meiolania.apps.habrahabr.data.CommentsData;
import net.meiolania.apps.habrahabr.utils.UIUtils;
@@ -39,7 +39,7 @@
public class PostsCommentsFragment extends SherlockListFragment{
public final static String LOG_TAG = "PostsCommentsFragment";
protected final ArrayList<CommentsData> commentsDatas = new ArrayList<CommentsData>();
- protected PostsCommentsAdapter postsCommentsAdapter;
+ protected CommentsAdapter commentsAdapter;
protected String url;
public PostsCommentsFragment(){
@@ -60,8 +60,8 @@ public String getUrl(){
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
- postsCommentsAdapter = new PostsCommentsAdapter(getSherlockActivity(), commentsDatas);
- setListAdapter(postsCommentsAdapter);
+ commentsAdapter = new CommentsAdapter(getSherlockActivity(), commentsDatas);
+ setListAdapter(commentsAdapter);
loadList();
}
@@ -115,7 +115,7 @@ protected void onPostExecute(Void result){
getSherlockActivity().runOnUiThread(new Runnable(){
public void run(){
if(!isCancelled())
- postsCommentsAdapter.notifyDataSetChanged();
+ commentsAdapter.notifyDataSetChanged();
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
}
});
View
109 src/net/meiolania/apps/habrahabr/fragments/QaCommentsFragment.java
@@ -0,0 +1,109 @@
+package net.meiolania.apps.habrahabr.fragments;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+import net.meiolania.apps.habrahabr.R;
+import net.meiolania.apps.habrahabr.adapters.CommentsAdapter;
+import net.meiolania.apps.habrahabr.data.CommentsData;
+import net.meiolania.apps.habrahabr.utils.UIUtils;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.widget.Toast;
+
+import com.actionbarsherlock.app.SherlockListFragment;
+
+public class QaCommentsFragment extends SherlockListFragment{
+ public final static String LOG_TAG = "QaCommentsFragment";
+ protected final ArrayList<CommentsData> commentsDatas = new ArrayList<CommentsData>();
+ protected CommentsAdapter commentsAdapter;
+ protected String url;
+
+ public QaCommentsFragment(){
+ }
+
+ public QaCommentsFragment(String url){
+ this.url = url;
+ }
+
+ public void setUrl(String url){
+ this.url = url;
+ }
+
+ public String getUrl(){
+ return url;
+ }
+
+ @Override
+ public void onActivityCreated(Bundle savedInstanceState){
+ super.onActivityCreated(savedInstanceState);
+ commentsAdapter = new CommentsAdapter(getSherlockActivity(), commentsDatas);
+ setListAdapter(commentsAdapter);
+ loadList();
+ }
+
+ protected void loadList(){
+ new LoadComments().execute();
+ if(!UIUtils.isHoneycombOrHigher())
+ Toast.makeText(getSherlockActivity(), R.string.loading_comments, Toast.LENGTH_SHORT).show();
+ }
+
+ protected final class LoadComments extends AsyncTask<Void, Void, Void>{
+
+ @Override
+ protected Void doInBackground(Void... params){
+ try{
+ Document document = Jsoup.connect(url).get();
+ Elements answers = document.select("div.answer");
+ for(Element answer : answers){
+ CommentsData commentsData = new CommentsData();
+
+ Element name = answer.select("a.username").first();
+ Element message = answer.select("div.message").first();
+
+ commentsData.setAuthor(name.text());
+ commentsData.setComment(message.text());
+ commentsData.setLevel(0);
+
+ commentsDatas.add(commentsData);
+
+ Elements comments = answer.select("div.comment_item");
+ for(Element comment : comments){
+ commentsData = new CommentsData();
+
+ name = comment.select("span.info > a").first();
+ message = comment.select("span.text").first();
+
+ commentsData.setAuthor(name.text());
+ commentsData.setComment(message.text());
+ commentsData.setLevel(1);
+
+ commentsDatas.add(commentsData);
+ }
+ }
+ }
+ catch(IOException e){
+ }
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void result){
+ getSherlockActivity().runOnUiThread(new Runnable(){
+ public void run(){
+ if(!isCancelled())
+ commentsAdapter.notifyDataSetChanged();
+ getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
+ }
+ });
+ }
+
+ }
+
+}

0 comments on commit 2b0c8c7

Please sign in to comment.
Something went wrong with that request. Please try again.