作者:廖汉林1026_843 | 来源:互联网 | 2023-09-14 09:09
1、简单新闻客户端
<RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"tools:context&#61;".MainActivity" ><ListViewandroid:id&#61;"&#64;&#43;id/lv"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"/>RelativeLayout>
android:singleLine&#61;"true" textview设置了此属性表示只显示一行&#xff0c;会自动增加...
android:lines&#61;"2" 只显示2行&#xff0c;多余的文字被截取。
xml version&#61;"1.0" encoding&#61;"utf-8"?>
<RelativeLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content" ><com.loopj.android.image.SmartImageView android:id&#61;"&#64;&#43;id/siv"android:layout_width&#61;"90dp"android:layout_height&#61;"70dp"android:src&#61;"&#64;drawable/ic_launcher"android:layout_centerVertical&#61;"true"/><TextViewandroid:id&#61;"&#64;&#43;id/tv_title" android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:text&#61;"标题阿克江还得靠就阿訇多空间阿红的卡口"android:textSize&#61;"18sp"android:layout_toRightOf&#61;"&#64;id/siv"android:singleLine&#61;"true"/><TextView android:id&#61;"&#64;&#43;id/tv_detail"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:text&#61;"详细阿红的卡上的空间阿红的科技爱好是三路口见大口径的空间啊顺丰的会计师费空间是否会计师"android:textColor&#61;"&#64;android:color/darker_gray"android:textSize&#61;"15sp"android:layout_below&#61;"&#64;id/tv_title"android:layout_toRightOf&#61;"&#64;id/siv"android:lines&#61;"2"/><TextView android:id&#61;"&#64;&#43;id/tv_comment"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"12323评论"android:textColor&#61;"#ff0000"android:layout_alignParentRight&#61;"true"android:layout_below&#61;"&#64;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 &#61; title;}public String getDetail() {return detail;}public void setDetail(String detail) {this.detail &#61; detail;}public String getComment() {return comment;}public void setComment(String comment) {this.comment &#61; comment;}public String getImageUrl() {return imageUrl;}public void setImageUrl(String imageUrl) {this.imageUrl &#61; imageUrl;}&#64;Overridepublic String toString() {return "News [title&#61;" &#43; title &#43; ", detail&#61;" &#43; detail &#43; ", comment&#61;"&#43; comment &#43; ", imageUrl&#61;" &#43; imageUrl &#43; "]";}}
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 newsList;Handler handler &#61; new Handler(){public void handleMessage(android.os.Message msg) {ListView lv &#61; (ListView) findViewById(R.id.lv);lv.setAdapter(new MyAdapter());}};&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getNewsInfo();// ListView lv &#61; (ListView) findViewById(R.id.lv);//此时新闻信息还没有解析完毕&#xff0c;newsList还没有new出来
// lv.setAdapter(new MyAdapter());
}class MyAdapter extends BaseAdapter{//返回的要显示的条目的数量
&#64;Overridepublic int getCount() {// TODO Auto-generated method stubreturn newsList.size();}//返回一个View对象&#xff0c;会作为ListView的一个条目显示在界面上
// 当条目划出屏幕时&#xff0c;系统会把该条目缓存至内存&#xff0c;当该条目再次进入屏幕&#xff0c;系统在重新调用getView时会把缓存的条目作为convertView参数传入&#xff0c;但是 //传入的条目不一定是之前被缓存的该条目&#xff0c;即系统有可能在调用getView方法获取第一个条目时&#xff0c;传入任意一个条目的缓存
&#64;Overridepublic View getView(int position, View convertView, ViewGroup parent) {News news &#61; newsList.get(position);View v &#61; null;ViewHolder mHolder &#61; null;if(convertView &#61;&#61; null){//如何填充的v &#61; View.inflate(MainActivity.this, R.layout.item_listview, null);//创建viewHoler封装所有条目使用的组件&#xff0c;不能在此赋值因为只要内存中有条目缓存&#xff0c;在新的条目出现时&#xff0c;就会使用缓存&#xff0c;所以每次要重新设置 //条目的值mHolder &#61; new ViewHolder();mHolder.tv_title &#61; (TextView) v.findViewById(R.id.tv_title);mHolder.tv_detail &#61; (TextView) v.findViewById(R.id.tv_detail);mHolder.tv_comment &#61; (TextView) v.findViewById(R.id.tv_comment);mHolder.siv &#61; (SmartImageView) v.findViewById(R.id.siv);//把viewHolder封装至view对象中&#xff0c;这样view被缓存时&#xff0c;viewHolder也就被缓存了
v.setTag(mHolder);}else{v &#61; convertView;//从view中取出保存的viewHolder&#xff0c;viewHolder中就有所有的组件对象&#xff0c;不需要再去findViewByIdmHolder &#61; (ViewHolder) v.getTag();}//给条目中的每个组件设置要显示的内容
mHolder.tv_title.setText(news.getTitle());mHolder.tv_detail.setText(news.getDetail());mHolder.tv_comment.setText(news.getComment() &#43; "条评论");mHolder.siv.setImageUrl(news.getImageUrl());return v;}//把条目需要使用到的所有组件封装在这个类中class ViewHolder{TextView tv_title;TextView tv_detail;TextView tv_comment;SmartImageView siv;}&#64;Overridepublic Object getItem(int position) {return newsList.get(position);}&#64;Overridepublic long getItemId(int position) {return position;}}private void getNewsInfo() {Thread t &#61; new Thread(){&#64;Overridepublic void run() {String path &#61; "http://169.254.244.136:8080/news.xml";try {URL url &#61; new URL(path);HttpURLConnection conn &#61; (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(8000);conn.setReadTimeout(8000);if(conn.getResponseCode() &#61;&#61; 200){//流里的信息是一个xml文件的文本信息&#xff0c;用xml解析器去解析&#xff0c;而不要作为文本去解析InputStream is &#61; 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 &#61; Xml.newPullParser();try {xp.setInput(is, "utf-8");//获取事件类型&#xff0c;通过事件类型判断出当前解析的是和什么节点int type &#61; xp.getEventType();News news &#61; null;while(type !&#61; XmlPullParser.END_DOCUMENT){switch (type) {case XmlPullParser.START_TAG:if("newslist".equals(xp.getName())){newsList &#61; new ArrayList();}else if("news".equals(xp.getName())){news &#61; new News();}else if("title".equals(xp.getName())){String title &#61; xp.nextText();news.setTitle(title);}else if("detail".equals(xp.getName())){String detail &#61; xp.nextText();news.setDetail(detail);}else if("comment".equals(xp.getName())){String comment &#61; xp.nextText();news.setComment(comment);}else if("image".equals(xp.getName())){String image &#61; xp.nextText();news.setImageUrl(image);}break;case XmlPullParser.END_TAG:if("news".equals(xp.getName())){newsList.add(news);}break;}//指针移动到下一个节点并返回事件类型type &#61; xp.next();}//发送消息&#xff0c;让主线程刷新listviewhandler.sendEmptyMessage(1);
// for (News n : newsList) {
// System.out.println(n.toString());
// }} catch (Exception e) {// TODO Auto-generated catch block
e.printStackTrace();}}
}