3

我想获取我的 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();
                }
            }
        });

这是我的适配器类:

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

4

5 回答 5

12

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

执行此操作的正确方法是使用 a of表示的值的Collection对象作为您的数据。这样,您只需检查该位置的对象的值,然后调用视图。当您想要获取所有值时,您可以在 中创建一个方法,然后对其进行迭代。EditTextAdaptergetViewsetText()getItems()AdapterCollection

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

于 2013-10-06T16:07:38.943 回答
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>

主要活动

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

}

在此处输入图像描述

于 2013-10-11T06:47:13.513 回答
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!
        }
    }
});
于 2013-10-09T14:41:53.393 回答
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();
                }   
            }
        });
于 2013-10-09T11:56:45.147 回答
0

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

于 2016-01-14T09:03:41.287 回答