@XStreamAlias("result")
public class Myresult {
@XStreamImplicit(itemFieldName="item")
package com.example.yuekao1;
import java.util.ArrayList;
import java.util.List;
import com.baidu.net.Network;
import com.baidu.vo1.Good;
import com.baidu.vo1.Mydata;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.KeyEvent;
import android.widget.GridView;
import android.widget.Toast;
public class Show extends Activity implements OnRefreshListener2<GridView> {
private String name;
private PullToRefreshGridView gv;
private List<Mydata> data;
private Base1 b1;
List<Mydata> list = new ArrayList<Mydata>();
private Exit exit;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
gv = (PullToRefreshGridView) findViewById(R.id.gv);
new Asy().execute();
gv.setOnRefreshListener(this);
exit = new Exit();
Intent in = getIntent();
name = in.getStringExtra("id");
}
class Asy extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... params) {
String s1 = Network
.getjson("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&dtype=gson&rn=10&pn=10&catalog_id="
+ name);
return s1;
}
protected void onPostExecute(String result) {
Gson g = new Gson();
Good json = g.fromJson(result, Good.class);
data = json.getResult().getData();
list = data;
b1 = new Base1(list, Show.this);
gv.setAdapter(b1);
gv.onRefreshComplete();
initIndicator();
}
}
class Asynt extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... params) {
String s1 = Network
.getjson("http://apis.juhe.cn/goodbook/query?key=9d6ef8c31647a206e05fcaff70527182&dtype=gson&rn=10&pn=10&catalog_id="
+ name);
return s1;
}
protected void onPostExecute(String result) {
Gson g = new Gson();
Good json = g.fromJson(result, Good.class);
List<Mydata> data2 = json.getResult().getData();
list.addAll(data2);
b1.notifyDataSetChanged();
gv.onRefreshComplete();
initIndicator();
}
}
public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
new Asy().execute();
}
public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
new Asynt().execute();
}
private void initIndicator() {
String label = DateUtils.formatDateTime(getApplicationContext(),
System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
gv.getLoadingLayoutProxy().setLastUpdatedLabel(label);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
pressAgainExit();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void pressAgainExit() {
if (exit.isExit()) {
finish();
} else {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
exit.doExitInOneSecond();
}
}
}
package com.example.yuekao1;
import android.os.Handler;
import android.os.HandlerThread;
public class Exit {
private boolean isExit = false;
private Runnable task = new Runnable() {
@Override
public void run() {
isExit = false;
}
};
public void doExitInOneSecond() {
isExit = true;
HandlerThread thread = new HandlerThread("doTask");
thread.start();
new Handler(thread.getLooper()).postDelayed(task, 1000);
}
public boolean isExit() {
return isExit;
}
public void setExit(boolean isExit) {
this.isExit = isExit;
}
}
<com.handmark.pulltorefresh.library.PullToRefreshGridView
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/gv"
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:numColumns="3"
android:smoothScrollbar="true"
ptr:ptrMode="both" >
</com.handmark.pulltorefresh.library.PullToRefreshGridView>
in.putExtra("id", item.get(arg2).getId());