1、简单新闻客户端

<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" > <ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </RelativeLayout>
android:singleLine="true"   textview设置了此属性表示只显示一行,会自动增加...
android:lines="2"   只显示2行,多余的文字被截取。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <com.loopj.android.image.SmartImageView
android:id="@+id/siv"
android:layout_width="90dp"
android:layout_height="70dp"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"

/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题阿克江还得靠就阿訇多空间阿红的卡口"
android:textSize="18sp"
android:layout_toRightOf="@id/siv"
android:singleLine="true"

/>
<TextView
android:id="@+id/tv_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="详细阿红的卡上的空间阿红的科技爱好是三路口见大口径的空间啊顺丰的会计师费空间是否会计师"
android:textColor="@android:color/darker_gray"
android:textSize="15sp"
android:layout_below="@id/tv_title"
android:layout_toRightOf="@id/siv"
android:lines="2"

/>
<TextView
android:id="@+id/tv_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12323评论"
android:textColor="#ff0000"
android:layout_alignParentRight="true"
android:layout_below="@id/tv_detail"
/>
</RelativeLayout>
package com.itheima.mrsbsclient.domain;

public class News {

    private String title;
private String detail;
private String comment;
private String imageUrl;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
public String toString() {
return "News [title=" + title + ", detail=" + detail + ", comment="
+ comment + ", imageUrl=" + imageUrl + "]";
} }
package com.itheima.mrsbsclient;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List; import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException; import com.itheima.mrsbsclient.domain.News;
import com.loopj.android.image.SmartImageView; import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity { List<News> newsList; Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(new MyAdapter());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); getNewsInfo(); // ListView lv = (ListView) findViewById(R.id.lv);
//此时新闻信息还没有解析完毕,newsList还没有new出来
// lv.setAdapter(new MyAdapter());
} class MyAdapter extends BaseAdapter{
//返回的要显示的条目的数量
@Override
public int getCount() {
// TODO Auto-generated method stub
return newsList.size();
} //返回一个View对象,会作为ListView的一个条目显示在界面上
// 当条目划出屏幕时,系统会把该条目缓存至内存,当该条目再次进入屏幕,系统在重新调用getView时会把缓存的条目作为convertView参数传入,但是 //传入的条目不一定是之前被缓存的该条目,即系统有可能在调用getView方法获取第一个条目时,传入任意一个条目的缓存
@Override
public View getView(int position, View convertView, ViewGroup parent) {
News news = newsList.get(position); View v = null;
ViewHolder mHolder = null;
if(convertView == null){
//如何填充的
v = View.inflate(MainActivity.this, R.layout.item_listview, null); //创建viewHoler封装所有条目使用的组件,不能在此赋值因为只要内存中有条目缓存,在新的条目出现时,就会使用缓存,所以每次要重新设置 //条目的值
mHolder = new ViewHolder();
mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);
mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);
mHolder.siv = (SmartImageView) v.findViewById(R.id.siv); //把viewHolder封装至view对象中,这样view被缓存时,viewHolder也就被缓存了
v.setTag(mHolder);
}
else{
v = convertView;
//从view中取出保存的viewHolder,viewHolder中就有所有的组件对象,不需要再去findViewById
mHolder = (ViewHolder) v.getTag();
}
//给条目中的每个组件设置要显示的内容
mHolder.tv_title.setText(news.getTitle());
mHolder.tv_detail.setText(news.getDetail());
mHolder.tv_comment.setText(news.getComment() + "条评论"); mHolder.siv.setImageUrl(news.getImageUrl());
return v;
} //把条目需要使用到的所有组件封装在这个类中
class ViewHolder{
TextView tv_title;
TextView tv_detail;
TextView tv_comment;
SmartImageView siv;
} @Override
public Object getItem(int position) {
return newsList.get(position);
} @Override
public long getItemId(int position) {
return position;
} } private void getNewsInfo() {
Thread t = new Thread(){
@Override
public void run() {
String path = "http://169.254.244.136:8080/news.xml";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000); if(conn.getResponseCode() == 200){
//流里的信息是一个xml文件的文本信息,用xml解析器去解析,而不要作为文本去解析
InputStream is = conn.getInputStream();
getNewsFromStream(is); }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
t.start();
} private void getNewsFromStream(InputStream is) {
//从XML对象中获取XML文件解析器对象。
XmlPullParser xp = Xml.newPullParser();
try {
xp.setInput(is, "utf-8"); //获取事件类型,通过事件类型判断出当前解析的是和什么节点
int type = xp.getEventType(); News news = null;
while(type != XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
if("newslist".equals(xp.getName())){
newsList = new ArrayList<News>();
}
else if("news".equals(xp.getName())){
news = new News();
}
else if("title".equals(xp.getName())){
String title = xp.nextText();
news.setTitle(title);
}
else if("detail".equals(xp.getName())){
String detail = xp.nextText();
news.setDetail(detail);
}
else if("comment".equals(xp.getName())){
String comment = xp.nextText();
news.setComment(comment);
}
else if("image".equals(xp.getName())){
String image = xp.nextText();
news.setImageUrl(image);
} break;
case XmlPullParser.END_TAG:
if("news".equals(xp.getName())){
newsList.add(news);
}
break; }
//指针移动到下一个节点并返回事件类型
type = xp.next();
} //发送消息,让主线程刷新listview
handler.sendEmptyMessage(1);
// for (News n : newsList) {
// System.out.println(n.toString());
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
04-28 10:07