three1
package com.bwie.yd;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.bwie.ada.MyAdapter;
import com.bwie.ada.MyAdapter_lv;
import com.bwie.utils.NetWorkUtil;
import com.bwie.vo.SuperClass_2;
import com.bwie.vo.SuperClass_3;
import com.bwie.vo.SuperClass_vp;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.BitmapUtils;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
public class MainActivity extends Activity {
private ViewPager vp;
private GridView gv;
private ListView lv;
private static final String URL_1 = "http://172.17.29.120/localuser/ljy/jc/ad.json";
private static final String URL_2 = "http://172.17.29.120/localuser/ljy/jc/nav.json";
private static final String URL_3 = "http://172.17.29.120/localuser/ljy/jc/alluser.json";
private List<SuperClass_vp> list_vp;
private List<ImageView> imgs;
private List<SuperClass_2> class_2;
private List<SuperClass_3> class_3;
int count = 0;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int i = msg.what;
if (i == 1) {
// 为轮播图设置适配器
setAdap();
// 设置轮播
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
count++;
if (count == 5) {
count = 0;
}
handler.sendEmptyMessage(3);
}
}, 1000, 2000);
}
if (i == 2) {
// 为gv设置适配器
gv.setAdapter(new MyAdapter(MainActivity.this, class_2));
}
if (i == 3) {
vp.setCurrentItem(count);
}
if (i == 4) {
lv.setAdapter(new MyAdapter_lv(MainActivity.this, class_3));
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 寻找控件
findView();
// 为轮播图准备数据
prepareData();
// 解析json
getData_m();
// 为listview准备数据
getData_li();
}
private void getData_li() {
new Thread() {
public void run() {
String data_li = NetWorkUtil.getdataByClient(URL_3);
Gson g = new Gson();
Type type = new TypeToken<List<SuperClass_3>>() {
}.getType();
class_3 = g.fromJson(data_li, type);
handler.sendEmptyMessage(4);
};
}.start();
}
private void getData_m() {
new Thread() {
public void run() {
String data_mi = NetWorkUtil.getdataByClient(URL_2);
Gson g = new Gson();
Type type = new TypeToken<List<SuperClass_2>>() {
}.getType();
class_2 = g.fromJson(data_mi, type);
System.out.println(class_2.get(0).getName());
handler.sendEmptyMessage(2);
};
}.start();
}
private void setAdap() {
vp.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imgs.size();
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
container.removeView(imgs.get(position));
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
container.addView(imgs.get(position));
return imgs.get(position);
}
});
}
private void prepareData() {
// 请求网络数据
new Thread() {
public void run() {
String vp_data = NetWorkUtil.getdataByClient(URL_1);
// 解析获取的json文件
Gson g = new Gson();
Type type = new TypeToken<List<SuperClass_vp>>() {
}.getType();
list_vp = g.fromJson(vp_data, type);
// 循环为控件赋值
BitmapUtils bit = new BitmapUtils(MainActivity.this);
imgs = new ArrayList<ImageView>();
for (int i = 0; i < list_vp.size(); i++) {
ImageView img = new ImageView(MainActivity.this);
bit.display(img, "http://172.17.29.120/localuser/ljy/jc/"
+ list_vp.get(i).getSrc());
imgs.add(img);
}
handler.sendEmptyMessage(1);
};
}.start();
}
private void findView() {
vp = (ViewPager) findViewById(R.id.vp);
gv = (GridView) findViewById(R.id.gv);
lv = (ListView) findViewById(R.id.lv);
}
}