Android control ListView Get 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:

Package Com.exmyth.android;public class Listeditoradapter extends Baseadapter {private Layoutinflater minflater; Private list<map<string, object>> mdata;//stored edittext value public map<string, string> editorvalue = new Hashmap<string, string> ();//Public Listeditoradapter (context context, list<map<string, object>>        Data) {mdata = data;        Minflater = Layoutinflater.from (context);    Init ();    }//Initialize private void init () {editorvalue.clear ();    } @Override public int getcount () {return mdata.size ();    } @Override public Object getItem (int position) {return null;    } @Override public long getitemid (int position) {return 0;    } private Integer index =-1;        @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder = null;        Initializes the Convertview when Convertview is null. if (Convertview = = null) {holdER = new Viewholder ();            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 (New Ontouchlistener () {@Override public boolean OnTouch (Vie W V, motionevent event) {if (event.getaction () = = motionevent.action_up) {Inde                    x = (Integer) v.gettag ();                } return false;            }            }); Class Mytextwatcher implements Textwatcher {public Mytextwatcher (ViewHolder Holder) {mholder = Holder;                } private Viewholder Mholder; @Override public void ontextchanged (charsequence s, int start, int before, int count                        ) {} @Override public void beforetextchanged (charsequence s, int start, int count, int after) {} @Override public void AFTERTEXTC Hanged (Editable s) {if (s! = null &&! ").                        Equals (S.tostring ())) {int position = (Integer) mHolder.value.getTag (); Mdata.get (position). Put ("List_item_inputvalue", S.tostring ()), or//////When the EditText data changes }} holder.value.addTextChangedListener (New Mytextwatcher (hold            ER));        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 ();    } return Convertview;        Public final class Viewholder {public TextView name;        Public TextView title; Public EditText value;//The input public TextView userkey;//used in the ListView to define the symbolic primary key, you can not 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 sub item layout
     edittext et = ( EditText) Layout.findviewbyid (r.id.et);//Get control from layout, according to its id
//EditText et = ( EditText) Layout.getchildat (1)//or depending on position, in this I assume textview in front, EditText in the rear
  &NBSP;&NBSP;&NBSP;
     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.