0

I want to insert mobile numbers in my SQLite database the values in my edittext fields. The edittext is dynamically created once the button is clicked as shown in the screenshot. User can add multiple numbers. Currently my code is adding only one number in my database table. Please see my codes below

Main Layout code:

        <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"/>

Dynamic add number Layout code:

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

.java code of dynamically adding of eddittext

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

Reference in my code of dynamically adding and removing edittext is this link.

Here's my code of inserting values to database

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dylan
  • 129
  • 2
  • 16

1 Answers1

0

You can consider creating a loop for that for example:

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

Try that and give a feedback

Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30