//创建AsyncTask对象
AsyncTaskasyncTask = new AsyncTask() { @Override
protected void onPreExecute() {
super.onPreExecute();
}
//子线程运行的方法
@Override
protected String doInBackground(Void... params) {
//用httpclient请求数据
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse execute = defaultHttpClient.execute(httpGet);
StatusLine statusLine = execute.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = execute.getEntity();
InputStream content = entity.getContent();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int len = 0;
byte[] buffer = new byte[1024];
while ((len = content.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
//给主线程返回请求的数据
return byteArrayOutputStream.toString();
}
} catch (IOException e) {
e.printStackTrace();
} return null;
}
//主线程运行的方法
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//创建list集合
list = new ArrayList();
//gson解析
Gson gson = new Gson();
final myBean myBean = gson.fromJson(s, myBean.class);
// 把解析出来的内容存入list集合
list = myBean.data;
//找控件
ListView listview = (ListView) findViewById(R.id.listview);
//创建自定义适配器
myBaseAdapter adpter = new myBaseAdapter(list, MainActivity.this);
//设置适配器
listview.setAdapter(adpter);
//设置item的条目点击事件
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
//跳转到第二个页面
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//intent传值
intent.putExtra("IMAGEURL", list.get(position).IMAGEURL);
intent.putExtra("TITLE", list.get(position).TITLE);
intent.putExtra("FROMNAME", list.get(position).FROMNAME);
intent.putExtra("SHOWTIME", list.get(position).SHOWTIME);
intent.putExtra("SUBTITLE",list.get(position).SUBTITLE);
startActivity(intent);
}
});
}
};
asyncTask.execute();
}
}
//////////////////////////////////////////////youhua
if (convertView == null) {
holder = new viewHolder();
convertView = convertView.inflate(context, R.layout.item, null);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageview);
holder.title = (TextView) convertView.findViewById(R.id.TITLE);
holder.FROMNAME = (TextView) convertView.findViewById(R.id.FROMNAME);
holder.SHOWTIME = (TextView) convertView.findViewById(R.id.SHOWTIME);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
//这步骤是用imageloder加载图片的
http://www.jianshu.com/p/a8cbb228fb83
这个网址里有
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true) .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(displayImageOptions)
.build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(list.get(position).IMAGEURL, holder.imageView);
holder.title.setText(list.get(position).TITLE);
holder.FROMNAME.setText(list.get(position).FROMNAME);
holder.SHOWTIME.setText(list.get(position).SHOWTIME);
return convertView;
}
解析惊悚 ViewLoader
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
//这步骤是用imageloder加载图片的
http://www.jianshu.com/p/a8cbb228fb83
这个网址里有
DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true) .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(displayImageOptions)
.build();
ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(list.get(position).IMAGEURL, holder.imageView);
/////
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.but01:
//创建FragmentManager管理者
FragmentManager fragmentManager=getSupportFragmentManager();
//通过FragmentManager开启一个事务
FragmentTransaction transaction=fragmentManager.beginTransaction();
//加载fragment arg0 占位的控件 第二个参数是加载的Fragment
transaction.replace(R.id.fragment, new Fragment01());
//提交
transaction.commit();
break;
case R.id.but02:
FragmentManager fragmentManager2=getSupportFragmentManager();
FragmentTransaction transaction2=fragmentManager2.beginTransaction();
transaction2.replace(R.id.fragment, new Fragment02());
transaction2.commit();
break;
case R.id.but03:
FragmentManager fragmentManager3=getSupportFragmentManager();
FragmentTransaction transaction3=fragmentManager3.beginTransaction();
transaction3.replace(R.id.fragment, new Fragment03());
transaction3.commit();
break;
default:
break;
}
}
});
网友评论