需求分析
解析服务端HTML数据 以ListView数据显示出来 目前图片显示还有问题 需要改进
上代码分析
MainActivity.java
public class MainActivity extends Activity {
private ListView subjectlist;
Map<String, SoftReference<Bitmap>> iconCache;
List<NewBook> newbooks;
NewBookAdapter adapter;
public TextView mTextViewTitle;
public RelativeLayout mRelativeLoading;
public ImageButton mImageBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.e("MainActivity", "onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
setupView();
setListener() ;
fillData();
}
public void showLoading() {
mRelativeLoading.setVisibility(View.VISIBLE);
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
aa.setDuration(1000);
ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
sa.setDuration(1000);
AnimationSet set = new AnimationSet(false);
set.addAnimation(sa);
set.addAnimation(aa);
mRelativeLoading.setAnimation(set);
mRelativeLoading.startAnimation(set);
}
public void hideLoading() {
AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
aa.setDuration(1000);
ScaleAnimation sa = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
sa.setDuration(1000);
AnimationSet set = new AnimationSet(false);
set.addAnimation(sa);
set.addAnimation(aa);
mRelativeLoading.setAnimation(set);
mRelativeLoading.startAnimation(set);
mRelativeLoading.setVisibility(View.INVISIBLE);
}
public void showToast(String text) {
Toast.makeText(this, text, 0).show();
}
public void setupView() {
Log.e("MainActivity", "setupView");
mRelativeLoading = (RelativeLayout) this.findViewById(R.id.loading);
subjectlist = (ListView) this.findViewById(R.id.subjectlist);
}
public void setListener() {
Log.e("MainActivity", "setListener");
}
public void fillData() {
Log.e("MainActivity", "fillData");
new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
newbooks = NetUtil.getNewBooks(getApplicationContext());
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showLoading();
}
@Override
protected void onPostExecute(Boolean result) {
hideLoading();
super.onPostExecute(result);
if(result){
//设置数据适配器
if(adapter==null){
adapter = new NewBookAdapter();
subjectlist.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}else{
showToast("数据获取 失败,请检查网络");
}
}
}.execute();
}
private class NewBookAdapter extends BaseAdapter {
public int getCount() {
Log.e("MainActivity", "getCount");
return newbooks.size();
}
public Object getItem(int position) {
Log.e("MainActivity", "getItem");
return newbooks.get(position);
}
public long getItemId(int position) {
Log.e("MainActivity", "getItemId");
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("MainActivity", "getView");
NewBook newbook = newbooks.get(position);
View view;
if(convertView==null){
view = View.inflate(MainActivity.this, R.layout.new_book_item, null);
}else{
view = convertView;
}
final ImageView iv = new ImageView(MainActivity.this);
LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_book_image);
//清空ll的里面的view对象
ll.removeAllViews();
ll.addView(iv, new LayoutParams(60, 60));
TextView tv_title = (TextView) view.findViewById(R.id.book_title);
TextView tv_description = (TextView) view
.findViewById(R.id.book_description);
tv_title.setText(newbook.getName());
tv_description.setText(newbook.getDescription());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean canloadicon = sp.getBoolean("canloadicon", false);
if(canloadicon){
LoadImageAsynTask task = new LoadImageAsynTask(new LoadImageAsynTaskCallback() {
public void beforeLoadImage() {
iv.setImageResource(R.drawable.book);
}
public void afterLoadImage(Bitmap bitmap) {
if(bitmap!=null){
iv.setImageBitmap(bitmap);
}else{
iv.setImageResource(R.drawable.book);
}
}
});
task.execute(newbook.getIconpath());
}else{
iv.setImageResource(R.drawable.book);
}
return view;
}
}
}
subject.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_books"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFF5F5F5"
android:orientation="vertical"
android:padding="0px" >
<RelativeLayout
android:id="@+id/mainRL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0" >
<ListView
android:id="@+id/subjectlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="3.0dip"
android:layout_weight="1.0"
android:cacheColorHint="#00000000"
android:divider="@android:color/transparent"
android:dividerHeight="5.0dip"
android:listSelector="@android:color/transparent"
android:paddingTop="5.0dip"
android:scrollbarStyle="outsideInset" >
</ListView>
<RelativeLayout
android:id="@+id/loading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="invisible" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AA111111"
android:gravity="center"
android:padding="8dip" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="24dip"
android:layout_height="24dip"
android:layout_marginBottom="6dip"
android:layout_marginTop="6dip"
android:indeterminate="true" />
<TextView
android:id="@+id/txt_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="8dip"
android:layout_toRightOf="@id/progress"
android:shadowColor="#000000"
android:shadowDx="0"
android:shadowDy=".5"
android:shadowRadius="1"
android:text="正在 获取数据"
android:textColor="#FFFFFF" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
new_book_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/category_selector"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/ll_book_image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="15dip" >
<TextView
android:id="@+id/book_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="50dip"
android:textColor="@android:color/black"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/book_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="60dip"
android:textColor="@android:color/black"
android:textSize="15dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
NewBook.java 数据类
public class NewBook {
private String name; //书名
private String description; //描述
private String summary; //简介
private String iconpath; //图片地址
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getIconpath() {
return iconpath;
}
public void setIconpath(String iconpath) {
this.iconpath = iconpath;
}
}
NetUtil.java 网络工具类
public class NetUtil {
public static Bitmap getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
return BitmapFactory.decodeStream(is);
}
public static List<NewBook> getNewBooks(Context context) throws Exception {
String path = context.getResources().getString(R.string.newbookpath);
URL url = new URL(path);
URLConnection conn = url.openConnection();
Source source = new Source(conn);
List<NewBook> newbooks = new ArrayList<NewBook>();
List<Element> lielements = source.getAllElements("li");
System.out.println(lielements.size());
for (Element lielement : lielements) {
List<Element> childrenlists = lielement.getChildElements();
if (childrenlists.size() == 2) {
if ("detail-frame".equals(childrenlists.get(0)
.getAttributeValue("class"))) {
NewBook newbook = new NewBook();
// 数目对应的div信息
Element div = childrenlists.get(0);
List<Element> divChildren = div.getChildElements();
String name = divChildren.get(0).getTextExtractor()
.toString();
newbook.setName(name);
String description = divChildren.get(1).getTextExtractor()
.toString();
newbook.setDescription(description);
String summary = divChildren.get(2).getTextExtractor()
.toString();
newbook.setSummary(summary);
Element achild = childrenlists.get(1);
String iconpath = achild.getChildElements().get(0)
.getAttributeValue("src");
newbook.setIconpath(iconpath);
newbooks.add(newbook);
}
}
}
return newbooks;
}
}
LoadImageAsynTask.java 异步加载图片工具类
public class LoadImageAsynTask extends AsyncTask<String, Void, Bitmap> {
LoadImageAsynTaskCallback loadImageAsynTaskCallback;
public LoadImageAsynTask(LoadImageAsynTaskCallback loadImageAsynTaskCallback) {
this.loadImageAsynTaskCallback = loadImageAsynTaskCallback;
}
public interface LoadImageAsynTaskCallback{
public void beforeLoadImage();
public void afterLoadImage(Bitmap bitmap);
}
/**
* 当异步任务执行之前调用
*/
@Override
protected void onPreExecute() {
//初始化的操作具体怎么去实现, LoadImageAsynTask 不知道
// 需要让调用这个 LoadImageAsynTask 的人 去实现
loadImageAsynTaskCallback.beforeLoadImage();
super.onPreExecute();
}
/**
* 异步任务执行之后调用
*/
@Override
protected void onPostExecute(Bitmap result) {
loadImageAsynTaskCallback.afterLoadImage(result);
super.onPostExecute(result);
}
/**
* 后台子线程运行的异步任务
* String... params 可变长度的参数
*/
@Override
protected Bitmap doInBackground(String... params) {
try {
String path = params[0];
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
return BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
AndroidManifest.xml 加权限 <application上面
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
导入外部的Jar包
代码还有问题 明天继续搞