本文共 4702 字,大约阅读时间需要 15 分钟。
public class MainActivity extends Activity { private ListView listview_load; private View layout; // private LinearLayout layout; private MyBaseAdapter adapter; private List > data; private boolean isbottom;// 是否为最后一个数据标记 private String url = "http://m2.qiushibaike.com/article/list/suggest?page="; private int currentPage = 1;// 当前页码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listview_load = (ListView) this.findViewById(R.id.listview_load); layout = this.findViewById(R.id.layout); // layout = (LinearLayout) this.findViewById(R.id.layout); data = new ArrayList >(); adapter = new MyBaseAdapter(data); listview_load.setAdapter(adapter); new MyAsyncTask().execute(url + currentPage); listview_load.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE:// 停止时 if (isbottom) { currentPage++; // 加载下一页数据, 显示进度条 layout.setVisibility(View.VISIBLE); new MyAsyncTask().execute(url + currentPage); } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 到达底部最后一条,判断再加载数据 isbottom = ((firstVisibleItem + visibleItemCount) == totalItemCount); } }); } // 异步方法进行联网下载 class MyAsyncTask extends AsyncTask { @Override protected byte[] doInBackground(String... params) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(params[0]); try { Log.i("MainActivity", "-3-"); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toByteArray(response.getEntity()); } Log.i("MainActivity", "-4-"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } // 联网下载完后马上执行 @Override protected void onPostExecute(byte[] result) { if (result != null) { List > list = parserJSON(new String(result)); adapter.data.addAll(list);// 将数据给到data-list adapter.notifyDataSetChanged();// 会调用MyBaseAdapter中的getView方法 Log.i("MainActivity", "-list-" + list.size()); } else { Toast.makeText(MainActivity.this, "网络连接错误!!", Toast.LENGTH_SHORT).show(); } layout.setVisibility(View.GONE); } // JSON解析 private List > parserJSON(String json) { List > list = new ArrayList >(); try { JSONObject obj = new JSONObject(json); JSONArray array = obj.getJSONArray("items"); for (int i = 0; i < array.length(); i++) { JSONObject object_items = array.getJSONObject(i); Map itemMap = new HashMap (); itemMap.put("content", object_items.getString("content")); itemMap.put("image", object_items.getString("image")); itemMap.put("comments_count", object_items.getString("comments_count")); list.add(itemMap); } Log.i("MainActivity", "itemMap"); } catch (JSONException e) { e.printStackTrace(); } return list; } } // BaseAdapter适配器的代码填写 class MyBaseAdapter extends BaseAdapter { private List > data; public MyBaseAdapter(List > data) { this.data = data; } @Override public int getCount() { return this.data.size(); } @Override public Object getItem(int position) { return this.data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vHolder; if (convertView == null) { vHolder = new ViewHolder(); convertView = getLayoutInflater().inflate( R.layout.listview_item, null); TextView text_content = (TextView) convertView .findViewById(R.id.text_content); TextView text_image = (TextView) convertView .findViewById(R.id.text_image); TextView comments_count = (TextView) convertView .findViewById(R.id.comments_count); vHolder.text_content = text_content; vHolder.text_image = text_image; vHolder.comments_count = comments_count; convertView.setTag(vHolder); } else { vHolder = (ViewHolder) convertView.getTag(); } vHolder.text_content.setText(this.data.get(position).get("content") .toString()); vHolder.comments_count.setText(this.data.get(position) .get("comments_count").toString()); // 图片时另外判断,当然这里只是获取图片的名字,并没有下载图片,实际中文字和图片是分开下载的 if (!this.data.get(position).get("image").equals("null")) { vHolder.text_image.setText(this.data.get(position).get("image") .toString()); } return convertView; } class ViewHolder { private TextView text_content; private TextView text_image; private TextView comments_count; } }}//主布局 //适配器自定义布局
转载于:https://my.oschina.net/u/2541146/blog/603766