I'm VERY new to programming, and my teacher doesn't really teach. I've been trying to learn from her example code but I have no idea what I'm doing and I probably worded the question wrong because she never once explained what an argument/variable/object/anything is. Anyways, I'm attempting to make a program that allows me to add a restaurant with a name, phone number, and rating and then be able to update any section of that. E.g. Just the phone number. Here's the code I have:
public void update( View view)
{
String u1 = updateold.getText().toString();
String u2 = updatenewname.getText().toString();
String u3 = updatenewphone.getText().toString();
String u4 = updatenewrating.getText().toString();
if(u1.isEmpty())
{
Message.message(getApplicationContext(),"Please enter the name of the restaurant you want to update!");
}
else {
if (u2.isEmpty() & u3.isEmpty() & u4.isEmpty()) {
Message.message(getApplicationContext(), "Please enter some information to be updated!");
} else {
int a = helper.updateName(u1, u2, u3, u4);
if (a <= 0) {
Message.message(getApplicationContext(), "Unsuccessful");
updateold.setText("");
updatenewname.setText("");
updatenewphone.setText("");
updatenewrating.setText("");
} else {
Message.message(getApplicationContext(), "Updated");
updateold.setText("");
updatenewname.setText("");
updatenewname.setText("");
updatenewphone.setText("");
updatenewrating.setText("");
}
}
So that section right there is in my MainActivity.java, and it calls a.. Function? From a class to update it. That code is:
public int updateName(String oldName , String newName , String newPhone, String newRating)
{
if(!newName.equals("")){
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.NAME,newName);
String[] whereArgs= {oldName}; //this is a string array object
//String array stores and allows the program to look at each character individually
//whereArgs = "JOE", then J is in index position 0, O is in index position 1, and E is in index position 2
int count = db.update(myDbHelper.TABLE_NAME,contentValues, myDbHelper.NAME " = ?",whereArgs );
return count;
}
if(!newPhone.equals("")) {
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.PHONE_NUMBER, newPhone);
String[] whereArgs = {oldName}; //this is a string array object
//String array stores and allows the program to look at each character individually
//whereArgs = "JOE", then J is in index position 0, O is in index position 1, and E is in index position 2
int count = db.update(myDbHelper.TABLE_NAME, contentValues, myDbHelper.PHONE_NUMBER " = ?", whereArgs );
return count;
}
if(!newRating.equals("")){
//Message.message(myhelper.context, "Hit Non-Null Rating"); use this to test where the code is hitting
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.RATING,newRating);
String[] whereArgs= {oldName}; //this is a string array object
//String array stores and allows the program to look at each character individually
//whereArgs = "JOE", then J is in index position 0, O is in index position 1, and E is in index position 2
int count = db.update(myDbHelper.TABLE_NAME,contentValues, myDbHelper.RATING " = ?",whereArgs );
return count;
}
//should never hit the else if or the return after it, but I'm so confused I'm adding random code
else if(newName.equals("") && newPhone.equals("") && newRating.equals("")){
return 0;
}
return 0;
}
I can update the name of the restaurant, but when I try to change the phone number or rating it returns unsuccessful. I know it's hitting the block of code to update the phone/rating but then for some reason it's not updating it? I thought maybe the whereArgs was sending it to the wrong place, but if that were the case it would change the name of the restaurant to the new phone number/rating. I'm so confused. Please help and speak to me like I'm a toddler because at this point that's what I feel like.
I also realize that even if the code worked, it would only update one piece of the info at a time because of the returns inside each individual if statement, but one problem at a time.
CodePudding user response:
I noticed you had "where arguments" set incorrectly.
Try the code below.
public int updateName(String oldName , String newName , String newPhone, String newRating)
{
if(newName.isNotEmpty()){
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.NAME,newName);
String[] whereArgs= {oldName};
int count = db.update(myDbHelper.TABLE_NAME,contentValues,
myDbHelper.NAME " = ?", whereArgs);
return count;
}
if(newPhone.isNotEmpty()) {
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.PHONE_NUMBER, newPhone);
String[] whereArgs = {oldName};
int count = db.update(myDbHelper.TABLE_NAME, contentValues,
myDbHelper.NAME " = ?", whereArgs );
return count;
}
if(newRating.isNotEmpty()){
SQLiteDatabase db = myhelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(myDbHelper.RATING,newRating);
String[] whereArgs= {oldName};
int count = db.update(myDbHelper.TABLE_NAME,contentValues,
myDbHelper.NAME " = ?",whereArgs );
return count;
}
if(newName.isEmpty() && newPhone.isEmpty() && newRating.isEmpty()){
return 0;
}
return 0;
}