news for Android

来源:互联网 发布:美国网络硕士 编辑:程序博客网 时间:2024/06/10 18:01

步骤一:activity_main.xml 添加ListView

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.itcast.wh17.netnews.MainActivity" >

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</RelativeLayout>

 步骤二:MainActivity.java 获取控件

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1、获取ListView
listView = (ListView) findViewById(R.id.lv);}

步骤三:NewsInfo.java  设置bean

public class NewsInfo {
private String id;
private String title;
private String content;
private String image;
public NewsInfo() {
super();
// TODO Auto-generated constructor stub
}
public NewsInfo(String id, String title, String content, String image) {
super();
this.id = id;
this.title = title;
this.content = content;
this.image = image;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the image
*/
public String getImage() {
return image;
}
/**
* @param image the image to set
*/
public void setImage(String image) {
this.image = image;
}}

步骤四:items.XML 设置items文件

<?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="match_parent" >


<com.loopj.android.image.SmartImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>

<TextView 
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_image"
android:textSize="20sp"
android:singleLine="true"
android:text="hello"
/>

<TextView 
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_image"
android:layout_below="@id/tv_title"
android:lines="2"
android:textColor="@android:color/darker_gray"
android:text="world"
/>

</RelativeLayout>

步骤五:MyAdapter.java  设置适配器

import java.util.List;

import com.loopj.android.image.SmartImageView;

import cn.itcast.wh17.netnews.R;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.itcast.wh17.netnews.bean.NewsInfo;

public class MyAdapter extends BaseAdapter {

private List<NewsInfo> newList;
private Context context;

public MyAdapter(List<NewsInfo> newList, Context context) {
super();
this.newList = newList;
this.context = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return newList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view=null;
if (convertView==null) {
view=View.inflate(context, R.layout.items, null);
}else{
view=convertView; 
}

//获取数据
NewsInfo newsInfo = newList.get(position);
//获取控件
SmartImageView iv_image = (SmartImageView) view.findViewById(R.id.iv_image);
TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
TextView tv_content = (TextView) view.findViewById(R.id.tv_content);

iv_image.setImageUrl(newsInfo.getImage()); 
tv_title.setText(newsInfo.getTitle());
tv_content.setText(newsInfo.getContent());


return view;
}

}

步骤六:MainActivity.java 下载xml文件

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1、获取ListView
listView = (ListView) findViewById(R.id.lv);


// 1、xml-->网络-->tomcat
// 网络代码
// 第一步:开权限、开线程

new Thread(new Runnable() {

@Override
public void run() {
String path = "http://10.0.2.2:8088/hello//news.xml";
// 第二步:
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);

if (connection.getResponseCode() == 200) {
// inputStream===>news.xml
InputStream inputStream = connection.getInputStream();
// xml的解析
List<NewsInfo> newInfoList = readXML(inputStream);//未完成,下面继续

步骤七:定义 rendXML方法

public List<NewsInfo> readXML(InputStream inputStream) {
List<NewsInfo> newList = null;
NewsInfo info = null;
XmlPullParser pullParser = Xml.newPullParser();
try {
pullParser.setInput(inputStream, "utf-8");

int eventType = pullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {

switch (eventType) {
case XmlPullParser.START_TAG:

if (pullParser.getName().equals("news")) {
// 创建集合
newList = new ArrayList<NewsInfo>();

} else if (pullParser.getName().equals("new")) {
info = new NewsInfo();
info.setId(pullParser.getAttributeValue(0));

} else if (pullParser.getName().equals("tilte")) {
info.setTitle(pullParser.nextText());

} else if (pullParser.getName().equals("content")) {
info.setContent(pullParser.nextText());
} else if (pullParser.getName().equals("image")) {
info.setImage(pullParser.nextText());
}
break;

case XmlPullParser.END_TAG:
if (pullParser.getName().equals("new")) {
// 添加元素
newList.add(info);

}
break;

default:
break;
}

eventType = pullParser.next();
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return newList;

}

步骤八:得到刚刚的一个集合后,发送消息

//接着刚刚断点继续

Message message = Message.obtain();
message.what = SUCCESS_DOWNLOAD_XML;
message.obj = newInfoList;

mHandler.sendMessage(message);

}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}).start();

}

步骤九:new Handler(){};

Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESS_DOWNLOAD_XML:

List<NewsInfo> newList=(List<NewsInfo>) msg.obj;
//ge 
MyAdapter adapter = new MyAdapter(newList, MainActivity.this);
listView.setAdapter(adapter);
break; 
default:
break;
}

};
};

总体思路:用网络代码下载XML解析,将数据用消息机制发送给适配器,适配器将数据、布局整合后set到ListView

用到的image view 工具

android-smart-image-view-master  jar包

0 0