如何在ListView中获取所有EditText的值

时间:2013-10-06 14:26:31

标签: android android-listview

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

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();
                }
            }
        });

这是我的Adapter类:

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的值,但错误不正确。

5 个答案:

答案 0 :(得分:11)

这不会像你期望的那样发挥作用。使用Adapter时,视图会被回收。这意味着只有与屏幕上可见的一样多的膨胀视图(加上一对)。因此,如果您尝试迭代所有子项,您会发现任何屏幕外项都将返回null。

执行此操作的正确方法是使用Collection对象表示 EditText的值作为Adapter数据。这样,在getView中,您只需检查该位置对象的值,然后在视图上调用setText()。如果要获取所有值,请在getItems()中创建Adapter之类的方法,然后迭代Collection

如果您发布Adapter代码的相关部分,我可以向您展示如何执行此操作。

答案 1 :(得分:10)

尝试这种方式并根据您的要求进行修改。我使用了HashMap的ArrayList而不是NodeList

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="5dp"
              android:gravity="center"
              android:orientation="vertical">

    <ListView
            android:id="@+id/lst"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:smoothScrollbar="true" />

    <Button
        android:id="@+id/getAllValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Get All Value"/>
</LinearLayout>

<强> row_edit_string.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="5dp"
              android:orientation="vertical">

    <TextView
            android:id="@+id/propertyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/editPropertyValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"/>
</LinearLayout>

<强> MainActivity

package com.example.MyTest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends Activity {

    private ListView lst;
    private Button getAllValue;
    private EditorAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lst=(ListView)findViewById(R.id.lst);
        getAllValue=(Button)findViewById(R.id.getAllValue);

        ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
        HashMap<String,String> row1 = new HashMap<String, String>();
        row1.put("name","FirstName");
        row1.put("value","FirstValue");
        data.add(row1);
        HashMap<String,String> row2 = new HashMap<String, String>();
        row2.put("name","SecondName");
        row2.put("value","SecondValue");
        data.add(row2);
        HashMap<String,String> row3 = new HashMap<String, String>();
        row3.put("name","ThirdName");
        row3.put("value","ThirdValue");
        data.add(row3);
        HashMap<String,String> row4 = new HashMap<String, String>();
        row4.put("name","FourthName");
        row4.put("value","FourthValue");
        data.add(row4);
        HashMap<String,String> row5 = new HashMap<String, String>();
        row5.put("name","FifthName");
        row5.put("value","FifthValue");
        data.add(row5);
        adapter = new EditorAdapter(this,data);
        lst.setAdapter(adapter);

        getAllValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String allValues="";
                ArrayList<String> valueList = new ArrayList<String>();
                for (int i=0;i<adapter.getCount();i++){
                    allValues +=((HashMap<String,String>)adapter.getItem(i)).get("value")+ ",";
                    valueList.add(((HashMap<String,String>)adapter.getItem(i)).get("value"));
                }
                // use this valueList as per ur requirement
                allValues = allValues.substring(0,allValues.length()-1);
                Toast.makeText(MainActivity.this,allValues,Toast.LENGTH_LONG).show();

            }
        });

    }

}


    class EditorAdapter extends BaseAdapter {

    Context context;
    ArrayList<HashMap<String,String>> data;

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

        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int pos) {
        return data.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.row_edit_string,null,false);
            holder.editPropertyValue = (EditText) convertView.findViewById(R.id.editPropertyValue);
            holder.propertyName = (TextView) convertView.findViewById(R.id.propertyName);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.propertyName.setText(data.get(position).get("name"));
        holder.editPropertyValue.setText(data.get(position).get("value"));

        convertView.setTag(holder);
        return convertView;
    }

    class ViewHolder {
        EditText editPropertyValue;
        TextView propertyName;
    }

}

enter image description here

答案 2 :(得分:2)

我现在正在复制您的问题并为您提供快速解决方案。请记住,您的代码未经过优化,我的代码也未进行优化。而且,我刚刚编写了我的代码,它甚至没有在语法上进行过测试。

