Android ListView and BaseAdapter applications, androidbaseadapter

Source: Internet
Author: User

Android ListView and BaseAdapter applications, androidbaseadapter

Steps:

1. Create a ListView object. -- (function: bind an Adapter to present data)

2. Create an Item column. xml layout for ListView; -- (Role: implement the column layout for ListView)

3. Create a View data instance (class) for Item. xml; -- (Role: implement the Item column data interface)

4. inherit BaseAdapter to create a MyAdapter class; -- (function: encapsulate ListView's. xml layout and Item data)

5. Of course, more operations are required. Here is a simple method;

Adapter implementation:

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; // title 73 private String price; // price 74 private String text; // content 75 private String status; // 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 has an instance:

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 (); // initial listview data} @ 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>

:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.