package com.example.project2zhoukao1;
import java.util.List;
import java.util.concurrent.ExecutionException;
import com.bwie.adapter.Mbaseadapter;
import com.bwie.bean.JsondDataone;
import com.bwie.bean.Jsonone;
import com.google.gson.Gson;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gridView;
private String url = "http://m.yunifang.com/yunifang/mobile/goods/getall?random=9949&encode=6c2154232994e7d36ad461e3caa68ca";
private List<Jsonone> list;
private Mbaseadapter adapter;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
String str = (String) msg.obj;
getJson(str);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview_main);
getDatajson();
gridView.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,
ShowImageactivity.class);
intent.putExtra("ls", list.get(position).id);
startActivity(intent);
}
});
}
private void getDatajson() {
new Thread(new Runnable() {
@Override
public void run() {
// 请求网络数据
// TODO Auto-generated method stub
MAsynctask asynctask = new MAsynctask();
try {
String string = asynctask.execute(url).get();
Message msg = new Message();
msg.what = 1;
msg.obj = string;
handler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
private void getJson(String string) {
// TODO Auto-generated method stub
// 解析网络数据
Gson gson = new Gson();
JsondDataone fromJson = gson.fromJson(string, JsondDataone.class);
list = fromJson.data;
// Log.i("tga", list.toString());
adapter = new Mbaseadapter(list, getApplicationContext());
gridView.setAdapter(adapter);
}
}