What Makes My EditText Empty?

2019-08-20 03:25发布

问题:

I created the database. I used edittext to insert data to my database. There is a problem in my edittexts. When i used log to show the data in my edittexts but they are empty. I don't understand where the problem is. Please help me. Thanks in advance.

Adding Data:

public void AddData() {
        setContentView(R.layout.dbase2);
        EditText e1 = (EditText) findViewById(R.id.eText1);
        EditText e2 = (EditText) findViewById(R.id.eText2);
        EditText e3 = (EditText) findViewById(R.id.eText3);
        EditText e4 = (EditText) findViewById(R.id.eText4);

        String bn = dContents;
        String pn = e1.getText().toString();
        String at = e2.getText().toString();
        String tp = e3.getText().toString();
        String py = e4.getText().toString();

        SQLiteDatabase db = book.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put("bNum", bn);
        cv.put("pName", pn);
        cv.put("author", at);
        cv.put("type", tp);
        cv.put("pyear", py);

        db.insert("btable", null, cv);
        db.close();


    }

Dbase 2.xml

 

<TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:text="Ürün İsmi" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/t2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/eText1" android:layout_centerHorizontal="true" android:text="Yazar" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/eText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/eText1" android:layout_below="@+id/t2" android:ems="10" /> <EditText android:id="@+id/eText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/t1" android:gravity="top" android:layout_centerHorizontal="true" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/t3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/t2" android:layout_below="@+id/eText2" android:text="Türü" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/eText3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/eText2" android:layout_below="@+id/t3" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/t1" android:layout_below="@+id/eText3" android:text="Yayın Yılı" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/eText4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/eText3" android:layout_below="@+id/textView1" android:ems="10" /> <Button android:id="@+id/adding" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/eText4" android:layout_centerHorizontal="true" android:layout_marginTop="36dp" android:text="Kayıt Ekle" /> </RelativeLayout> </pre>

回答1:

Your problem is you are declaring an EditText and fetching its value at same time. So it will be blank all the time.

What you should do is, declare your EditText in onCreate() method and fetch it's value on some event like onClick();

Try my way,

public void AddData() 
{
    setContentView(R.layout.dbase2);
    EditText e1 = (EditText) findViewById(R.id.eText1);
    EditText e2 = (EditText) findViewById(R.id.eText2);
    EditText e3 = (EditText) findViewById(R.id.eText3);
    EditText e4 = (EditText) findViewById(R.id.eText4);
}

public void onClick ( View view ) // I am assuming you have one command Button
{

    String bn = dContents;
    String pn = e1.getText().toString();
    String at = e2.getText().toString();
    String tp = e3.getText().toString();
    String py = e4.getText().toString();

    SQLiteDatabase db = book.getWritableDatabase();
    ContentValues cv = new ContentValues();

    cv.put("bNum", bn);
    cv.put("pName", pn);
    cv.put("author", at);
    cv.put("type", tp);
    cv.put("pyear", py);

    db.insert("btable", null, cv);
    db.close();
}