步聚:
1.建立ListView对象;--(作用:绑定Adapter呈现数据)
2.建立ListView实现的Item栏位.xml布局;--(作用:实现ListView的栏位布局)
3.建立Item.xml 的View数据实例(类);--(作用:实现Item栏位数据接口)
4.继承BaseAdapter建立MyAdapter类class;--(作用:封装ListView的.xml布局与Item数据)
5.当然还要作更多的操作,这里例出简洁的方法;
adapter实现:
private class UpdateAdpter extends BaseAdapter { private LayoutInflater mInflater;
private List<ItemData> mItemsData; public UpdateAdpter(Context context, List<ItemData> listItemData) {
this.mInflater = LayoutInflater.from(context);
this.mItemsData = listItemData; } @Override
public int getCount() {
// TODO Auto-generated method stub
return mItemsData.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mItemsData.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.activity_account_update_listview_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder); } else {
holder = (ViewHolder) convertView.getTag();
}
ItemData item = mItemsData.get(position);
holder.title.setText(item.getTitle());
holder.price.setText(item.getPrice());
holder.text.setText(item.getBuyText());
holder.status.setText(item.getStatus()); return convertView;
} public class ViewHolder {
public TextView title;
public TextView price;
public TextView buytext;
public TextView status; ViewHolder(View view) {
title = (TextView) view
.findViewById(R.id.tv_accountupdateitem_title);
price = (TextView) view
.findViewById(R.id.tv_accountupdateitem_price);
text = (TextView) view
.findViewById(R.id.tv_accountupdateitem_buytext);
status = (TextView) view.findViewById(R.id.tv_accountupdate_status);
}
}
} private class ItemData {
private String title;//标题
private String price;//价格
private String text;//内容
private String status;//状态 public void setTitle(String value) {
title = value;
} public String getTitle() {
return title;
} public void setPrice(String value) {
price = value;
} public String getPrice() {
return price;
} public void setText(String value) {
buytext = value;
} public String getText() {
return buytext;
} public void setStatus(String value) {
status = value;
} public String getStatus() {
return status;
}
}
activity调有实例:
public class UpdateActivity extends Activity implements OnClickListener{
private ListView UpdateLView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_update);
iniView();
} public void iniView() {
UpdateLView = (ListView) findViewById(R.id.account_update_listView);
iniListViewDataUpdate();//初始listview数据
} @Override
public void onClick(View v) {
// TODO Auto-generated method stub
} private void iniListViewDataUpdate(){
ItemData itemData;
List<ItemData> listItemData = new ArrayList<ItemData>(); for (int i=0;i<2;i++){
itemData= new ItemData();
itemData.setTitle("title");
itemData.setPrice("price");
itemData.setText("text");
itemData.setStatus("status"); listItemData.add(itemData);
}
nowUpdateLView.setAdapter(new UpdateAdpter(this,listItemData));
}
item.xml;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical" > <LinearLayout
android:id="@+id/lyt_roomlistview_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@android:color/white" > <TextView
android:id="@+id/tv_accountupdateitem_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="title"
android:textSize="18sp" /> <TextView
android:id="@+id/tv_accountupdate_status"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="12dp"
android:layout_weight="1"
android:gravity="left|center"
android:text="button"
android:textColor="@android:color/holo_orange_dark"
android:textSize="16sp" /> <TextView
android:id="@+id/tv_accountupdateitem_price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="price"
android:textColor="@android:color/holo_orange_dark"
android:textSize="16sp" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@android:color/white" > <TextView
android:id="@+id/tv_accountupdateitem_buytext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Textview"
android:textSize="16sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@android:color/holo_orange_dark"
android:gravity="center"
android:text="@string/title_buy"
android:textColor="@android:color/white"
android:textSize="16sp" /> </LinearLayout> </LinearLayout> </RelativeLayout>
效果图: