Android开发----Listview刷新的相关代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android开发----Listview刷新的相关代码相关的知识,希望对你有一定的参考价值。
Mainactity
public class MainActivity extends ListActivity {
static final int MENU_MANUAL_REFRESH = 0;
static final int MENU_DISABLE_SCROLL = 1;
static final int MENU_SET_MODE = 2;
static final int MENU_DEMO = 3;
int index=1;
private PullToRefreshListView mPullRefreshListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);//1
new GetDataTask().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"+index);
// Set a listener to be invoked when the list should be refreshed.下拉刷新处理
mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { //2
@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);//3.刷新界面处理代理,显示新的时间
// Do work to refresh the list here.
new GetDataTask().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"+index); //4.执行自定义AsyncTask,进行数据的请求刷新
}
});
// Add an end-of-list listener //5.设置上拉加载处理
mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
new GetDataTask().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/"+index);
Toast.makeText(MainActivity.this, "正在加载", 1).show();
}
});
}
private class GetDataTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// Simulates a background job.
String json = null;
try {
Thread.sleep(4000);
HttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(params[0]);
try {
HttpResponse execute = httpClient.execute(httpGet);
int statusCode = execute.getStatusLine().getStatusCode();
if(statusCode==200){
HttpEntity entity = execute.getEntity();
json = EntityUtils.toString(entity);
Log.i("aaa",json);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException e) {}
return json;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Gson gson=new Gson();
final List_news fromJson = gson.fromJson(result,List_news.class);
Log.i("bbb",fromJson.getData().toString());
//mListItems.addFirst("Added after refresh...");
MyAdapter adapter=new MyAdapter(fromJson.getData(), getApplicationContext());
ListView actualListView = mPullRefreshListView.getRefreshableView();
actualListView.setAdapter(adapter);
actualListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,DetailsActivity.class);
intent.putExtra("list", fromJson.getData().get(arg2));
startActivity(intent);
}
});
adapter.notifyDataSetChanged();
index= index+1;
// Call onRefreshComplete when the list has been refreshed.
mPullRefreshListView.onRefreshComplete();
}
}
}
DetailsActivity
public class DetailsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show_detail);
Intent intent = getIntent();
News news=(News) intent.getSerializableExtra("list");
ImageView imageView=(ImageView) findViewById(R.id.image_show);
TextView textView=(TextView) findViewById(R.id.text_show);
textView.setText(news.getNews_title());
BitmapUtils bitmapUtils=new BitmapUtils(getApplicationContext());
bitmapUtils.display(imageView, news.getPic_url());
}
}
Baseadapter
public class MyAdapter extends BaseAdapter {
List<News> list;
Context context;
public MyAdapter(List<News> list, Context context) {
super();
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView==null){
viewHolder=new ViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.list_view, null);
viewHolder.textView=(TextView) convertView.findViewById(R.id.text);
viewHolder.imageView=(ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position).getNews_title());
BitmapUtils bitmapUtils=new BitmapUtils(context);
bitmapUtils.display(viewHolder.imageView,list.get(position).getPic_url());
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}
分装类
public class List_news {
List<News> data;
public List<News> getData() {
return data;
}
public void setData(List<News> data) {
this.data = data;
}
}
布局页面main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bwie.sx.MainActivity" >
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pull_refresh_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="4dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:smoothScrollbar="true" />
</RelativeLayout>
public class News implements Serializable{
String news_title;
String pic_url;
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
以上是关于Android开发----Listview刷新的相关代码的主要内容,如果未能解决你的问题,请参考以下文章
android开发:列表listview的实现 | 下拉刷新
android开发中appwidget 中的listView的数据怎么刷新
android解决ScrollView嵌套ListView不能下拉刷新