Android control ListView gets item in EditText value

Source: Internet
Author: User

It can be clear that there is no direct way to get the value of EditText for each row in the ListView.

Solution: Rewrite Baseadapter and get the EditText value for each row entered in the ListView yourself.

Approximate algorithm: Rewrite the Baseadapter.getview function, use an array to store the values in the EditText, according to the position is the array subscript, Dynamically update the EditText in GetView and get the values in EditText dynamically. Because the item in the ListView is multiplexed, data is disturbed if the value is not dynamically emptied or dynamically fetched in the EditText. or no data. Then monitor the change of its value when generating edittext. Store it down.

Code:

 Packagecom.exmyth.android; Public classListeditoradapterextendsBaseadapter {PrivateLayoutinflater Minflater; PrivateList<map<string, object>> Mdata;//the stored edittext value     Publicmap<string, string> editorvalue =NewHashmap<string, string> ();//     PublicListeditoradapter (context context, list<map<string, object>>data) {Mdata=data; Minflater=Layoutinflater.from (context);    Init (); }    //Initialize    Private voidinit () {editorvalue.clear (); } @Override Public intGetCount () {returnmdata.size (); } @Override PublicObject GetItem (intposition) {        return NULL; } @Override Public LongGetitemid (intposition) {        return0; }    PrivateInteger index =-1; @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder Holder=NULL; //initializes the Convertview when Convertview is null.         if(Convertview = =NULL) {Holder=NewViewholder (); Convertview= Minflater.inflate (R.layout.listview_item,NULL); Holder.name=(TextView) convertview. Findviewbyid (R.id.list_item_name); Holder.title=(TextView) convertview. Findviewbyid (R.id.list_item_title); Holder.value=(EditText) convertview. Findviewbyid (R.id.list_item_inputvalue);            Holder.value.setTag (position); Holder.userkey=(TextView) Convertview.findviewbyid (R.id.user_key); Holder.value.setOnTouchListener (NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {if(event.getaction () = =motionevent.action_up) {Index=(Integer) V.gettag (); }                    return false;            }            }); classMytextwatcherImplementsTextwatcher { PublicMytextwatcher (Viewholder holder) {Mholder=Holder; }                PrivateViewholder Mholder; @Override Public voidOnTextChanged (Charsequence S,intStart,intBefore,intcount) {} @Override Public voidBeforetextchanged (Charsequence S,intStart,intCountintAfter ) {} @Override Public voidaftertextchanged (Editable s) {if(s! =NULL&& "". Equals (S.tostring ())) {                        intPosition =(Integer) MHolder.value.getTag (); Mdata.get (position). Put ("List_item_inputvalue", s.tostring ());//stored in the data variable when the EditText}}} holder.value.addTextChangedListener (Newmytextwatcher (holder));        Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag ();        Holder.value.setTag (position); } Object Value= Mdata.get (position). Get ("List_item_name"); if(Value! =NULL) {holder.name.setText (String) value); } Value= Mdata.get (position). Get ("List_item_title"); if(Value! =NULL) {Holder.title.setText (value.tostring ()); } Value= Mdata.get (position). Get ("User_key"); if(Value! =NULL) {Holder.userkey.setText (value.tostring ()); } Else{Holder.userkey.setText ("-1"); } Value= Mdata.get (position). Get ("List_item_inputvalue"); if(Value! =NULL&& "". Equals (value))        {Holder.value.setText (value.tostring ()); } Else{String key= Mdata.get (position). Get ("User_key"). toString (); String Inputvalue=Editorvalue.get (key);        Holder.value.setText (Inputvalue);        } holder.value.clearFocus (); if(Index! =-1 && index = =position)        {Holder.value.requestFocus (); }        returnConvertview; }     Public Final classViewholder { PublicTextView name;  PublicTextView title;  PublicEditText value;//input in the ListView         PublicTextView UserKey;//used to define the iconic primary key, you don't have to care    }}

How to use:

Private list<map<string, object>> mcheckitemlist = new arraylist<map<string, Object>> ();

Madapter = new Listeditoradapter (this, mcheckitemlist);

M_lvlistview.setadapter (Madapter);

Change the data in the Mcheckitemlist directly and then call Madapter.notifydatasetchanged () to update the data in the ListView

Of course, the data entered by the user in EditText can also be obtained directly from mcheckitemlist. More convenient

Other:

ListView list= (ListView) Findviewbyid (r.id.list);//Get ListView

for (int i = 0; i < List.getchildcount (); i++) {
LinearLayout layout = (linearlayout) list.getchildat (i);//Get layout of child item
EditText et = (EditText) Layout.findviewbyid (r.id.et);//Get control from layout, based on its ID
EditText et = (EditText) layout.getchildat (1)//or depending on position, in this I assume textview in front, EditText in the rear

System.out.println ("The text of" +i+ "s EditText:----------->" +et.gettext ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.