我们的想法是跟踪每个EditText的值。我使用了HashMap。

这是你的新适配器类:

public class EditorAdapter extends BaseAdapter {

    Context context;
    NodeList nodes;
    int sizeOfList;

    //here is the hashmap and its getter I talked about upstairs
    HashMap<Integer, String> mValues = new HashMap<Integer, String>();
    public HashMap<Integer, String> getValues() {
        return mValues;
    }
    public EditorAdapter(Context context, NodeList nodes, int sizeOfList){

        this.context = context;
        this.nodes = nodes;
        this.sizeOfList = sizeOfList;
        System.out.println("No . of items in nodes"+nodes.getLength());
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //if one of the convertView is reused (but in ur case discarded) let me update the value for that position in case changed
        if(convertView!=null && nodes.item(position).getNodeType() == Node.ELEMENT_NODE) {
            mValues.put((Integer) convertView.getTag(), ((TextView)convertView.findViewById(R.id.propertyValue)).getText().toString());
        }
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = new View(context);
        Node node = nodes.item(position);

        if (node.getNodeType() == Node.ELEMENT_NODE) { 
            rowView = inflater.inflate(R.layout.row_edit_string, null);
            //set the tag of this view to the position
            rowView.setTag(Integer.valueOf(position));
            TextView propertyName = (TextView) rowView.findViewById(R.id.propertyName);
            Element child = (Element) node;
            propertyName.setText(child.getAttribute("name"));
            EditText editPropertyValue = (EditText) rowView.findViewById(R.id.propertyValue);
            if(position == 1){
                editPropertyValue.requestFocus();
            }
            if(node.getNodeName().equals("string")){

                editPropertyValue.setText(child.getTextContent());
            } else {
                editPropertyValue.setText(child.getAttribute("value"));
            }
        }

        return rowView;
    }

}

现在让我们更新你的onClick。这里唯一的问题是mValues是不够的,因为hashmap只保证丢弃/重用convertViews的值,但不保证屏幕上已有的EditTexts值。

    final ListView editorList = (ListView) findViewById(R.id.editorList);
    final EditorAdapter mAdapter = new EditorAdapter(context, nodes, sizeOfList);
    editorList.setAdapter(mAdapter);


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

    @Override
    public void onClick(View v) {
        try {
            //I am getting the hashmap
            HashMap<Integer, String> mValues = mAdapter.getValues();
            for(int i=0;i<editorList.getChildCount() ;i++) {
                View layout = editorList.getChildAt(i);
                //I am checking if any of the visible children is an EditText and updating its value in the HashMap
                if(layout.getTag()!=null && layout.getTag() instanceof Integer) {
                    mValues.put((Integer) layout.getTag(), ((TextView)layout.findViewById(R.id.propertyValue)).getText().toString());
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        //now if you want the Text of the EditText on position `2` (for example) Use:
        String valueAtPosition_2 = mValues.get(position);
        if(valueAtPosition_2 == null) {
            //this means the view at position two is not really an EditText and the node at position 2 is not of type Node.ELEMENT_NODE
        }
        else {
            //here is your value!
        }
    }
});

答案 3 :(得分:0)

尝试这种方式希望这可以帮助你

commitButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try{
                    //System.out.println("Size of List : " + editorList.getChildCount());
                    for(int i =0;i<editorList.getChildCount() ;i++){
                        Node node = nodes.item(i);
                         Element child = (Element) node;
                        if (node.getNodeType() == Node.ELEMENT_NODE) { 

                            if(node.getNodeName().equals("string")){
                                System.out.println("TextContent : "+child.getTextContent());
                            } else {
                                System.out.println("AttributeValue : "+child.getAttribute("value"));
                            }
                        }




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

答案 4 :(得分:0)

为什么不尝试使用容器保存列表项作为子项的滚动视图。在单击侦听器上,您可以遍历所有子节点,并找出在edittext中输入的值。如果真的不需要listview你可以试试这样的东西。

相关问题