I have a class that contains:
#Override
public void onClick(View createView) {
switch (createView.getId()) {
case R.id.save_button:
Appointments add = new Appointments();
add.addAppointment(TITLE);
add.addAppointment(TIME);
break;
}
}
I want to be able to call the addAppointment method which is in another class:
public void addAppointment(String string) {
// Insert a new record into the Appointment data source.
// You would do something similar for delete and update.
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TITLE, string);
values.put(TIME, string);
db.insertOrThrow(TABLE_NAME, null, values);
}
I have tried this way, but when I click the onclick , the program crashes
#Override
public void onClick(View createView) {
switch (createView.getId()) {
case R.id.save_button:
Appointments add = new Appointments();
add.addAppointment(TITLE, TIME); //Send both together
break;
}
}
I want to be able to call the addAppointment method which is in another class:
public void addAppointment(String title, String time) { // Change method header to accept both Strings here
// Insert a new record into the Appointment data source.
// You would do something similar for delete and update.
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title); // second param is what you passed in for first param of method
values.put("time", time); // second param here is whatever you passed in for second param of method
db.insertOrThrow(TABLE_NAME, null, values);
}
I don't know what TITLE and TIME are because I don't see them declared or initialized anywhere but inside the vaules.put() should be (key, value) so you make the key something descriptive about the value and the value obviously just that. Normally all caps represent a constant so just something to think about to keep up with standards
write method with two parameters 1st.calling method one after other with one parameter won't give you both values.
add.addAppointment(TITLE,TIME);
Related
I have a problem, I created database for all activities, and in each activity I should insert information to database, so for the first activity , the insert is done , for the second activity I update the row with new insertion to complete all information and so on, my problem is that I don't know how to refer to the last row, I mean what should I do that make the update for the second activity occurs to the last row that has been insert in the first activity, do you have any suggestions ???
Well you can just use the primary key. When you insert something into the database you get as a return value the primary key. You can add this to the Intent that opens the other Activity and that way refer back to the row you previously inserted.
Edit:
I don't know if you are working with and SQLiteDatabase Object or with a ContentProvider, but in any case the code would be pretty much the same. In this example I will work directly with an SQLiteDatabase Object, even though using ContentProviders is in most cases the better alternative.
In your first Activity:
// When you perform an insert you get the id of the row which was just inserted.
long id = sqliteDatabase.insert("some_table", null, contentValues);
// The id will be -1 if an error occured
if(id >= 0) {
...
}
...
// When you start your second Activity you can add the id to the Intent
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
// The String is a tag with which you can later retrieve the id from the Intent.
// Note that you should never hardcode String like that, use constants in your app.
intent.putExtra("rowId", id);
In the onCreate Method of your second Activity you can retrieve the id:
#Override
protected void onCreate (Bundle savedInstanceState) {
// Check if the Activity has been created for the first time
if(savedInstanceState == null) {
// Retrieve the Intent with which the Activity was started
Intent intent = getIntent();
long id = intent.getLongExtra ("rowId", -1);
// If id is valid proceed
if(id >= 0) {
Cursor cursor = sqliteDatabase.query("some_table", columns, "_id = ?",
new String[] { String.valueOf(id) }, null, null, null, null);
// Check if Cursor is valid and if yes read your data.
if(cursor != null) {
...
}
}
}
}
The best way to do this would be to add a column to your database which will hold the time the row was inserted. Then when you need the latest row, query for the one with the most current time. An example SQL string would be:
SELECT * FROM my_table WHERE 1 ORDER BY time_stamp LIMIT 1
i am here to ask for help again, my problem is that in first activity i have values that i insert into database , in the second activity when i try to insert the values it creates a new row, what i want is the values of second activity completes the row of the first activity. And also how could i refer to the last row ??
i don't know how to do it! so, is there any ideas ?
public void updateMENAGE(int id, int a16, int b17, String rm_18_1ts,
String rm_18_2ts, int c19, int d20, int e21) {
ContentValues values = new ContentValues();
values.put(col_Type_habitat,a16);
values.put(col_Statut_occupation ,b17);
values.put(col_Nombre_ménages_habitant_logement,rm_18_1ts);values.put(col_Nombre_pièces_occupes_ménage,rm_18_2ts);
values.put(col_Mode_principal_approvisionnement_eau_potable, c19);
values.put(col_Mode_principal_éclairage,d20);
values.put(col_Mode_principal_assainissement,e21);
db.update(MENAGE,values,_id+"="+id, null);
}
public class ActivityDeux extends Activity {
DBAdapter db = new DBAdapter(this);
public void ajouter(View v) {
db.open();
db.updateMENAGE(1,a16,b17,rm_18_1ts,rm_18_2ts,c19,d20,e21);
db.close();
}
You don't need to insert new record. You need to update existing record. That is your solution
I am trying to update my database. It is not really updating. I think it is due to my update command
my main activity is a listview activity. I have a button on the action bar that basically starts a new activity with two editboxes. So once I click that button, I create a new database item. It is originally set to two empty strings. On my second activity, I have a save button that is supposed to save my changes into the textboxes
so after my main activity takes me to my second activity, the code is as follows
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note);
// if new then create new db, else retrieve old database, right now
// assume everything is new
db = new DBHandler(this);
nowAdd = new UENote("", "");
db.addNote(nowAdd);
}
i update my nowAdd contact strings through this function
public void updateColumnData() {
// gets the strings from edittext
EditText textTitle = (EditText) findViewById(R.id.editText1);
EditText textSubject = (EditText) findViewById(R.id.editText2);
// converts the string
String titleString = textTitle.getText().toString();
String subjectString = textSubject.getText().toString();
nowAdd.setSubject(subjectString);
nowAdd.setTitle(titleString);
db.updateNote(nowAdd);
}
the update note function is as follows
public int updateNote(UENote note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, note.getTitle());
values.put(KEY_SUBJECT, note.getSubject());
// updating row
return db.update(TABLE1, values, KEY_ID + " = ? ",
new String[] { String.valueOf(note.getID()) });
}
Make sure that the id you are passing in the db.update() is really the same one that you want to update. From the code snippet, it's not obvious how you are getting the value of note.getID(). Are you making query for empty record to get the id?
Assuming that KEY_ID is an auto incremented primary key, I would then debug getID() to check if it returns the same id of the record you wanted to update.
This is the Method by which I insert the row in my application's database. But its insert only one row. So what I have to do add multiple rows in database. This code I had used for the new User Registration in my application.
Thanks in advance.....
public void addProductToCart(String username, String password,
String email, String contact, String bloodgroup, String city, String state) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(colUsername, username);
contentValues.put(colPassword, password);
contentValues.put(colEmail, email);
contentValues.put(colContact, contact);
contentValues.put(colBloodgroup, bloodgroup);
contentValues.put(colCity, city);
contentValues.put(colState, state);
db.insert(USERTABLE, colUsername, contentValues);
db.close();
context.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, "You have registered successfully.",
Toast.LENGTH_LONG).show();
context.finish();
}
});
}
I don't think there is something else that android supports, but my solution will be to build for that a new class with the Fields you need, and then pass as parameter to the addProductToCart method a list of all the objects you want to put. Inside the method do for loop until you get to the end of the list.
It is not possible to register multiple users at a time, if u want to add more number of users at a time use the back-end process in you database class.
try to add the values in your code instead of using the user Interface. If you set a on click function to a button then call the method as many times u want with the values you prefer.
Like this,
db.addProductToCart(username1,password1,email1, contact1,bloodgroup1,city1,state1);
db.addProductToCart(username2,password2,email2, contact2,bloodgroup2,city2,state2);
db.addProductToCart(username3,password3,email3,contact3,bloodgroup3,city3,state3);
So this is my first app and first database. I think I have finally created the DBAdapter correctly, and now I am trying to implement that into my main activity.
I have a save button listed under an onClick
public void onClick(View src) {
switch(src.getId()){
...other buttons here...
case R.id.buttonSave:
db.open();
long id;
id = db.insertFinalscore("20110612", "91", "18");
db.close();
break;
}
}
As I'm sure you can not see, I am trying to insert that data into the table. I have created an instance of DBAdapter inside my onClick:
public void onClick(View src) {
//***DATABASE INFO***//
DBAdapter db = new DBAdapter(this);
Is anyone able to tell me what I am missing? I am getting the following warning:
case R.id.buttonSave:
db.open();
long id; <--- The local variable id is never read
id = db.insertFinalscore("20110612", "91", "18");
db.close();
You are getting a warning, not an error. Basically the warning is saying that the variable id is never actually used. While you do set its value, you never read that value making the variable worthless. Your program will run just fine as is, but you might as well remove the variable id.
Your program should now look like this:
public void onClick(View src) {
switch(src.getId()){
...other buttons here...
case R.id.buttonSave:
db.open();
db.insertFinalscore("20110612", "91", "18");
db.close();
break;
}
}