android 重写BaseAdapter类

1.自定义Adapter
主要继承的方法是getCount()和getView()
public class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}
		
		@SuppressWarnings("all")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				// 可以理解为从vlist获取view 之后把view返回给ListView
				convertView = mInflater.inflate(R.layout.listitem, null);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.info = (TextView) convertView.findViewById(R.id.info);
				holder.viewBtn = (Button) convertView
						.findViewById(R.id.view_btn);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.title.setText((String) mData.get(position).get("title"));
			holder.info.setText((String) mData.get(position).get("info"));
			holder.viewBtn.setTag(position);
			// 给Button添加单击事件 添加Button之后ListView将失去焦点 需要的直接把Button的焦点去掉
			holder.viewBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showInfo(position);
				}
			});
			// holder.viewBtn.setOnClickListener(MyListener(position));
			return convertView;
		}
		
	}

// ListItem中的实体类
	public final class ViewHolder {
		public TextView title;
		public TextView info;
		public Button viewBtn;
	}
// 弹框的写法
public void showInfo(int position) {

		ImageView img = new ImageView(ListViewActivity.this);
		img.setImageResource(R.drawable.b);
		new AlertDialog.Builder(this)
				.setView(img)
				.setTitle("详情" + position)
				.setMessage("菜名:" + title[position] + "   价格:" + info[position])
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				}).show();
	}


2.ListItem的布局文件
以后写item布局文件时都可以使用这个
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70.0dip"
    android:background="@drawable/item"
    android:drawingCacheQuality="high"
    android:minHeight="70.0dip"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/sItemIcon"
        android:layout_width="80dip"
        android:layout_height="60dip"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="5dip"
        android:background="@drawable/bg_img_coner"
        android:padding="1dip"
        android:scaleType="fitXY"
        android:src="@drawable/b" 
        android:contentDescription="@string/app_name"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/sItemIcon"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dip"
        android:layout_toRightOf="@+id/sItemIcon"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="@color/listTitle"
        android:textSize="20.0sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginLeft="20dip"
        android:layout_marginTop="20dip"
        android:layout_toRightOf="@id/sItemIcon"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textColor="#ff4400"
        android:textSize="12.0sp" />

    <Button
        android:id="@+id/view_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dip"
        android:background="@drawable/btn_detail_selecter"
        android:text="@string/detail" />
</RelativeLayout>

3.selector内容
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_detail_normal" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_detail_pressed" android:state_enabled="true" android:state_focused="true"/>

</selector>

你可能感兴趣的:(android 重写BaseAdapter类)