java - Android: Get Textview value of Spinner on Button click -


i have spinner , populating custom simplecursoradapter. spinner item layout contains 2 textviews, 1 textview item id , not visible other item name. want item id on button click event insert sqlite database. id on setonitemselectedlistener of spinner as

companyspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() {             @override             public void onitemselected(adapterview<?> parent, view view, int position, long id) {                  // selected row data show on screen                 string companyid = ((textview) view.findviewbyid(r.id.spinneritemidtv)).gettext().tostring();                 toast.maketext(getactivity(), companyid, toast.length_long).show();                 log.w(tag, "companyid:" + companyid);              }              @override             public void onnothingselected(adapterview<?> parent) {               }         }); 

and spinner item layout

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="horizontal" >      <textview         android:id="@+id/spinneritemidtv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textcolor="#888"         android:textsize="20sp"         android:visibility="gone"/>      <textview         android:id="@+id/spinneritemnametv"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textcolor="#888"         android:textsize="20sp" />  </linearlayout> 

but couldnt make on button click. appreciated.

i think searching

view selectedview = null;  //declare class level variable don't need make final    spinner2.setonitemselectedlistener(new adapterview.onitemselectedlistener() {         @override         public void onitemselected(adapterview<?> parent, view view, int position, long id) {             selectedview = view;         }         @override         public void onnothingselected(adapterview<?> parent) {          }     }); 

and inside button's click event, this

btn.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                     if(selectedview!=null){                                             string companyid = ((textview) selectedview.findviewbyid(r.id.spinneritemidtv)).gettext().tostring();                 }                else{//something}                  }         }); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -