Mainactivity
public class MainActivity extends Activity {
private String url = "http://apis.juhe.cn/goodbook/catalog?key=9d6ef8c31647a206e05fcaff70527182&dtype=xml";
private List<Xmlarray> list;
private ListView lv;
// 定义一个变量,来标识是否退出
// 实现两次点击退出
private static boolean isExit = false;
private static final String TAG = MainActivity.class.getSimpleName();
private static Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv_main);
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.i("TAG", "请求失败了");
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
// Log.i("TAG", arg0.result.toString());
XStream stream = new XStream();
stream.processAnnotations(XmlRoot.class);
XmlRoot xroot = (XmlRoot) stream.fromXML(arg0.result);
list = xroot.getResult().getItem();
// Log.i("TAG", list.toString());
lv.setAdapter(new XmlAdapter(list, getApplicationContext()));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
ItemActivitytwo.class);
intent.putExtra("lsid", list.get(position).getId());
startActivity(intent);
}
});
}
});
}
// 重写onKeyDown方法
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
if (!isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "再按一次后退键退出程序",
Toast.LENGTH_SHORT).show();
// 利用handler延迟发送更改状态信息
// 时间2s
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
Log.e(TAG, "exit application");
this.finish();
}
}
Itemactivity类
public class ItemActivitytwo extends Activity {
private int index = 0;
private String xmlid;
private List<Jsonarray> list;
private GridView gridView;
private JsonAdapter adapter;
private PullToRefreshGridView mPullRefreshGridView;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.itemactivity);
Intent intent = getIntent();
xmlid = intent.getStringExtra("lsid");
// 获得PullToRefreshGridView
mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
gridView = mPullRefreshGridView.getRefreshableView();
gethttp();
// 实现上拉刷新与下拉加载
mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2() {
@SuppressWarnings("rawtypes")
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
gethttp();
}
@SuppressWarnings("rawtypes")
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
gethttp();
}
});
}
private void gethttp() {
// TODO Auto-generated method stub
String url = "http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&catalog_id="
+ xmlid + "&pn=5&rn=" + index + "";
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.POST, url, new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.i("TAG", "请求失败啦");
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// TODO Auto-generated method stub
// gson解析
Gson gson = new Gson();
JsonResult fromJson = gson.fromJson(arg0.result,
JsonResult.class);
list = fromJson.getResult().getData();
Log.i("TAG", list.toString());
adapter = new JsonAdapter(list, getApplicationContext());
gridView.setAdapter(adapter);
index = index++;
// 适配器刷新
adapter.notifyDataSetChanged();
mPullRefreshGridView.onRefreshComplete();
}
});
}
}
自定义Application实现加载圆形图片
public class MyApliation extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
// 系统的
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(getApplicationContext());
ImageLoader.getInstance().init(configuration);
}
public static DisplayImageOptions getOptions() {
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(100)).build();
return displayImageOptions;
}
}
//Adapter加载图片
}
ImageLoader.getInstance().displayImage(list.get(position).getImg(),
vh.iamge, MyApliation.getOptions());
//bitmap加载图片
// BitmapUtils bitmapUtils = new BitmapUtils(context);
// bitmapUtils.display(vh.iamge, list.get(position).getImg());