ListView和SimPleteAdapter 把新聞數據綁定到ListViewandroid
//佈局界面有2個xml文件 1 -- ListView -- activity_main.xml
2 -- 新聞數據內容 -- item_activity.xmljson
//記得添加聯網權限api
一、activity_main.xml文件 佈局ide
代碼工具
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>佈局
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>this
</LinearLayout>url
---------------xml
item_activity.xml文件 佈局get
代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/subject"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"/>
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"/>
<TextView
android:id="@+id/changed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"/>
</LinearLayout>
----------------------
二、MainActivity 類
代碼
public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this.findViewById(R.id.listview);
String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=20&pageIndex=1&val=100511D3BE5301280E0992C73A9DEC41";
new MyAsyncTask().execute(url);
}
//啓動工具類 進行聯網下載數據操做
class MyAsyncTask extends AsyncTask<String, Void, byte[]>{
@Override protected byte[] doInBackground(String... params) { String url = params[0]; HttpGet get = new HttpGet(url); HttpClient client = new DefaultHttpClient(); try { HttpResponse response = client.execute(get); if(response.getStatusLine().getStatusCode() == 200){ return EntityUtils.toByteArray(response.getEntity()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(byte[] result) { if(result != null){ //把下載好的數據用json解析 把須要的數據解析出來 方法 -- jsonObject List<Map<String, Object>> data = jsonObject(new String(result)); //把須要的數據解析好 返回來 而後 裝在一個集合裏//把裝在集合裏的數據丟到自定義的適配器裏 adapter = new SimpleAdapter(MainActivity.this, data , R.layout.item_activity, new String[]{"subject","summary","changed"}, new int[]{R.id.subject,R.id.summary,R.id.changed}); //把適配器綁定到ListView listview.setAdapter(adapter); } } private List<Map<String, Object>> jsonObject(String string) { List<Map<String, Object>> data_list = new ArrayList<Map<String, Object>>(); try { JSONObject obj = new JSONObject(string); JSONObject obj_paramz = obj.getJSONObject("paramz"); JSONArray array_feeds = obj_paramz.getJSONArray("feeds"); for(int i = 0;i<array_feeds.length();i++){ JSONObject object = array_feeds.getJSONObject(i); JSONObject obj_data = object.getJSONObject("data"); Map<String,Object> map = new HashMap<String, Object>(); map.put("subject", obj_data.getString("subject")); map.put("summary", obj_data.getString("summary")); map.put("changed", obj_data.getString("changed")); data_list.add(map); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return data_list; } }}