如何在sqlite数据库中插入动态edittext字段的值

时间:2018-10-10 09:21:32

标签: android sqlite android-edittext dynamically-generated

我想在我的SQLite数据库中插入移动电话号码,并将其在edittext字段中的值。单击屏幕快照中所示的按钮后,将动态创建edittext。用户可以添加多个数字。目前,我的代码在数据库表中仅添加一个数字。请在下面查看我的代码

主要布局代码:

        <EditText
            android:id="@+id/editNumber"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Mobile Number"
            android:inputType="number"
            android:layout_gravity="center"/>

        <Button
            android:id="@+id/btnNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@android:drawable/ic_input_add"
            android:text="Add Number"
            android:layout_gravity="center"
            android:onClick="onAddField"/>

动态加号布局代码:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">

<EditText
    android:id="@+id/editNumber"
    android:layout_width="260dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="32dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="Mobile Number"
    android:inputType="number" />
<Button
    android:id="@+id/delete_button"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="1"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@android:drawable/ic_delete"
    android:onClick="onDelete"/>

</LinearLayout>

.eddittext动态添加的.java代码

public void onAddField(View v) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.field, null);
    parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}

public void onDelete(View v) {
    parentLinearLayout.removeView((View) v.getParent());
}

我的代码中动态添加和删除edittext的参考是this链接。

这是我在数据库中插入值的代码

if (editNumber.length() != 0) {
            boolean isInserted = myDB.addData(editNumber.getText().toString(),

            if (isInserted == true) {
                Toast.makeText(this, "Successfully added!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Something went wrong, please try again.", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "All fields are required.", Toast.LENGTH_SHORT).show();
        }

DatabaseHelper addData

public boolean addData(String number){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL1, number);
    long result = db.insert(TABLE_NAME,null,contentValues);

    if(result == -1){
        return false;
    }else {
        return true;
    }
}

Image 1

1 个答案:

答案 0 :(得分:0)

您可以考虑为此创建一个循环:

private void addAll()
{
    for(int i=0;i<parentLinearLayout.getChildCount();i++)
        {
            EditText ediNumber=(EditText)parentLinearLayout.getChildAt(i);

           if (editNumber.getText().toString().trim().length() != 0) {
            boolean isInserted = myDB.addData(editNumber.getText().toString(),

            if (isInserted == true) {
                Toast.makeText(this, "Successfully added!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Something went wrong, please try again.", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "All fields are required.", Toast.LENGTH_SHORT).show();
        }
        }
}

尝试一下并提供反馈