android - Custom ListView in Fragment not working -
i'm newbie in android , trying display custom listview in fragment when run display nothing.can me fix that. here code:
public class tab1 extends fragment implements listview.onitemclicklistener{ private arraylist<custom> fetch = new arraylist<custom>(); private contactsadapter adapter; private listview lv; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle saveinstancestate){ view v = inflater.inflate(r.layout.tab_layout, container, false); custom 1 = new custom("hoang ha", "01672286349"); custom 2 = new custom("ha link", "03203590176"); fetch.add(one); fetch.add(two); lv =(listview)v.findviewbyid(r.id.list); adapter = new contactsadapter(getactivity(), fetch); lv.setadapter(adapter); lv.setonitemclicklistener(this); return v; } public class custom{ private string contactname; private string contactphone; public custom(string st1, string st2){ contactname = st1; contactphone = st2; } public string getname(){ return contactname; } public string getphone(){ return contactphone; } public void setname(string st1){ contactname = st1; } public void setphone(string st2){ contactphone = st2; } } @override public void onitemclick(adapterview<?> ad, view v, int position, long id) { // todo auto-generated method stub } private class contactsadapter extends baseadapter{ private fragmentactivity activity; private layoutinflater inflater; private arraylist<custom> data; public contactsadapter (fragmentactivity a, arraylist<custom> d){ activity = a; data = d; inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub data.size(); return 0; } @override public object getitem(int position) { // todo auto-generated method stub return 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 v = convertview; v = inflater.inflate(r.layout.list_row, null); textview contact_name = (textview)v.findviewbyid(r.id.contact_name); textview phone_number = (textview)v.findviewbyid(r.id.phone_number); //final custom custom = entries.get(position); final custom custom = data.get(position); contact_name.settext(custom.getname()); phone_number.settext(custom.getphone()); return v; } } }
here tab xml code:
<?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:background="@color/white" android:id="@+id/tab_layout" > <listview android:id="@+id/list" android:layout_height="match_parent" android:layout_width="match_parent" android:dividerheight="1dp" android:divider="@color/white" /> </linearlayout>
and here list item xml code :
<?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="wrap_content" android:orientation="vertical" android:background="@color/light_dark" android:padding="5dip" > <textview android:id="@+id/contact_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textcolor="@color/white" android:typeface="sans" android:textsize="@dimen/font_medium" android:textstyle="bold"/> <textview android:id="@+id/phone_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/name" android:textcolor="@color/white" android:typeface="sans" android:textsize="@dimen/font_small" /> </linearlayout>
@override public int getcount() { return data.size(); } @override public custom getitem(int position) { return data.get(position); }
Comments
Post a Comment