BaseAdapter和ListView、異步下載,Json解析的簡單運用(08) public class MainActivity extends Activity { private ListView listview; // private SimpleAdapter adapter; private MyBaseAdapter baseadapter; @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 MyTask(this).execute(url); } class MyBaseAdapter extends BaseAdapter { private List<Map<String, Object>> list; public MyBaseAdapter(List<Map<String, Object>> list) { this.list = list; } @Override public int getCount() { return list.size(); } @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) { ViewHolder viewHolder; if (convertView == null) { viewHolder=new ViewHolder(); convertView = getLayoutInflater().inflate(R.layout.listview, null); TextView subject = (TextView) convertView .findViewById(R.id.subject); TextView summary = (TextView) convertView .findViewById(R.id.summary); TextView changed = (TextView) convertView .findViewById(R.id.changed); viewHolder.subject = subject; viewHolder.summary=summary; viewHolder.changed=changed; convertView.setTag(viewHolder); }else{ viewHolder=(ViewHolder) convertView.getTag(); } viewHolder.subject.setText(this.list.get(position).get("subject").toString()); viewHolder.summary.setText(this.list.get(position).get("summary").toString()); viewHolder.changed.setText(this.list.get(position).get("changed").toString()); return convertView; } class ViewHolder { private TextView subject; private TextView summary; private TextView changed; } } class MyTask extends AsyncTask<String, Void, byte[]> { private Context context; private ProgressDialog dialog; public MyTask(Context context) { this.context = context; dialog = new ProgressDialog(context); dialog.setTitle("提示"); dialog.setMessage("正在下載。。。"); } @Override protected void onPreExecute() { super.onPreExecute(); dialog.show(); } @Override protected byte[] doInBackground(String... params) { String url = params[0]; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response; try { response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toByteArray(response.getEntity()); // 這個是獲得內容:response.getEntity(); // 這個獲得的是轉換爲流response.getEntity().getContent(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(byte[] result) { if (result != null) { //解析JSON,講數組轉爲字符串 List<Map<String, Object>> list = parser(new String(result)); baseadapter = new MyBaseAdapter(list); listview.setAdapter(baseadapter); /* //context在simpleAdapter用時,要嘛用MainAcivity.this,不這樣的話要在構造函數那裏定義 //私有:private Context context; this.context = context; adapter = new SimpleAdapter(context, list, R.layout.listview, new String[] { "subject", "summary", "changed" }, new int[] { R.id.subject, R.id.summary, R.id.changed }); listview.setAdapter(adapter); */ } else { Toast.makeText(MainActivity.this, "網絡鏈接錯誤!!", Toast.LENGTH_SHORT).show(); } dialog.dismiss(); } private List<Map<String, Object>> parser(String json) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); JSONObject obj; try { obj = new JSONObject(json); JSONObject object_paramz = obj.getJSONObject("paramz"); JSONArray array_feeds = object_paramz.getJSONArray("feeds"); for (int i = 0; i < array_feeds.length(); i++) { JSONObject object = array_feeds.getJSONObject(i); JSONObject object_data = object.getJSONObject("data"); Map<String, Object> map = new HashMap<String, Object>(); map.put("subject", object_data.getString("subject")); map.put("summary", object_data.getString("summary")); map.put("changed", object_data.getString("changed")); list.add(map); } } catch (JSONException e) { e.printStackTrace(); } return list; } } } // 主佈局 <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> //適配器自定義佈局 <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" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/subject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#6699ff" android:textSize="30sp" /> <TextView android:id="@+id/summary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00cc00" android:textSize="20sp" /> <TextView android:id="@+id/changed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="13sp" /> </LinearLayout>