Android control ListView obtains the EditText value in item, androidedittext

Source: Internet
Author: User

Android control ListView obtains the EditText value in item, androidedittext

It is clear that there is no direct way to obtain the value of EditText in each row of ListView.

Solution: rewrite the BaseAdapter and obtain the EditText value entered in each row of the ListView.

Approximate Algorithm: rewrite BaseAdapter. the getView function uses an array to store the values in EditText. According to position, that is, the array subscript, The EditText is dynamically updated in getView and the value in EditText is dynamically obtained. because items in ListView are reused. If the value of EditText is not cleared dynamically or the value of EditText is obtained dynamically, data disorder or no data occurs. monitor the value changes when generating EditText. stored.

Code:

Package com. exmyth. android; public class ListEditorAdapter extends BaseAdapter {private LayoutInflater mInflater; private List <Map <String, Object> mData; // The EditText value stored in 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; // initialize 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 = (TextVi Ew) convertView. findViewById (R. id. user_key); holder. value. setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_UP) {index = (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 afterTextChanged (Editable s) {if (s! = Null &&! "". Equals (s. toString () {int position = (Integer) mHolder. value. getTag (); mData. get (position ). put ("list_item_inputvalue", s. toString (); // save it to the data variable when the EditText data changes.} holder. value. addTextChangedListener (new MyTextWatcher (holder); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag (); holder. value. setTag (position);} Object value = mData. get (position ). get ("list_ite M_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; // enter public TextView userkey in ListView; // The iconic primary key to be defined. Do not worry about it }}

Usage:

Private List <Map <String, Object> mCheckItemList = new ArrayList <Map <String, Object> ();

MAdapter = new ListEditorAdapter (this, mCheckItemList );

M_lvListView.setAdapter (mAdapter );

Directly change the data in mCheckItemList, and then call mAdapter. yydatasetchanged (); To update the ListView data.

Of course, you can also obtain the data entered by the user in EditText directly from the mCheckItemList. This is more convenient.

 

 

 

Others:

ListView list = (ListView) findViewById (R. id. list); // obtain the listview

For (int I = 0; I <list. getChildCount (); I ++ ){
LinearLayout layout = (LinearLayout) list. getChildAt (I); // obtain the layout of the sub-item
EditText et = (EditText) layout. findViewById (R. id. et); // obtain the control from layout based on its id
// EditText et = (EditText) layout. getChildAt (1) // or based on the location, here I assume that TextView is in front and EditText is in the back

System. out. println ("the text of" + I + "'s EditText: ----------->" + et. getText ());
}



How does an android project obtain the ITEM value in Listview?

Your 5, 12 are in the same item, right?

If yes, you can rewrite the listadapter and write return a * B (a, B is 5, 12, or 19 26 in the getitem () function, in short, the value you want to display on this item ).
Then, for loop calls the getitem () of listadapter, obtains the multiplication result, and then adds the result.
Hope to adopt it.

Android gets the item value in listview

TextView tvLoc = (TextView) arg1.findViewById (android. R. id. text1 );

This should be the case. It mainly looks at the controls of text1 and text2 and changes the TextView accordingly.

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.