代码如下:
package com.easaa.adapter;
}
2.自定义适配器显示图片
package com.easaa.adapter;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.easaa.demo.R;
import com.easaa.vo.ProductItem;
/**
* 自定义一个Adapter用来显示数据
* @author 明邦良
*
*/
public class ProductAdapter extends BaseAdapter{
private Context mContext;//上下文
private List<ProductItem> mItems;//用于存放ProductItem对象
private LayoutInflater inflater;//布局提取器
private BufferedInputStream bufferis;//一个缓存
private AssetManager assetManager;//为了获取Asset文件下的资源
public ProductAdapter(Context context, List<ProductItem> mItems) {
this.mContext = context;
this.mItems = mItems;
/**
* 初始化布局提取器和AssetManager对象
*/
inflater=LayoutInflater.from(mContext);
assetManager=mContext.getAssets();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mItems.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/**
* 判断convertView是否为空,并提取布局
*
*/
if(convertView==null){
convertView=inflater.inflate(R.layout.lock_time_sale_item, null);
}
ImageView imageView=(ImageView) convertView.findViewById(R.timeSale.image);
MylockAdapter mylockAdapter=(MylockAdapter) convertView.findViewById(R.timeSale.remainTime);
try {
bufferis=new BufferedInputStream(assetManager.open(mItems.get(position).getImageUrl()));
imageView.setImageBitmap(BitmapFactory.decodeStream(bufferis));
} catch (IOException e) {
e.printStackTrace();
}
mylockAdapter.setEndTime(mItems.get(position).getRemainTime());
mylockAdapter.setClockListener(new MylockAdapter.ClockListener() {
@Override
public void timeEnd() {
// TODO Auto-generated method stub
}
@Override
public void remainFiveMinutes() {
// TODO Auto-generated method stub
}
} );
return convertView;
}
}
Activity活动:
package com.easaa.demo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import com.easaa.adapter.ProductAdapter;
import com.easaa.vo.ProductItem;
/**
*使用适配器 将数据显示在View中
* @author 明邦良
*
*/
public class MainActivity extends Activity {
private ListView listView;
private ProductAdapter productAdapter;
Random random=new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock_time_sale);
initData();
}
private void initData(){
listView=(ListView) findViewById(R.timeSale.listView);
List<ProductItem> items=new ArrayList<ProductItem>();
ProductItem item = null;
for(int i=0;i<10;i++){
item=new ProductItem();
item.setId(System.currentTimeMillis()+30*1000*24*24);
item.setImageUrl("images/"+getImages()[random.nextInt(7)]);
item.setRemainTime(getRandomTime());
items.add(item);
}
productAdapter=new ProductAdapter(this,items);
listView.setAdapter(productAdapter);
}
//随机取时间
private long getRandomTime(){
long curTime=System.currentTimeMillis();
long []t=new long[]{500,200,640,120,300,450,100,1000,1540,2500};//定义时间
return curTime+t[random.nextInt(9)]*1000;//在里面随机抽并*1000
}
//取图片
private String [] getImages(){
String images[]=null;
try {
images=this.getApplicationContext().getAssets().list("images");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return images;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
一个实体,用于装数据
package com.easaa.vo;
public class ProductItem {
/**
* 一个实体
* 用于装载图片及时间
*/
private long id;
private String imageUrl;
private long remainTime;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public long getRemainTime() {
return remainTime;
}
public void setRemainTime(long remainTime) {
this.remainTime = remainTime;
}
}
如图:
源码下载:http://download.csdn.net/detail/mxbhxx/5596357