android - Button press add new row to a ListView programmatically, how to? -



android - Button press add new row to a ListView programmatically, how to? -

my main layout main.xml has button, edittext , empty listview below:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" > <linearlayout android:id="@+id/input_area" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <edittext android:id="@+id/input_field" android:layout_height="40dip" android:layout_width="fill_parent" android:layout_weight="5" /> <button android:id="@+id/send_btn" android:layout_width="60dip" android:layout_height="30dip" android:text="@string/send" android:layout_weight="1" /> </linearlayout> <linearlayout android:id="@+id/output_area" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#006633" android:visibility="gone" > <listview android:id="@+id/output_list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingtop="10dip" > <!-- when "send" button in above input_area pressed, text edittext field show here programmatically new row of listview--> </listview> </linearlayout> </linearlayout>

as see above, there 2 kid linearlayout hosted main linearlayout.

the 1st kid linearlayout id input_area consists of edittext , button.

the 2nd kid linearlayout id output_area linearlayout empty listview, , visibility set "gone".

the feature going implement simple, that's in input_area, when user input text in edittext field, , press send button, input string should shown in output linearlayout a new row of listview programmatically.

what tried java code below:

edittext inputtxt = (edittext) findviewbyid(r.id.input_field); button sendbtn = (button) findviewbyid(r.id.send_btn); linearlayout outputarea = findviewbyid(r.id.output_area); //updated saurabh listview lv = findviewbyid(r.id.output_list); mylistadapter madapter = new mylistadapter(this, arraylist); //update finished sendbtn.setonclicklistener(new onclicklistener(){ @override public void onclick(view v){ int visibility = outputarea.getvisibility(); if(visibility==view.gone) // set listview visible outputarea.setvisibility(view.visible); //get user input string userinput = inputtxt.gettext().tostring(); // show string in new row of listview //but how dynamically add together new row listview here??? } });

but not sure how add together new row listview programmatically, can help me?

by way, have layout xml fiel (row.xml) defined each row's layout.

-----------------------update------------------------------------

each row of list contain icon , text string. list adapter , row layout showing below:

my adapter:

private static class mylistadapter extends baseadapter { private layoutinflater minflater; arraylist<string> arraylist; public mylistadapter(context context, arraylist<string> arraylist) { minflater = layoutinflater.from(context); this.arraylist = arraylist; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.row, null); holder = new viewholder(); holder.icon = (imageview) convertview.findviewbyid(r.id.icon); holder.userinput = (textview) convertview.findviewbyid(r.id.user_input); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.icon.setimage(???); holder.userinput.settext(arraylist.get(position)); homecoming convertview; } static class viewholder { imageview icon; textview userinput; } }

my list row layout:

<?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" > <imageview android: id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/user_input" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textsize="10dip"/> </linearlayout>

make global variable below

arraylist<string> arraylist = new arraylist<string>();

on click of send button update adapter setting on listview adding

string userinput = inputtxt.gettext().tostring(); arraylist.add(userinput)

in adapter , call

adapter.notifydatasetchanged();

update updated reply in question , in post re-create new adapter class , utilize

android android-layout android-emulator android-widget android-manifest

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -