android开发中怎么获取listview中多个edittext的值
如题,请看图片红色框是listview里动态添加的tiem本次消费次数和本次车牌是需要用户输入的,在我点击确定消费的时候需要把红色框里的值封装成一个对象然后拿出来请问要怎...
如题,请看图片 红色框是listview里动态添加的tiem 本次消费次数和本次车牌是需要用户输入的,在我点击确定消费的时候 需要把红色框里的值封装成一个对象然后拿出来 请问要怎么实现 请大神帮忙做个demo 十分感谢
展开
3个回答
展开全部
您好:
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)) {
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;// 用来定义的标志性主键,可不用关心
}
}
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)) {
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;// 用来定义的标志性主键,可不用关心
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不管多少个edittext,最好在适配器中来处理。适配器的getView方法来处理。
public View getView(int positon, View convertView, ViewGroup parent){
//省略代码
convertView = inflate.inflate(R.layout.bindxml,null);//bindxml.xml就是帮顶的xml,通过EditText edittext1 = (EditText)convertView.findViewById(R.id.edittext1);
EditText edittext2 = (EditText)convertView.findViewById(R.id.edittext2);
EditText edittext3 = (EditText)convertView.findViewById(R.id.edittext3);
.....
//省略代码
}
public View getView(int positon, View convertView, ViewGroup parent){
//省略代码
convertView = inflate.inflate(R.layout.bindxml,null);//bindxml.xml就是帮顶的xml,通过EditText edittext1 = (EditText)convertView.findViewById(R.id.edittext1);
EditText edittext2 = (EditText)convertView.findViewById(R.id.edittext2);
EditText edittext3 = (EditText)convertView.findViewById(R.id.edittext3);
.....
//省略代码
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
跟获取一个又有什么区别呢?不都是这个框,然后再拿到他的值吗?值都拿到了,你想怎么封装还不是随意啊。
追问
这里的item是动态添加进去的 我只能在adapte中重写了onTextChanged 的方法但是每次只能获取到某个改变了的值。我需要的是确定之后直接封装成一个对象传给服务端,大神如果知道怎么做请帮忙简单写个demo 不胜感激涕零
追答
你一个listView是一行是吧?那对listView遍历一遍嘛,不就得到所有item的值了嘛。
找到listView,然后getChildAtPostion(),再findViewById,就可以得到你想要的EditText了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询