新闻布局加其网络获取实现(ListView 的再次优化 )
2016-03-07 15:04
429 查看
主界面布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> <ListView android:id="@+id/list_news" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>ListView条目界面布局:
<?xml version="1.0" encoding="utf-8"?> <!--suppress AndroidDomInspection --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content">
<--------------------github框架获取网络图片框架----------------------> <sqlitetest.workharder.NewsTest.com.loopj.android.image.SmartImageViewandroid:id="@+id/news_icon"android:layout_height="120dp"android:layout_width="100dp"android:layout_centerVertical="true"android:src="@drawable/as"/> <TextViewandroid:id="@+id/news_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:singleLine="true"android:textSize="40sp"android:text="生活是以免好的建设领导开卡视角的发生了地方"android:layout_toRightOf="@id/news_icon" /> <TextViewandroid:id="@+id/detail"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toRightOf="@id/news_icon"android:textSize="25dp"android:text="laksjdflaksjdalksjd"android:layout_below="@id/news_title"android:textColor="@android:color/darker_gray"android:lines="2"/> <TextViewandroid:id="@+id/conment"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_below="@id/detail"android:text="13245条评论"android:textColor="#ff0000"/></RelativeLayout>
主界面代码实现:
public class MainActivity extends AppCompatActivity {
//由于如果将ListView定在UI主线程上,那么开机就会启动,但是BaseAdapter内部设置试图层时,模型层从网络获取数据是没有这么快的,会导致空指针指向问题; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); ListView listView= (ListView) findViewById(R.id.list_news); listView.setAdapter(new MyAdapter()); } }; List<News> list=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getNewsInfo(); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.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) {
/*
News为JavaBin保存获取网络数据的java文档
*/
News news=list.get(position); ViewHolder mHolder=null; View v=null; if(convertView==null){ v=View.inflate(MainActivity.this, R.layout.lv_news, null); //当没有缓存的时候,初始化mHolder mHolder =new ViewHolder(); mHolder.title= (TextView) findViewById(R.id.news_title); mHolder.detail=(TextView)findViewById(R.id.detail); mHolder.comment= (TextView) findViewById(R.id.conment); mHolder.imageView= (SmartImageView) findViewById(R.id.news_icon); //将所有组件信息保存至View组件中,v会加入缓存,那么MyHolder对象也会随之加入缓存 v.setTag(mHolder); }else{ v=convertView; //获取缓存内部的信息,就不用再次调用findViewById mHolder= (ViewHolder) v.getTag(); } TextView title= mHolder.title; String tv_title=news.getTitle(); title.setText(tv_title); TextView detail= mHolder.detail; String tv_detail=news.getDetail(); detail.setText(tv_detail); TextView comment=mHolder.comment; String tv_coment=news.getComment(); comment.setText(news.getComment() + "条评论"); //选用框架直接调用下载网络上的图片,而不是再重新开启子线程网上下载 SmartImageView imageView= mHolder.imageView; imageView.setImageUrl(news.getImageUrl()); return null; } //创建一个内部类优化ListView的条目刷新,当有缓存的时候,就不用再次调用findViewById(); //将ListView条目的所有组件都封装至ViewHolder类 class ViewHolder{ private TextView title; private TextView detail; private TextView comment; private SmartImageView imageView; } } public void getNewsInfo() { //先从网上获取新闻消息 final String path = new String("http://192.168.13.13:8080/news.xml"); Thread thread = new Thread() { @Override public void run() { try { URL url = new URL(path); //获取网络连接对象 HttpURLConnection connectionURLConnection = (HttpURLConnection) url.openConnection(); //请求数据的方法 connectionURLConnection.setRequestMethod("GET"); connectionURLConnection.setConnectTimeout(5000); connectionURLConnection.setReadTimeout(8000); connectionURLConnection.connect(); //如果获取成功 if (connectionURLConnection.getResponseCode() ==200) { InputStream is = connectionURLConnection.getInputStream(); //使用Pull解析器,解析这个流,解析方法 parseNewsXml(is); } } catch (Exception e) { e.printStackTrace(); } } }; thread.start(); //发消息让主线程处理ListView,如果消息不用携带数据,那么则发送空消息 handler.sendEmptyMessage(1); } private void parseNewsXml(InputStream inputStream){ //创建解码器 XmlPullParser xp= Xml.newPullParser(); //创建一个javabean对象 News news=null; try { xp.setInput(inputStream,"UTF-8"); //获取当前节点的事件类型 int type=xp.getEventType(); //对节点的事件类型进行判断,判断当前节点是什么节点 while(type != XmlPullParser.END_DOCUMENT){ switch (type){ case XmlPullParser.START_TAG: if("newslist".equals(xp.getName())){ //创建集合对象管理javabean list=new ArrayList<News>(); }else if ("news".equals(xp.getName())){ news=new News(); }else if("title".equals(xp.getName())){ //获取下一个节点的文本节点 news.setTitle(xp.nextText()); }else if("detail".equals(xp.getName())){ //获取下一个节点的文本节点 news.setDetail(xp.nextText()); }else if("comment".equals(xp.getName())){ //获取下一个节点的文本节点 news.setComment(xp.nextText()); }else if("image".equals(xp.getName())){ //获取下一个节点的文本节点 news.setImageUrl(xp.nextText()); } break; //只有一个结束节点是需要处理 case XmlPullParser.END_TAG: if ("news".equals(xp.getName())){ list.add(news); } break; } //将指针移动至下一个节点,并返回它的事件类型 type=xp.next(); } for(News news1:list){ System.out.println(news1.toString()); } } catch (Exception e) { e.printStackTrace(); } }}
相关文章推荐
- Android程序开发通过HttpURLConnection上传文件到服务器
- 安卓基础:Http之通过Get、Post传递数据
- 计算机网络基础
- OkHttp3升级实践与之前2.0对比
- android 获取不带后缀的网络图片
- linux上网络配置不生效的怪异现象处理
- PHP 5.6 开启CURL HTTPS 类型
- Linux 网络配置基础
- 一、网络知识介绍
- Windows平台下NS2网络仿真环境的搭建
- Xcode新建工程无网络状况解决办法
- 发起https和http请求
- TCP/IP网络编程模型
- 网络字节序与主机字节序
- 逾千万使用 https 的站点受到新型解密攻击的威胁
- Cocos2dx中的TestCPP运行Box2dTest
- Exception in thread "http-bio-8080-exec-1" PermGen space
- Java中List Set Map的区别(转载自网络)
- OkHttp的深入研究:强大的功能(四)
- 安卓第八天笔记--网络编程二