android - How to retrieve widgets from Single Layout ArrayAdapters -


so have listview full of rows, each textview , spinner. use arrayadapter fill in each row. there single layout rows.

i want have button underneath rows can press saves selections of spinners in text file.

my problem though dont know how reference spinners anymore since use single layout approach. no longer have unique ids: (spinner1, spinner2, spinner3, etc.) each 1 because filled in row row.

any advice on how approach problem can reference each spinner , call "get text" method?

the more detail better, in advance!

this how it: if model object type of array doesn't have variable current spinner selection, need update model or extend adapter can capture current selection of spinner item.

in adapter's getview(), when create spinner, add onitemselectedlistener updates model corresponding item in array.

then when save button pressed, loop through items in adapter's array , current spinner values.

here code example:


mainactivity.java

package com.example.spinnerdemo;  import java.util.arraylist; import java.util.list;  import android.app.activity; import android.content.context; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemselectedlistener; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; import android.widget.spinner; import android.widget.textview;  public class mainactivity extends activity {      private static final string tag = "mainactivity";      private static final string[] mvalues = {             "strongly agree",             "agree",             "neutral",             "disagree",             "strongly disagree"     };      public static class mymodel {         public string text;         public int spinnerval;         public mymodel(string txt, int val) {             text = txt; spinnerval = val;         }     }      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          final list<mymodel> items = createitemlist();           listview listview = (listview) findviewbyid(r.id.listview1);         final mylistadapter adapter = new mylistadapter(this, items);         listview.setadapter(adapter);          button button = (button) findviewbyid(r.id.button1);         button.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // here spinner values                 log.d(tag, "------- save button clicked");                 (mymodel model : items) {                     log.d(tag, model.text + " : " + mvalues[model.spinnerval]);                 }                 log.d(tag, "-------");             }         });     }      public static class mylistadapter extends arrayadapter<mymodel> {          public mylistadapter(context context, list<mymodel> objects) {             super(context, 0, objects);         }          @override         public view getview(int position, view convertview, viewgroup parent) {              if (convertview == null) {                 layoutinflater inflater = (layoutinflater) parent.getcontext().getsystemservice(layout_inflater_service);                 convertview = inflater.inflate(r.layout.list_item, parent, false);             }              final mymodel model = (mymodel) getitem(position);              textview textview = (textview) convertview.findviewbyid(r.id.textview1);             textview.settext(model.text);              spinner spinner = (spinner) convertview.findviewbyid(r.id.spinner1);             spinner.setadapter(new arrayadapter<string>(parent.getcontext(),                     android.r.layout.simple_spinner_dropdown_item,                     android.r.id.text1, mvalues));             // here spinner gets value model             spinner.setselection(model.spinnerval);             spinner.setonitemselectedlistener(new onitemselectedlistener() {                  @override                 public void onitemselected(adapterview<?> parent, view view,                         int position, long id) {                     // here update model current position of spinner                     model.spinnerval = position;                 }                  @override                 public void onnothingselected(adapterview<?> parent) {                 }             });              return convertview;         }      }      private list<mymodel> createitemlist() {         list<mymodel> items = new arraylist<mymodel>();         // initialize "neutral" spinner setting         items.add(new mymodel("question 1", 2));         items.add(new mymodel("question 2", 2));         items.add(new mymodel("question 3", 2));         items.add(new mymodel("question 4", 2));         items.add(new mymodel("question 5", 2));         return items;     }  } 

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context="com.example.spinnerdemo.mainactivity" >      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentbottom="true"         android:layout_centerhorizontal="true"         android:text="save" />      <listview         android:id="@+id/listview1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_above="@+id/button1"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true" >      </listview>  </relativelayout> 

list_item.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:gravity="center_vertical"     android:orientation="horizontal" >      <textview         android:id="@+id/textview1"         android:textappearance="?android:attr/textappearancelarge"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="textview" />      <spinner         android:id="@+id/spinner1"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1" />  </linearlayout> 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -