public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
int page=2;
int count=15;
private Myadapter myadapter;
private XListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Wifitext.getnet(MainActivity.this)){
new anyc().execute();
Toast.makeText(MainActivity.this,"当前为WIFI",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"怎么非事,老弟啊!",Toast.LENGTH_LONG).show();
}
}
//下拉刷新
@Override
public void onRefresh() {
page=1;
new Runnable(){
@Override
public void run() {
new anyc().execute();
myadapter.notifyDataSetChanged();
}
}.run();
onLoad();
}
//上拉加载更多
@Override
public void onLoadMore() {
page++;
new Runnable(){
@Override
public void run() {
new anyc().execute();
myadapter.notifyDataSetChanged();
}
}.run();
onLoad();
}
public void onLoad(){
lv.stopLoadMore();
lv.stopRefresh();
lv.setRefreshTime(getTime());
}
//获取当前时间
public String getTime(){
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
return formatter.format(curDate);
}
class anyc extends AsyncTask<Integer,Integer,String>{
private View drawer;
@Override
protected String doInBackground(Integer... integers) {
String message="";
try {
String url="http://47.95.253.123/boot/api/findKtcon?pageNum="+page+"&pageSize="+count+"";
URL urll = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urll.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
InputStream stream = connection.getInputStream();
int len = 1;
byte[] bytes = new byte[512];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ((len = stream.read(bytes)) != -1) {
bout.write(bytes, 0, len);
}
message = bout.toString();
stream.close();
connection.disconnect();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return message;
}
@Override
protected void onPreExecute() {
lv = findViewById(R.id.listview);
lv.setXListViewListener(MainActivity.this);
lv.setPullLoadEnable(true);
lv.setPullRefreshEnable(true);
drawer = findViewById(R.id.drawerlayout);
}
@Override
protected void onPostExecute(String s) {
if (s!=null){
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
if (bean!=null){
myadapter = new Myadapter(MainActivity.this, bean);
lv.setAdapter(myadapter);
}
}
}
//适配器
public class Myadapter extends BaseAdapter { Context context; Bean bean; LayoutInflater minflater; final int TYPE0=0; final int TYPE1=1; public Myadapter(Context context, Bean bean) { this.context = context; this.bean = bean; minflater = LayoutInflater.from(context); } @Override public int getCount() { return bean.getResult().size(); } @Override public Object getItem(int i) { return bean.getResult().get(i); } @Override public long getItemId(int i) { return i; } @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { switch (position){ case 0: return TYPE0; case 1: return TYPE1; default: } return position%2; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder=new ViewHolder(); int type = getItemViewType(i); if (view==null){ switch (type){ case TYPE0: view=View.inflate(context,R.layout.item_listview,null); holder.lftv1= view.findViewById(R.id.lftextView1); holder.lfimage=view.findViewById(R.id.lf_imageView); view.setTag(holder); break; case TYPE1: view=View.inflate(context,R.layout.item_listviewr,null); holder.rttv1=view.findViewById(R.id.rttextview1); holder.rtimage=view.findViewById(R.id.rightimage); view.setTag(holder); } }else{ holder= (ViewHolder) view.getTag(); } switch (i){ case 0: holder.lftv1.setText(bean.getResult().get(i).getKtdetail()); ImageLoader.getInstance().displayImage(bean.getResult().get(i).getKtimg(),holder.lfimage); return view; case 1: holder.rttv1.setText(bean.getResult().get(i).getKtsection()); ImageLoader.getInstance().displayImage(bean.getResult().get(i).getKtimg(),holder.rtimage); return view; default: } return view; } class ViewHolder{ TextView rttv1,rttv2,lftv1,lftv2; ImageView rtimage,lfimage; }