MainActivity
来源:互联网 发布:淘宝客户管理系统 编辑:程序博客网 时间:2024/05/31 19:05
package com.bwie.loaderimagelistviewframe;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
loadData("http://op.juhe.cn/yi18/news/list?key=3c0aa5802f5638a205efbe9940f01354");
}
private void loadData(String path) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
try {
String path = params[0];
System.out.println("path:" + path);
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("InputStream之前");
InputStream is = connection.getInputStream();
System.out.println("InputStream之后");
String json = StreamTools.readFromNetWork(is);
System.out.println(json);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
Gson gson = new Gson();
HealthInfo healthInfo = gson.fromJson(s, HealthInfo.class);
List<HealthInfo.ResultBean.ListBean> list = healthInfo.getResult().getList();
lv.setAdapter(new MyAdapter(list));
}
}
}.execute(path);
}
class MyAdapter extends BaseAdapter {
private List<HealthInfo.ResultBean.ListBean> list;
DisplayImageOptions option;
public MyAdapter(List<HealthInfo.ResultBean.ListBean> list) {
this.list = list;
option = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.loading)
.showImageOnFail(R.mipmap.ic_error)
.showImageForEmptyUri(R.mipmap.ic_empty)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
}
@Override
public int getCount() {
return list.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return list.get(position);
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.item, null);
}
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.iv);
TextView tvContent = (TextView) convertView.findViewById(R.id.tv);
tvContent.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImg(), ivIcon, option);
return convertView;
}
}
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
loadData("http://op.juhe.cn/yi18/news/list?key=3c0aa5802f5638a205efbe9940f01354");
}
private void loadData(String path) {
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
try {
String path = params[0];
System.out.println("path:" + path);
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("InputStream之前");
InputStream is = connection.getInputStream();
System.out.println("InputStream之后");
String json = StreamTools.readFromNetWork(is);
System.out.println(json);
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
Gson gson = new Gson();
HealthInfo healthInfo = gson.fromJson(s, HealthInfo.class);
List<HealthInfo.ResultBean.ListBean> list = healthInfo.getResult().getList();
lv.setAdapter(new MyAdapter(list));
}
}
}.execute(path);
}
class MyAdapter extends BaseAdapter {
private List<HealthInfo.ResultBean.ListBean> list;
DisplayImageOptions option;
public MyAdapter(List<HealthInfo.ResultBean.ListBean> list) {
this.list = list;
option = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.loading)
.showImageOnFail(R.mipmap.ic_error)
.showImageForEmptyUri(R.mipmap.ic_empty)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
}
@Override
public int getCount() {
return list.size();
}
/**
* Get the data item associated with the specified position in the data set.
*
* @param position Position of the item whose data we want within the adapter's
* data set.
* @return The data at the specified position.
*/
@Override
public Object getItem(int position) {
return list.get(position);
}
/**
* Get the row id associated with the specified position in the list.
*
* @param position The position of the item within the adapter's data set whose row id we want.
* @return The id of the item at the specified position.
*/
@Override
public long getItemId(int position) {
return position;
}
/**
* Get a View that displays the data at the specified position in the data set. You can either
* create a View manually or inflate it from an XML layout file. When the View is inflated, the
* parent View (GridView, ListView...) will apply default layout parameters unless you use
* {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
* to specify a root view and to prevent attachment to the root.
*
* @param position The position of the item within the adapter's data set of the item whose view
* we want.
* @param convertView The old view to reuse, if possible. Note: You should check that this view
* is non-null and of an appropriate type before using. If it is not possible to convert
* this view to display the correct data, this method can create a new view.
* Heterogeneous lists can specify their number of view types, so that this View is
* always of the right type (see {@link #getViewTypeCount()} and
* {@link #getItemViewType(int)}).
* @param parent The parent that this view will eventually be attached to
* @return A View corresponding to the data at the specified position.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(getApplicationContext(), R.layout.item, null);
}
ImageView ivIcon = (ImageView) convertView.findViewById(R.id.iv);
TextView tvContent = (TextView) convertView.findViewById(R.id.tv);
tvContent.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImg(), ivIcon, option);
return convertView;
}
}
}
阅读全文
0 0
- MainActivity
- mainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- MainActivity
- 自己动手写操作系统(五)
- 注册表收集
- 组合模式
- FZU
- MarkDown语法分析
- MainActivity
- Python 闭包
- Linux系统上安装JDK
- 前端面试题整理——CSS篇
- 学习akka之future
- #6 块参数
- 并发实战——原子类AtomicReference及底层源码CompareAndSwapObject分析
- Spark的Dataset操作(四)-其他单表操作
- 哈喽