user555910
user555910

Reputation: 2667

press the save button EditText value is stored in to the database

I have four EditText and two buttons namely, save and review button.

If i press the save button EditText value is stored in to the database and on clicking the review button saved value in the database is listed in ListView.

How is this done please anybody explain to me.

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;


public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, "MedicinePill", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS names3 ("
                + BaseColumns._ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last VARCHAR ,dose2 VARCHAR ,dose3 VARCHAR)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Steps to upgrade the database for the new version ...
    }
}

//this is my database class in this where i add the insert data from the database






import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Medical extends Activity
{
    Button AddMedicine,ReviewMedicine;
    EditText dose1,dose2,dose3;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        AddMedicine = (Button)findViewById(R.id.add);
        ReviewMedicine = (Button)findViewById(R.id.rev);
        dose1 = (EditText)findViewById(R.id.Edit1);


        AddMedicine.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                String s1 = dose1.getText().toString();


                // TODO Auto-generated method stub

            }
        });
    }
}

//and this is my activity class

how i add edittext value in to database pls any body help me!!!!!!!!

Upvotes: 0

Views: 7830

Answers (2)

Sonali
Sonali

Reputation: 65

You need to create class insertdose in DatabaseHelper like this:

public long insertdose(String dose1)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_dose, dose1);
        return db.insert(DATABASE_TABLE, null, initialValues);

    }

and call it at your onClick view

Upvotes: 2

Greg McGowan
Greg McGowan

Reputation: 1360

You will need to create a method in the DatabaseHelper class that takes in four strings and adds them to your database.

Then in your onClick() method in your Medical activity class you can get the string values from your 4 EditText fields and call your method with these values.

I would recommend going through the Notepad Tutorial. This should provide a good example of what you want to do.

Upvotes: 1

Related Questions