android ListView 和 BaseAdapter 应用
步聚:
1.建立ListView对象;--(作用:绑定Adapter呈现数据)
2.建立ListView实现的Item栏位.xml布局;--(作用:实现ListView的栏位布局)
3.建立Item.xml 的View数据实例(类);--(作用:实现Item栏位数据接口)
4.继承BaseAdapter建立MyAdapter类class;--(作用:封装ListView的.xml布局与Item数据)
5.当然还要作更多的操作,这里例出简洁的方法;
adapter实现:
1 private class UpdateAdpter extends BaseAdapter { 2 3 private LayoutInflater mInflater; 4 private List<ItemData> mItemsData; 5 6 public UpdateAdpter(Context context, List<ItemData> listItemData) { 7 this.mInflater = LayoutInflater.from(context); 8 this.mItemsData = listItemData; 9 10 } 11 12 @Override 13 public int getCount() { 14 // TODO Auto-generated method stub 15 return mItemsData.size(); 16 } 17 18 @Override 19 public Object getItem(int position) { 20 // TODO Auto-generated method stub 21 return mItemsData.get(position); 22 } 23 24 @Override 25 public long getItemId(int position) { 26 // TODO Auto-generated method stub 27 return position; 28 } 29 30 @Override 31 public View getView(final int position, View convertView, 32 ViewGroup parent) { 33 // TODO Auto-generated method stub 34 ViewHolder holder; 35 if (convertView == null) { 36 convertView = mInflater.inflate( 37 R.layout.activity_account_update_listview_item, null); 38 holder = new ViewHolder(convertView); 39 convertView.setTag(holder); 40 41 } else { 42 holder = (ViewHolder) convertView.getTag(); 43 } 44 ItemData item = mItemsData.get(position); 45 holder.title.setText(item.getTitle()); 46 holder.price.setText(item.getPrice()); 47 holder.text.setText(item.getBuyText()); 48 holder.status.setText(item.getStatus()); 49 50 return convertView; 51 } 52 53 public class ViewHolder { 54 public TextView title; 55 public TextView price; 56 public TextView buytext; 57 public TextView status; 58 59 ViewHolder(View view) { 60 title = (TextView) view 61 .findViewById(R.id.tv_accountupdateitem_title); 62 price = (TextView) view 63 .findViewById(R.id.tv_accountupdateitem_price); 64 text = (TextView) view 65 .findViewById(R.id.tv_accountupdateitem_buytext); 66 status = (TextView) view.findViewById(R.id.tv_accountupdate_status); 67 } 68 } 69 } 70 71 private class ItemData { 72 private String title;//标题 73 private String price;//价格 74 private String text;//内容 75 private String status;//状态 76 77 public void setTitle(String value) { 78 title = value; 79 } 80 81 public String getTitle() { 82 return title; 83 } 84 85 public void setPrice(String value) { 86 price = value; 87 } 88 89 public String getPrice() { 90 return price; 91 } 92 93 public void setText(String value) { 94 buytext = value; 95 } 96 97 public String getText() { 98 return buytext; 99 } 100 101 public void setStatus(String value) { 102 status = value; 103 } 104 105 public String getStatus() { 106 return status; 107 } 108 }
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>
效果图:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。