private ListView listview;
private List<MenuInfo.ListBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
listview = (ListView) findViewById(R.id.lv);
//调用方法
read("http://qhb.2dyt.com/Bwei/news?page=1&type=7&postkey=ad1AK");
}
//定义一个有参方法
public void read(String path){
new AsyncTask<String, Void, String>() {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//判断
if (s!=null){
//实例化
Gson gson=new Gson();
MenuInfo menuInfo = gson.fromJson(s, MenuInfo.class);
//参加集合
list = menuInfo.getList();
//做适配器
listview.setAdapter(new myadapter());
//点击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.putExtra("image",list.get(position).getPic());
intent.putExtra("title",list.get(position).getTitle());
//跳转
startActivity(intent);
}
});
}
}
@Override
protected String doInBackground(String... params) {
try {
//看做成一个数组
String path=params[0];
//实例化
URL url=new URL(path);
//实例化HttpURLConnection方法
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
//请求方法
connection.setRequestMethod("GET");
//链接时间
connection.setConnectTimeout(5000);
//读取时间
connection.setReadTimeout(5000);
//得到响应码
int code = connection.getResponseCode();
if (code==200){
//得到json
InputStream is = connection.getInputStream();
String json = SteaInfo.add(is);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(path);
}
//继承BaseAdapter
class myadapter extends BaseAdapter{
private DisplayImageOptions options;
//得到个数
@Override
public int getCount() {
options= new DisplayImageOptions.Builder()
.cacheInMemory(true).cacheOnDisk(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
return list!=null?list.size():0;
}
//得到内容
@Override
public Object getItem(int position) {
return list.get(position);
}
//得到索引
@Override
public long getItemId(int position) {
return position;
}
//重要方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//判断
if (convertView==null){
//找到xml
convertView=convertView.inflate(MainActivity.this,R.layout.item,null);
}
//找到控件
ImageView image = (ImageView) convertView.findViewById(R.id.iv);
TextView tv = (TextView) convertView.findViewById(R.id.tv);
//得到标题
tv.setText(list.get(position).getTitle());
//添加图片
ImageLoader.getInstance().displayImage(list.get(position).getPic(),image,options);
return convertView;
}
}
public class SecondActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//找到控件
final ImageView im = (ImageView) findViewById(R.id.im);
TextView tv = (TextView) findViewById(R.id.tev);
Intent intent = getIntent();
final String image = intent.getStringExtra("image");
String title = intent.getStringExtra("title");
tv.setText(title);
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
.build();
ImageLoader.getInstance().init(configuration);
ImageLoader.getInstance().loadImage(image, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
//失败设置系统图片
im.setImageResource(R.mipmap.ic_launcher);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//成功调用图片转换把图片变为圆形
//把图片设置上去
im.setImageBitmap(loadedImage);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}