Android 如何从充气LinerLayout的edittext中获取文本

Android 如何从充气LinerLayout的edittext中获取文本,android,android-layout,android-edittext,layout-inflater,Android,Android Layout,Android Edittext,Layout Inflater,我有一个LinearLayout(比如说parent),我正在为另一个LinearLayout(比如child)充气,其中包含一个EditText&ImageButton 点击主布局中的一个按钮,我正在放大子布局。单击子版面的ImageButton时,该版面将被删除。我已经设置好了,没有问题 现在,当点击ActionBar的Save按钮时,我想获得子版面的所有EditText的文本 下面是我用来膨胀我的观点的方法。而mainLayout是我的父布局。现在,我如何获取活动中任何位置添加的所有Edi

我有一个
LinearLayout
(比如说parent),我正在为另一个
LinearLayout
(比如child)充气,其中包含一个
EditText
&
ImageButton

点击主布局中的一个按钮,我正在放大子布局。单击子版面的
ImageButton
时,该版面将被删除。我已经设置好了,没有问题

现在,当点击
ActionBar
的Save按钮时,我想获得子版面的所有
EditText
的文本

下面是我用来膨胀我的观点的方法。而
mainLayout
是我的父布局。现在,我如何获取活动中任何位置添加的所有
EditText
文本

private void addEditView(final LinearLayout mainLayout) {

    LayoutInflater inflater = LayoutInflater.from(this);
    View theInflatedView = inflater.inflate(R.layout.layout_add_phone_number_listview, null);

    //Get the EditText in you xml to set the new id 
    EditText editText = (EditText) theInflatedView.findViewById(R.id.et_pn);
    editText.setId(4000+1);//mainLayout.getChildCount()

    ImageButton remove = (ImageButton) theInflatedView.findViewById(R.id.btn_remove_raw);
    remove.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            mainLayout.removeView((View)v.getParent());
        }
    });  
    //add the view into the LinearLayout (global variable)        
    mainLayout.addView(theInflatedView);
}
我试过什么:

LinearLayout ll = (LinearLayout)findViewById(R.id.phone_info);

            int count = ll.getChildCount();
            for(int i =0;i<count;i++)
            {
                Log.e("child count:", "count:" + count + "ID:" + ll.getChildAt(i).getId());
                View vi = ll.getChildAt(i);
                if(vi instanceof EditText)
                {
                    // you got the spinner
                    EditText s = (EditText) findViewById(4000+i);
                    Log.e("Item selected",s.getText().toString());
                }
            }
LinearLayout ll=(LinearLayout)findViewById(R.id.phone\u info);
int count=ll.getChildCount();

对于父视图
mainLayout
上的(int i=0;i使用
findViewById()
方法查找子线性布局。然后在其上查找具有相同逻辑的子元素。每个视图的子视图都有
findViewById()
方法。

<

ArrayList<String> edittextValues = new ArrayList<String>();
for(int i = 0; i < mainLayout.getChildCount(); i++){
    View view = (View)mainLayout.getChildAt(i);
    EditText editText = (EditText) view.findViewById(R.id.et_pn);
    edittextValues.add(editText.getText().toString());
}
ArrayList edittextValues=new ArrayList();
对于(int i=0;i
您在这里遇到的问题是,使用
列表视图
时,您试图创建自己的列表,将每个
视图添加到列表中

我提议的解决办法: 使用带有
ArrayAdapter
ListView
。您将能够为
ArrayAdapter
中的每一行访问
EditText
ImageButton

编辑
根据我们的讨论,问题是由于
ListView
的循环属性,用户输入的文本在添加行后没有保存或丢失,您应该自定义
ArrayAdapter
以保存用户键入的文本。
这可以在
ArrayAdapter
getView
中完成,如下所述:。

您是否尝试过String text=editText.getText().toString();
setId
需要什么?@Raghunandan:如果不需要,我会删除它。我只需要eddittext的文本。这个ID对我来说并不重要now@I-droid将此
EditText EditText
声明为实例变量。将EditText初始化为
EditText=(EditText)inflatedView.findViewById(R.id.et\u pn);
then
editText.getText().toString()
@Raghunandan:当我有多个
EditText
时,这会起作用吗?我已经尝试过这种方法。但是在这种情况下,当我添加新项目时,我无法保留以前的
EditText
的值。如果有工作示例,那么我可以再次使用它。也许我错过了一些东西,但你应该能够访问O的所有值ArrayAdapter构造函数ArrayAdapter中给定的对象列表(上下文上下文、int资源、int textViewResourceId、列表对象)。您也可以自定义ArrayAdapter以匹配您的数据模型。当我将新项添加到ArrayAdapter时,用户已经输入的值应该保留在视图中。但是当我使用适配器更新视图时,
EditText
的那些文本被清除。我想这是指向ListView回收的链接,其他问题的原因等等此处的解决方案可能相同:。基本上,您需要做的是将edittext的输入保存在某些数据结构/对象列表或数组中。在最后一行获取
nullPointerException
。表示edittext没有到达。仍然获取相同的结果。