java - How to add the items in listview Dynamically in android? -


i adding items in listview, it's working fine listview not expanding.

this code:

private arraylist<namedetails> namedetails = null; 

inside oncreate() code is

    namedetails = new arraylist<namedetails>();     case r.id.tvaddmore:                  final dialog dialog = new dialog(this);                 dialog.requestwindowfeature(window.feature_no_title);                 dialog.setcontentview(r.layout.location_name_details_dialog);                  final edittext etname = (edittext) dialog.findviewbyid(r.id.etname);                 final edittext etphone = (edittext) dialog.findviewbyid(r.id.etphone);                 button btnok = (button) dialog.findviewbyid(r.id.btnok);                 button btncancel = (button) dialog.findviewbyid(r.id.btncancel);                   btnok.setonclicklistener(new onclicklistener() {                      @override                     public void onclick(view v)                      {                         string name= etname.gettext().tostring();                         string phone= etphone.gettext().tostring();                           namedetails = new arraylist<namedetails>();                          namedetails namedetailss = new namedetails();                         namedetailss.setname(name);                         namedetailss.setphone(phone);                         namedetails.add(namedetailss);                          namedetailsadapter namedetailsadapter = new namedetailsadapter(namedetails);                         lvnamedetails.setadapter(namedetailsadapter);                         namedetailsadapter.notifydatasetchanged();                         dialog.cancel();                      }                 });                  btncancel.setonclicklistener(new onclicklistener() {                      @override                     public void onclick(view v)                      {                         dialog.cancel();                      }                 });                 dialog.show();                 break;              public class namedetailsadapter extends baseadapter     {          arraylist<namedetails> namedetails;         string name,phone;         int count  = 0;         context ctx;         activity act;         public namedetailsadapter(arraylist<namedetails> namedetails)         {              this.namedetails = namedetails;          }            @override         public int getcount() {             return namedetails.size();         }          @override         public object getitem(int position) {             return position;         }          @override         public long getitemid(int position) {             return position;         }          @override         public view getview(final int position, view convertview, viewgroup parent)          {               layoutinflater vi = (layoutinflater)myapp.context.getsystemservice(context.layout_inflater_service);              convertview = vi.inflate(r.layout.name_details, null);              textview tvname     =    (textview)convertview.findviewbyid(r.id.tvname);             textview tvphone    =    (textview)convertview.findviewbyid(r.id.tvphone);             textview tvnameone  =    (textview)convertview.findviewbyid(r.id.tvnameone);             textview tvphoneone =    (textview)convertview.findviewbyid(r.id.tvphoneone);               namedetails name = namedetails.get(position);             tvname.settext(name.getname());             tvphone.settext(name.getphone());              tvnameone.settypeface(appconstants.bold);             tvphoneone.settypeface(appconstants.bold);                return convertview;         }      } 

namedetails.xml

<?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" >      <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_marginleft="@dimen/margin15"         android:layout_marginright="@dimen/margin15"         android:layout_margintop="5dp"         android:gravity="center"         android:orientation="vertical" >          <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_margintop="10dp"             android:gravity="center"             android:orientation="horizontal" >              <textview                 android:id="@+id/tvnameone"                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_weight="0.25"                 android:text="@string/name"                 android:textsize="@dimen/text_size14" />              <textview                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_weight="0.1"                 android:text=":" />              <textview                 android:id="@+id/tvname"                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_marginleft="@dimen/margin10"                 android:layout_weight="0.65"                 android:background="@drawable/text_field"                 android:gravity="center_vertical"                 android:inputtype="textpersonname"                 android:maxems="40"                 android:paddingleft="@dimen/padding5"                 android:paddingright="@dimen/padding5"                 android:singleline="true"                 android:textcolor="@android:color/black"                 android:textsize="@dimen/text_size13" />         </linearlayout>          <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_margintop="5dp"             android:gravity="center"             android:orientation="horizontal" >              <textview                 android:id="@+id/tvphoneone"                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_weight="0.25"                 android:text="@string/phone_no"                 android:textsize="@dimen/text_size14" />              <textview                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_weight="0.1"                 android:text=":" />              <textview                 android:id="@+id/tvphone"                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_marginleft="@dimen/margin10"                 android:layout_weight="0.65"                 android:background="@drawable/text_field"                 android:gravity="center_vertical"                 android:inputtype="phone"                 android:maxems="40"                  android:textcolor="@android:color/black"                 android:paddingleft="@dimen/padding5"                 android:paddingright="@dimen/padding5"                 android:singleline="true"                 android:textsize="@dimen/text_size13" />         </linearlayout>     </linearlayout>  </linearlayout> 

listview in android not increases dynamically. dimesion same have provided in layout file(but can scroll it). if want dynamically increase listview make child of scrollview , make sure scrollview shoud go upto bottom of screen, , hieght of listview should match parent.

note- not standard practice do


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -