package com.tired.itemlist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView mlistView;
private static String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mlistView = (ListView) findViewById(R.id.lv_main);
new NewsAsyncTask().execute(URL);
}
private List<NewsBean> getJsonData(String url) {
List<NewsBean> newsBeanList = new ArrayList<NewsBean>();
try {
String jsonString = readStream(new java.net.URL(url).openStream());
JSONObject jsonObject;
NewsBean newsBean;
try {
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 (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newsBeanList;
}
private String readStream(InputStream is) {
InputStreamReader isr;
String result2 = "";
try {
String line = "";
isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
try {
while ((line = br.readLine()) != null) {
result2 += line;
Log.d("main", result2);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result2;
}
class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {
@Override
protected List<NewsBean> doInBackground(String... arg0) {
return getJsonData(arg0[0]);
}
@Override
protected void onPostExecute(List<NewsBean> result) {
super.onPostExecute(result);
NewsAdapter adapter = new NewsAdapter(MainActivity.this, result);
mlistView.setAdapter(adapter);
}
}
}
package com.tired.itemlist;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class NewsAdapter extends BaseAdapter {
private List<NewsBean> mList;
private LayoutInflater mInflater;
public NewsAdapter(Context context,List<NewsBean> data){
mInflater = LayoutInflater.from(context);
mList =data;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(arg1 ==null){
viewHolder =new ViewHolder();
arg1 =mInflater.inflate(R.layout.item_layout,null);
viewHolder.ivIcon =(ImageView) arg1.findViewById(R.id.icon);
viewHolder.tvTitle =(TextView) arg1.findViewById(R.id.tv_title);
viewHolder.tvContent = (TextView) arg1.findViewById(R.id.tv_content);
arg1.setTag(viewHolder);
}else{
viewHolder =(ViewHolder) arg1.getTag();
}
viewHolder.ivIcon.setImageResource(R.drawable.ic_launcher);
viewHolder.tvTitle.setText(mList.get(arg0).newsTitle);
viewHolder.tvContent.setText(mList.get(arg0).newsContent);
return arg1;
}
class ViewHolder{
public TextView tvTitle,tvContent;
public ImageView ivIcon;
}
}
package com.tired.itemlist;
public class NewsBean {
public String newsIconUrl;
public String newsTitle;
public String newsContent;
}