Android采用ListView实现列表数据的显示--Adapter应用

显示效果:

界面布局:

photo_list.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 需要拍摄照片的列表页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
	  android:gravity="center_horizontal">
        
        <TextView
		android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center_horizontal"
        	android:layout_weight="1"
       	android:text="号 码" />
        
        <TextView        
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:gravity="center_horizontal"
        	android:layout_weight="1"
        	android:text="名 称" />
        
        <TextView        
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
       	android:gravity="center_horizontal"
        	android:layout_weight="1"
      	android:text="图 片" />
         
        <TextView        
        	android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
       	android:gravity="center_horizontal"
       	android:layout_weight="1"
       	android:text="拍 照" />
    </LinearLayout>
    
    <ListView 
    	android:id="@+id/lvPhotoList"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"/>

    <Button 
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="完 成"/>
</LinearLayout>

photo_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 照片列表数据项item布局 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:gravity="center">
    
    <TextView
        android:id="@+id/tvIndex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="1" />
    
     <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="车前45度" />
    
     <ImageView
         android:id="@+id/ivPhoto"
         android:layout_width="100dp"
         android:layout_height="86dp"
         android:background="@drawable/nopic"
         android:gravity="center" />

     <Button 
         android:id="@+id/btnTakePhoto"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="拍 照" />
</LinearLayout>

PhotoListAdapter.java

public class PhotoListAdapter extends ArrayListAdapter<PhotoNoList> {  
    private int carId;  //车辆id
    public PhotoListAdapter(Context context, int carId) {
		super(context);
		this.carId = carId;
	}

    private static final int mLayout = R.layout.photolist_item;  
    
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder viewHolder;
    	final PhotoNoList photoNoList = mList.get(position);
		if(convertView == null){			
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate( mLayout, null);
			viewHolder.tvIndex = (TextView)convertView.findViewById(R.id.tvIndex);
			viewHolder.tvName = (TextView)convertView.findViewById(R.id.tvName);
			viewHolder.ivPhoto = (ImageView)convertView.findViewById(R.id.ivPhoto);
			viewHolder.btnTakePhoto = (Button)convertView.findViewById(R.id.btnTakePhoto);
			convertView.setTag(viewHolder);
		}else{			
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.tvIndex.setText(photoNoList.getPhotoKindCode() + "");
		viewHolder.tvName.setText(photoNoList.getPhotoKind());
		
		viewHolder.ivPhoto.setBackgroundResource(R.drawable.nopic);
		viewHolder.btnTakePhoto.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("carId", carId);
				intent.putExtra("photoKindCode", photoNoList.getPhotoKindCode());
				intent.setClass(mContext, TakePhotoActivity.class);
				mContext.startActivity(intent);
			}
		});

        return convertView;  
    }

    class ViewHolder {
    	TextView tvIndex,tvName;
    	ImageView ivPhoto;
    	Button btnTakePhoto;
    }
}

PhotoListActivity.java

public class PhotoListActivity extends Activity {
	ListView lvPhotoList;
	DataService dataService;
	PhotoListAdapter adapter;
	String[] newArrays;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo_list);
		int carId = getIntent().getIntExtra("carId", 0);  
		String categoryCode = getIntent().getStringExtra("categoryCode");
		lvPhotoList = (ListView)findViewById(R.id.lvPhotoList);
		dataService = new DataService(this);
		adapter = new PhotoListAdapter(this, carId);
				
		List<PhotoNoList> photoNoList = dataService.queryRequiredPhoto(categoryCode);
		Log.d("lb", photoNoList.size() + "");
		adapter.setList(photoNoList);
		lvPhotoList.setAdapter(adapter);

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值