package com.example.newsapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.jcodecraeer.PullToRefreshListView;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public ArrayList<Article> mArticleList;
public NewsAdapter newsAdapter;
private PullToRefreshListView mListView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String href = "
http://www.fx120.net/elder/lrxw/";
newsAdapter = new NewsAdapter(MainActivity.this, mArticleList);
mArticleList=new ArrayList<Article>();
mListView = (PullToRefreshListView) findViewById(R.id.listview);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
});
mListView.setAdapter(newsAdapter);
mListView.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
public void onRefresh() {
loadNewsList(href, 1, true);
}
public void onLoadingMore() {
int pageIndex = mArticleList.size() / 10 + 1;
Log.i("pageIndex","pageIndex = " + pageIndex);
loadNewsList(href, pageIndex, false);
}
});
loadNewsList(href, 1, true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public ArrayList<Article> parseArticleList(String href, final int page) {
ArrayList<Article> articleList = new ArrayList<Article>();
href=_MakeURL(href, page);
try {
Document doc = Jsoup
.connect(href)
.userAgent(
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
.get();
Element masthead = doc.select("div.w_cnl_1").first();
Elements titleElements = masthead.select("div.w_fl h3 a");
Elements summaryElements = masthead.select("div.w_fl p");
Elements postTimeElements = masthead
.select("div.w_fl span");
int count = titleElements.size();
for (int i = 0; i < count; i++)
{
Article article = new Article();
Element titleElement = titleElements.get(i);
Element summaryElement = summaryElements.get(i);
Element postTimeElement = postTimeElements.get(i);
String url_1 = titleElement.attr("href");
String url = "
http://www.fx120.net/elder/lrxw/" + url_1;
String title = titleElement.text();
String summary = summaryElement.text();
String time = postTimeElement.text();
Log.d("Title", title);
article.setTitle(title);
article.setSummary(summary);
article.setPostTime(time);
Log.d("Time", time);
article.setUrl(url);
articleList.add(article);
Log.d("List", ""+articleList.size());
}
} catch (IOException e) {
e.printStackTrace();
}
return articleList;
}
private void loadNewsList(final String href ,final int page, final boolean isRefresh) {
final Handler handler = new Handler(){
public void handleMessage(Message msg) {
if (msg.what == 1) {
ArrayList<Article> articleList = (ArrayList<Article>)msg.obj;
Log.d("loadNewsList", ""+articleList.size());
/*newsAdapter = new NewsAdapter(MainActivity.this, articleList);
mListView.setAdapter(newsAdapter);*/
if (isRefresh) {
articleList.clear();//下拉刷新之前先将数据清空
mListView.onRefreshComplete (new Date().toLocaleString());
}
for (Article article : articleList) {
articleList.add(article);
}
newsAdapter.notifyDataSetChanged();
if (articleList.size() < 10) {
mListView.onLoadingMoreComplete(true);
} else if (articleList.size() == 10) {
mListView.onLoadingMoreComplete(false);
}
}
}
};
new Thread() {
public void run() {
Message msg = new Message();
ArrayList<Article> articleList = new ArrayList<Article>();
try {
articleList = parseArticleList(href,page);
} catch (Exception e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
msg.what = 1;
msg.obj = articleList;
handler.sendMessage(msg);
}
}.start();
}
private static String _MakeURL(String p_url, int page) {
StringBuilder url = new StringBuilder(p_url);
if (page>1){
url.append("List_"+page+".html");
}
Log.d("_MakeURL",url.toString());
return url.toString();
}
}