import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private final static String TAG = “zxt/MainActivity”;
private final String Url = “http://www.imooc.com/api/teacher?type=4&num=30”;
List mNewsDatas = null;
private RefreshListView mListView ;
private TextView mNoDatas;
//下拉数据刷新 改写数组为集合
//public static String[] urls; public static List urls;
//下拉数据刷新
private NewsAdapter mNewsAdapter;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (RefreshListView) findViewById(R.id.listView1);
mNoDatas = (TextView) findViewById(R.id.tv_nodatas);
new NewTask().execute(Url);
}
//下拉刷新数据
/* public interface IRefreshCompleteListener{
public void onRefreshComplete(); } private IRefreshCompleteListener mRefreshComplete; public void IRefreshCompleteListener(IRefreshCompleteListener listener){ mRefreshComplete = listener; }*/ public void refreshData(RefreshListView listview){
//获取最新数据
setReflashData();
//֪ͨ������ʾ
//mRefreshComplete.onRefreshComplete();
}
//下拉刷新
private int newDataslength ;
private void setReflashData() {
new DownPullTask().execute(Url);
//执行更新 start
/*NewsBean bean = new NewsBean(); bean.NewsNo = “31”; bean.NewsName = “new Name”; bean.NewsDescription = “new Des”; bean.NewsPicResUrl = Math.random()>0.5? “https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png” : “https://img-my.csdn.net/uploads/201407/26/1406383291_6518.jpg”;*/
//执行更新 end
}
private void showList(List apk_list) {
//NewsAdapter adapter = (NewsAdapter) mListView.getAdapter();
/*if (adapter == null) { listview = (ReFlashListView) findViewById(R.id.listview); listview.setInterface(this); adapter = new MyAdapter(this, apk_list); listview.setAdapter(adapter); } else {*/ if(mNewsAdapter!=null)
mNewsAdapter.onDateChange(apk_list);
//}
}
private class DownPullTask extends AsyncTask<String,Void,List>{
@Override
protected List doInBackground(String… params) {
Log.i(TAG,“NewTask:doInBackground”);
return getDataByUrl(params[0]);
}
@Override
protected void onPostExecute(List newsBeens) {
super.onPostExecute(newsBeens);
//fix bug if first nodata
//更新了几条数据 if(null!=newsBeens){
newDataslength = newsBeens.size();
if(null==mNewsDatas ){
mNewsDatas = new ArrayList();
}
mNewsDatas.addAll(0,newsBeens);
showList(mNewsDatas);
//֪ͨlistview ˢ�������ϣ�
}else{
newDataslength = 0;
}
mNewsAdapter.refreshComplete(newDataslength);
setHintVisibility(newsBeens);
Log.i(TAG,“NewTask:onPostExecute:”+newDataslength);
Toast.makeText(MainActivity.this,“成功更新”+newDataslength+“条新闻”,Toast.LENGTH_SHORT).show();
}
}
//下拉刷新 end
class NewTask extends AsyncTask<String, Void, List> {
@Override
protected List doInBackground(String… params) {
return getDataByUrl(params[0]);
}
@Override
protected void onPostExecute(List result) {
super.onPostExecute(result);
//下拉刷新
mNewsAdapter= new NewsAdapter(MainActivity.this, result,mListView);
//下拉刷新
mListView.setAdapter(mNewsAdapter);
mListView.setOnScrollListener(mNewsAdapter);
// 处理下拉刷新
mListView.setOnTouchListener(mNewsAdapter);
mNewsDatas = result;
// 处理下拉刷新
//刷新回调 mNewsAdapter.setIRefereshListener(new IRefereshListener() {
@Override
public void onRefresh() {
mListView.postDelayed(new Runnable() {
@Override
public void run() {
refreshData(mListView);
}
}, 1000);
}
});
//刷新回调
setHintVisibility(result);
//加载更多begin
mNewsAdapter.setIOnScrollBottomListener(new NewsAdapter.IOnScrollBottomListener() {
@Override
public void onScrollBottom() {
mListView.postDelayed(new Runnable() {
@Override
public void run() {
//加载数据
loadMoreData();
mNewsAdapter.bottomDataLoadComplete();
}
}, 3000);
}
});
//加载更多end
}
}
//加载更多begin
private int index =100;
private void loadMoreData(){