android - How to get text from EditText within ListView with multiple Views? -


i have listview contains 2 textviews , 1 edittext in each row. have searched , searched , cannot figure out how text user enters edittext. recent attempt implements textwatcher.

public void addingredienttomeal(string ingredientname) {      ingredient_list = (listview) findviewbyid(r.id.ingredients_in_meal_listview);      int numberofingredients = ingredient_list.getchildcount();      listadapter adapter = new listadapter();     adapter.name_array = new string[numberofingredients+1];      (int = 0; < numberofingredients; i++)         adapter.name_array[i] = (string) ingredient_list.getitematposition(i);      adapter.name_array[numberofingredients] = ingredientname;     ingredient_list.setitemscanfocus(true);     ingredient_list.setadapter(adapter); }  private class listadapter extends baseadapter{      public string[] name_array, qty_array;      @override     public int getcount() {         // todo auto-generated method stub         if(name_array != null && name_array.length != 0){             return name_array.length;             }         return 0;     }      @override     public object getitem(int position) {         // todo auto-generated method stub         return name_array[position];     }      @override     public long getitemid(int position) {         // todo auto-generated method stub         return position;     }      @override     public view getview(int position, view convertview, viewgroup parent) {             //viewholder holder = null;              final viewholder holder;                 if (convertview == null) {                      holder = new viewholder();                     layoutinflater inflater = newmeal.this.getlayoutinflater();                     convertview = inflater.inflate(r.layout.ingredients_in_meal_layout, null);                     holder.textview1 = (textview) convertview.findviewbyid(r.id.ingredient_name_in_new_meal_textview);                     holder.edittext1 = (edittext) convertview.findviewbyid(r.id.ingredient_qty_in_new_meal_edittext);                     holder.textview2 = (textview) convertview.findviewbyid(r.id.ingredient_unit_in_new_meal_textview);                      convertview.settag(holder);                  } else {                      holder = (viewholder) convertview.gettag();                 }                  holder.ref = position;                 holder.textview1.settext(name_array[position]);                 holder.textview2.settext(databaseadapter.dbquery(name_array[position]));                 holder.edittext1.settext(qty_array[position]);                 holder.edittext1.addtextchangedlistener(new textwatcher() {                          @override                         public void ontextchanged(charsequence arg0, int arg1, int arg2, int arg3) {                             // todo auto-generated method stub                             }                          @override                         public void beforetextchanged(charsequence arg0, int arg1, int arg2,                                     int arg3) {                                 // todo auto-generated method stub                          }                          @override                         public void aftertextchanged(editable arg0) {                                 qty_array[holder.ref] = arg0.tostring();                          }                 });             return convertview;     }      private class viewholder {         textview textview1, textview2;         edittext edittext1;         int ref;     }   } } 

for may have similar issue, solved problem having.

the problem: need values entered in multiple edittexts contained within listview. using listviewname.getitematposition(integer) gets value of first edittext. moreover, had multiple views contained in each row of listview, getitematposition returned value of first view in first row of listview.

the solution:

  • define listview object identifies listview care about
  • define view object can listview
  • define whatever field(s) trying listview (edittexts, textviews etc.)
  • get number of rows listview contains
  • set view object using listviewname.getchildat(position)
  • assign field (say, edittext) using (edittext) viewobject.findviewbyid(r.id.edittextid)
  • get value field using edittextobject.gettext().tostring()

here's sample code implemented on button press:

listview lvname = (listview) findviewbyid(r.id.listview1); view v; edittext et;  int listlength = lv.getchildcount(); string[] valueofedittext = new string(listlength); (int = 0; < listlength; i++) {     v = lvname.getchildat(i);     et = (edittext) v.findviewbyid(r.id.edittext1);     valueofedittext[i] = et.gettext().tostring(); } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -