java - Android custom Row Item for ListView -


i have listview should have following layout in rows:

header text 

header should static text changes every few seconds.

i implemented populating string[] array, pass arrayadapter , set every time data changes:

data_array = populatestring(); adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, android.r.id.text1,  data_array); listview.setadapter(adapter); 

my problem not know how display data in format above.

thanks in advance.

add row.xml layout folder

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical" >  <textview android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="header"/>  <textview      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:id="@+id/text"/>   </linearlayout> 

make main xml layout this

<?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="horizontal" >      <listview         android:id="@+id/listview"         android:layout_width="fill_parent"         android:layout_height="fill_parent" >     </listview>  </linearlayout> 

this adapter

class youradapter extends baseadapter {      context context;     string[] data;     private static layoutinflater inflater = null;      public youradapter(context context, string[] data) {         // todo auto-generated constructor stub         this.context = context;         this.data = data;         inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);     }      @override     public int getcount() {         // todo auto-generated method stub         return data.length;     }      @override     public object getitem(int position) {         // todo auto-generated method stub         return data[position];     }      @override     public long getitemid(int position) {         // todo auto-generated method stub         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {         // todo auto-generated method stub         view vi = convertview;         if (vi == null)             vi = inflater.inflate(r.layout.row, null);         textview text = (textview) vi.findviewbyid(r.id.text);         text.settext(data[position]);         return vi;     } } 

your java activity

public class stackactivity extends activity {      listview listview;      /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);         listview = (listview) findviewbyid(r.id.listview);         listview.setadapter(new youradapter(this, new string[] { "data1",                 "data2" }));     } } 

results

enter image description here


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -