如何获取ListView中所有EditText的值 [英] How to get values of all EditText inside a ListView

查看:16
本文介绍了如何获取ListView中所有EditText的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我的 ListView 中存在的所有 EditText 元素的所有值.这是我的代码:

I want to get all the values of all the EditText elements that are present inside my ListView. This is my code :

final ListView editorList = (ListView) findViewById(R.id.editorList);
        final EditorAdapter adapter = new EditorAdapter(context, data);
        editorList.setAdapter(adapter);


        Button commitButton = (Button) findViewById(R.id.commit_button);
        commitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    //System.out.println("Size of List : " + editorList.getChildCount());
                    for(int i =0;i< data.size() ;i++){
                        System.out.println("Size of List : " + data.size());

                        EditText value = adapter.getItem(i);
                        String propertyValue = value.getText().toString();
                        System.out.println("PropertyValue : " + propertyValue);


                    }
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        });

这是我的适配器类:

package in.omerjerk.preferenceseditor;


public class EditorAdapter extends BaseAdapter {

Context context;
ArrayList<HashMap<String,String>> data;
EditText[] mHolders;

public EditorAdapter(Context context, ArrayList<HashMap<String,String>> data){

    this.context = context;
    this.data = data;
    System.out.println("No . of items in nodes"+data.size());
    mHolders = new EditText[data.size()];

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

@Override
public EditText getItem(int pos) {
    // TODO Auto-generated method stub
    return mHolders[pos];
}

@Override
public long getItemId(int pos) {
    // TODO Auto-generated method stub
    return pos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    if(convertView==null){
        System.out.println("CONVERT VIEW IS NULL");
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
        holder.editPropertyValue = (EditText) convertView.findViewById(R.id.propertyValue);
        holder.propertyName = (TextView) convertView.findViewById(R.id.propertyName);

        holder.propertyName.setText(data.get(position).get("propertyName"));
        holder.editPropertyValue.setText(data.get(position).get("propertyName"));
        convertView.setTag(holder);



    }else{
        System.out.println("CONVERT VIEW NOT NULL");
        holder = (ViewHolder) convertView.getTag();
        holder.propertyName.setText(data.get(position).get("propertyName"));
        holder.editPropertyValue.setText(data.get(position).get("propertyName"));
        convertView.setTag(holder);
        mHolders[position] = new EditText(context);
        mHolders[position] = holder.editPropertyValue;
    }

    return convertView;
}

}

我的输出出现奇怪的错误.mHolders 数组最多只包含 6-7 个元素,并且这些元素在整个数组中重复.我能够获取 EditText 的值,但错误不正确.

I'm getting strange error in my output. The mHolders array only contain upto 6-7 elements and these elements are repeated in the entire array. I'm able to get the values of EditText but the error is not correct.

推荐答案

这不会像您期望的那样工作.使用 Adapter 时会回收视图.这意味着只有在屏幕上可见的膨胀视图(加上几个).因此,如果您尝试遍历所有子项,您会发现任何屏幕外项目都将返回 null.

This is not going to work the way you'd expect. Views are recycled when using an Adapter. This means that there are only as many inflated views as there are visible on screen (plus a couple). So, if you're trying to iterate over all of the children, you will find that any offscreen items will return null.

正确的方法是使用Collection对象的代表EditText的值作为您的Adapter 数据.这样,在 getView 中,您只需检查该位置对象的值,然后在视图上调用 setText().当您想要获取所有值时,您可以在 Adapter 中创建一个类似于 getItems() 的方法,然后遍历该 Collection.

The proper way to do this is to use a Collection of objects that represent the values of the EditText as your Adapter data. This way, in getView you simply check the value of the object at that position, and then call setText() on the view. When you want to get all values, you create a method like getItems() in the Adapter, and iterate over that Collection.

如果你把 Adapter 代码的相关部分贴出来,我可以告诉你具体怎么做.

If you post the relevant parts of the Adapter code, I can show you exactly how to do this.

这篇关于如何获取ListView中所有EditText的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