Android控件ListView获取item中EditText值
Posted 台风中的橘子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android控件ListView获取item中EditText值相关的知识,希望对你有一定的参考价值。
可以明确,现在没有直接方法可以获得ListView中每一行EditText的值.
解决方案:重写BaseAdapter,然后自行获取ListView中每行输入的EditText值.
大概算法:重写BaseAdapter.getView函数,用一个数组存储EditText中的值,根据position即数组下标,在getView中动态更新EditText和动态获取EditText中的值.因为ListView中的item是复用的,如果不动态清空或动态获取EditText中值,就会出现数据紊乱,或者没数据.那么在生成EditText的时候要监控其值的变化.存储下来.
代码:
[java] view plain copy- package com.exmyth.android;
- public class ListEditorAdapter extends BaseAdapter
- private LayoutInflater mInflater;
- private List<Map<String, Object>> mData;// 存储的EditText值
- 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();
- // 初始化
- 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))
- Android控件ListView获取item中EditText值
Android中listView中的button控件获取item的值
android ListView显示多个类型item 和 item中控件抢夺焦点解决办法
android ListView显示多个类型item 和 item中控件抢夺焦点解决办法
我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法