Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android控件ListView獲取item中EditText值

Android控件ListView獲取item中EditText值

編輯:關於Android編程

可以明確,現在沒有直接方法可以獲得ListView中每一行EditText的值.

解決方案:重寫BaseAdapter,然後自行獲取ListView中每行輸入的EditText值.

大概算法:重寫BaseAdapter.getView函數,用一個數組存儲EditText中的值,根據position即數組下標,在getView中動態更新EditText和動態獲取EditText中的值.因為ListView中的item是復用的,如果不動態清空或動態獲取EditText中值,就會出現數據紊亂,或者沒數據.那麼在生成EditText的時候要監控其值的變化.存儲下來.

代碼:

package com.exmyth.android;

public class ListEditorAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List> mData;// 存儲的EditText值
    public Map editorValue = new HashMap();//

    public ListEditorAdapter(Context context, List> data) {
        mData = data;
        mInflater = LayoutInflater.from(context);
        init();
    }

    // 初始化
    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;
        // convertView為null的時候初始化convertView。
        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(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());// 當EditText數據發生改變的時候存到data變量中
                    }
                }
            }
            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_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;// ListView中的輸入
        public TextView userkey;// 用來定義的標志性主鍵,可不用關心
    }
}

使用方法:

private List> mCheckItemList = new ArrayList>();

mAdapter = new ListEditorAdapter(this, mCheckItemList);

m_lvListView.setAdapter(mAdapter);

直接改變mCheckItemList中的數據,然後調用mAdapter.notifyDataSetChanged();就可以更新ListView的數據

當然,直接從mCheckItemList也可以獲得用戶在EditText中輸入的數據.比較方便

其他:

ListView list= (ListView)findViewById(R.id.list);//獲得listview

for (int i = 0; i < list.getChildCount(); i++) {
LinearLayout layout = (LinearLayout)list.getChildAt(i);// 獲得子item的layout
EditText et = (EditText) layout.findViewById(R.id.et);// 從layout中獲得控件,根據其id
// EditText et = (EditText) layout.getChildAt(1)//或者根據位置,在這我假設TextView在前,EditText在後

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


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved