based on PullToRefresh
this essay describe how to refresh image
define two object (PullToRefreshListView , List) in class
private PullToRefreshListView lv;
List<Map<String, Object>> listem=new ArrayList<Map<String,Object>>();
in onCreate method , get the ListView
lv=(PullToRefreshListView) findViewById(R.id.mylv);
then define two array to show images and text
String[] name={"aa","bb"};
int[] imgid={R.drawable.a,R.drawable.e};
create loop and Map , put images and text in it
for(int i=0;i<name.length;i++){
Map<String, Object> listMap=new HashMap<String, Object>();
listMap.put("img", imgid[i]);
listMap.put("name", name[i]);
listem.add(listMap);
}
define SimpleAdapter object , and make it to show image and text in xml file
final SimpleAdapter adapter=new SimpleAdapter(this
,listem,R.layout.fragment_main
,new String[]{"img","name"},new int[]{R.id.head,R.id.name});
lv.setAdapter(adapter);
set ListView Listen to events and achieve those method
lv.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
//使用异步方式刷新
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result)
{
/*int i=(int) (Math.random()*3);
String jString=Integer.toString(i);
adapter.addAll("hello",jString);*/
//adapter.addAll(R.drawable.a,"cc");
//System.out.println(listem.size());
Map<String, Object> listMap=new HashMap<String, Object>();
listMap.put("img", R.drawable.a);
listMap.put("name", "c");
listem.add(listMap);
lv.onRefreshComplete();//完成刷新动作
};
}.execute();//执行下拉刷新操作
}
});
in the end , put my complete code
package com.example.usinglistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private PullToRefreshListView lv;
List