一般安卓在学到异步任务AsyncTask之后都会有个安卓小项目的任务。得到(荔枝新闻,茶百科等)新闻网络接口来解析网络图片或文字到ListView组件上显示。其中要使用到的知识大概有:获取网络数据(HttpUtil),解析网络数据(NewsParse),防止因解析超时应用程序无响应(ANR:Application Not Responding) 的异步任务(AsyncTask),还有一个自定义的适配器(NewsAdapter),还有就是实例化AsyncTask类传递路径进行解析加载的MainActivity了。剩下的就是两个xml了,一个是主方法的。一个是ListView的自定义布局xml,。本次博客就不讲解点击ListView后加载详情页面了。
先上图看看:
两个布局文件:
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="琦天下新闻"
android:textColor="#ffffff"
android:paddingTop="15dp"
android:paddingLeft="110dp"
android:background="#009999"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cccccc"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1" >
</ListView>
</RelativeLayout>