这是主Activity:
public class ListViewDownLoad extends Activity{
private ListView mListView;
private static String URL="www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
mListView=(ListView) findViewById(R.id.lv_main);
new NewsAsyncTask().execute(URL);
}
private String readStream(InputStream is){
InputStreamReader isr;
String result="";
try {
String line="";
isr = new InputStreamReader(is,"utf-8");
BufferedReader br=new BufferedReader(isr);
while((line = br.readLine())!=null){
result+=line;
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
class NewsAsyncTask extends AsyncTask<String,Void,List<NewsBean>>{
@Override
protected List<NewsBean> doInBackground(String... arg0) {
List<NewsBean> newsBeanList = new ArrayList<NewsBean>();
try {
String jsonString = readStream(new URL(arg0[0]).openStream());
JSONObject jsonObject;
NewsBean newsBean;
jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
newsBean = new NewsBean();
newsBean.newsIconUrl=jsonObject.getString("picSmall");
newsBean.newsTitle=jsonObject.getString("name");
newsBean.newsContent=jsonObject.getString("description");
newsBeanList.add(newsBean);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(newsBeanList);
return newsBeanList;
}
@Override
protected void onPostExecute(List<NewsBean> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
NewsAdapter adapter = new NewsAdapter(ListViewDownLoad.this, result);
mListView.setAdapter(adapter);
}
}
}
这是适配器:
public class NewsAdapter extends BaseAdapter{
private List<NewsBean> mList;
private LayoutInflater mInflater;
public NewsAdapter(Context context,List<NewsBean> data){
mList = data;
mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView==null) {
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_layout, null);
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.ivIcon.setImageResource(R.drawable.ic_launcher);
viewHolder.tvTitle.setText(mList.get(position).newsTitle);
viewHolder.tvContent.setText(mList.get(position).newsContent);
return convertView;
}
class ViewHolder{
public TextView tvTitle;
public TextView tvContent;
public ImageView ivIcon;
}
}
Faith_Insist
慕粉3720818
关耳金名
草鱼禾