标签:
package com.example.xml_shangxia; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.text.format.DateUtils; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.widget.Toast; import com.example.adapter.MyBaseAdapter; import com.example.net.NetWorkUtils; import com.example.vo.MyDatas; import com.example.vo.MyNews; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener; import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener; import com.handmark.pulltorefresh.library.PullToRefreshListView; import com.thoughtworks.xstream.XStream; public class MainActivity extends Activity{ private ArrayList<MyNews>list=new ArrayList<MyNews>(); private ArrayList<MyNews>list1=new ArrayList<MyNews>(); int i=1; private MyBaseAdapter adapter; private PullToRefreshListView mPullRefreshListView; private boolean boo=true; private String JSON="http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); new MyAsyncTask().execute(JSON); } private void init() { // TODO Auto-generated method stub //mylist=(ListView) findViewById(R.id.mylist); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); // Set a listener to be invoked when the list should be refreshed. mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // Update the LastUpdatedLabel refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); // Do work to refresh the list here. new MyAsyncTask().execute(JSON+i++); } }); // Add an end-of-list listener mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { Toast.makeText(MainActivity.this, "到底了!加载更多", Toast.LENGTH_SHORT).show(); new MyAsyncTask().execute(JSON+i++); } }); } class MyAsyncTask extends AsyncTask<String, Integer, String>{ @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String url=params[0]; String str=NetWorkUtils.newsList(url); return str; } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub if(boo=true){ XStream stream=new XStream(); stream.processAnnotations(MyDatas.class); MyDatas my=(MyDatas) stream.fromXML(result); list=(ArrayList<MyNews>) my.getNewslist().getNews(); list1=list; adapter=new MyBaseAdapter(MainActivity.this, list1); mPullRefreshListView.setAdapter(adapter); adapter.notifyDataSetChanged(); mPullRefreshListView.onRefreshComplete(); }else{ XStream stream=new XStream(); stream.processAnnotations(MyDatas.class); MyDatas my=(MyDatas) stream.fromXML(result); list=(ArrayList<MyNews>) my.getNewslist().getNews(); list1.addAll(list); adapter.notifyDataSetChanged(); mPullRefreshListView.onRefreshComplete(); } super.onPostExecute(result); mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Intent it=new Intent(MainActivity.this,InfoActivity.class); it.putExtra("title", list.get(arg2-1).getTitle()); it.putExtra("body", list.get(arg2-1).getBody()); System.out.println("intent=========="+it); startActivity(it); } }); } } }
标签:
原文地址:http://www.cnblogs.com/yr520/p/5496858.html