android - 从 layoutinflater 中的 edittext 中保存值

标签 android layout-inflater

我正在使用 layoutinflatter 在单击按钮时显示多个 edittexts 和 textviews...我如何处理 edittetxts 和 textviews 中的值..? 我的代码..

 buttonAdd.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater =
                    (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addView = layoutInflater.inflate(R.layout.row, null);
            TextView textOut = (TextView)addView.findViewById(R.id.textout);
            EditText val=(EditText)addView.findViewById(R.id.val);
            textOut.setText(textIn.getText().toString());
            Button buttonRemove = (Button)addView.findViewById(R.id.remove);
            buttonRemove.setOnClickListener(new View.OnClickListener(){

                @Override
                public void onClick(View v) {
                    ((LinearLayout)addView.getParent()).removeView(addView);
                }});

            container.addView(addView);
        }
    });

行.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/textout"
        android:layout_weight="1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/val"
        android:layout_weight="1"
        android:hint="Value" />

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Remove"
        />
</LinearLayout>

我需要在数据库中插入值作为 textview(textout) 作为列名和 edittext(val) 作为值...我该如何解决这个问题。 ?

最佳答案

当您尝试在布局上添加行时, 你正在创建这样的对象

final View addView = layoutInflater.inflate(R.layout.row, null);

现在只需创建一个 ArrayList,您可以在其中像这样添加此 View

list.add(addView);

现在最后,当你想提交数据时,只需迭代所有 View ,并使用获取其子元素

((TextView)addView.findViewById(<your text view's id>)).getText();

并将其保存到您的数据库中,或您想要使用它的任何其他地方。

关于android - 从 layoutinflater 中的 edittext 中保存值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277581/

相关文章:

android - 录制按钮状态

android - TextView文字大小等属性

Android - 跟踪 IntentService 中的多个操作

java - 使用应用程序发送 HTTP 请求 - Android Studio

android - 如何在其他布局中 inflatedView ?

android - 如何检测设备何时获得新 IP?

java - Android 通过单击按钮删除动态创建的微调器和按钮

具有多种布局的Android ListView

android - SDK 19 及以下版本无法识别 android.support.v7.widget.CardView ,错误膨胀类 <unknown>

android - 如何膨胀FrameLayout?