基于PHP后台的Android新闻浏览客户端(2)

package com.MR.news.adapter; import java.util.List; import com.MR.news.R; import com.MR.news.model.News; import com.MR.news.utils.HttpUtils; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class NewsAdapter extends BaseAdapter { //声明上下文对象,后面的getView方法需要 private Context context; private List<News> newsList; public NewsAdapter(Context context, List<News> newsList){ this.context = context ; this.newsList = newsList ; } @Override public int getCount() { // TODO Auto-generated method stub return newsList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return newsList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup arg2) { // TODO Auto-generated method stub if(convertView == null){ convertView = LayoutInflater.from(context).inflate(R.layout.news_item,null) ; } TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle) ; TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc) ; TextView tvTime = (TextView) convertView.findViewById(R.id.tvTime) ; ImageView ivPic = (ImageView) convertView.findViewById(R.id.ivPic); News news = newsList.get(position) ; tvTitle.setText(news.getTitle()) ; tvDesc.setText(news.getDesc()) ; tvTime.setText(news.getTime()) ; String pic_url = news.getPic_url() ; HttpUtils.setPicBitMap(ivPic, pic_url) ; return convertView; } }

news_item用来设置每个item的显示格式

<?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" > <ImageView android:id="@+id/ivPic" android:layout_width="42dp" android:layout_height="42dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/ivPic" android:text="title" android:textSize="18sp" /> <TextView android:id="@+id/tvDesc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvTitle" android:layout_below="@+id/tvTitle" android:text="desc" android:textSize="18sp" /> <TextView android:id="@+id/tvTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="time" android:textSize="10sp" /> </RelativeLayout>

注意:此item中需要显示单个图片,所以用到Bitmap这个类。由于用到网络传输,所以需要用到线程这个概念!!

关键理解handler message以及loop这三者的关系。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/752746eefa406a315e6ad5be2f6bccfa.html