- 综合案例
- 案例设计到知识点:listview —>baseAdapter tomcat xml(xml解析) url httpurlconnection 开线程 handler
- 案例开发的流程
- 开发这样一个程序 最少3个人 一个android客户端,一个UI设计师,做服务器开发人员. 产品经理(微信).
- 代码实现过程:
- 画UI
-
- 搭建服务器 去服务器取数据
private void initData() { new Thread(new Runnable() { @Override public void run() { try { String path = "http://47.105.71.243:8080/resource/news.xml"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); mList = NewsXmlUtils.pasreXml(is); Log.d(TAG, "run: " + mList.size()); if (myAdapter!=null){ myAdapter.notifyDataSetChanged(); }else{ myAdapter = new myAdapter(); mLv.setAdapter(myAdapter); } } } catch (Exception e) { e.printStackTrace(); } } }).start(); }
-
- 对数据进行解析 xml解析
public static ListpasreXml(InputStream in) { List lists = null; News news = null; XmlPullParser parser = Xml.newPullParser(); try { parser.setInput(in, "utf-8"); int eventType = parser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_TAG: if ("channel".equals(parser.getName())) { lists = new ArrayList (); } else if ("item".equals(parser.getName())) { news = new News(); } else if ("title".equals(parser.getName())) { news.setTitle(parser.nextText()); } else if ("description".equals(parser.getName())) { news.setDescription(parser.nextText()); } else if ("image".equals(parser.getName())) { news.setImagepath(parser.nextText()); } else if ("type".equals(parser.getName())) { news.setType(parser.nextText()); } else if ("comment".equals(parser.getName())) { news.setComment(parser.nextText()); } break; case XmlPullParser.END_TAG: if ("item".equals(parser.getName())) { lists.add(news); } break; } eventType = parser.next(); } return lists; } catch (Exception e) { e.printStackTrace(); return null; } }
-
- 定义数据适配器
class myAdapter extends BaseAdapter { @Override public int getCount() { if (mList == null) { return 0; } return mList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.item, null); } News news = mList.get(position); SmartImageView iv = convertView.findViewById(R.id.iv); TextView tv = convertView.findViewById(R.id.tv); TextView tvs = convertView.findViewById(R.id.tvs); String imagepath = news.getImagepath(); Log.d(TAG, "getView: " + imagepath); iv.setImageUrl(imagepath); tv.setText(news.getTitle()); tvs.setText(news.getDescription()); return convertView; } }
-
- smartImageview
- 要先声明 要求这个类的完整包名和类名
- smartImageview
-
-
- 找的控件 展示数据
-
SmartImageView iv = convertView.findViewById(R.id.iv); iv.setImageUrl(imagepath);