I have a edit tab in my app where the user can edit his details given during the time of registration .Inside the edit activity i have a save button which when clicked it saves the
data or update the datas into the database.
CODE
public void onSave(View btn)
{
EditText edtName = (EditText) findViewById(R.id.edtNameED);
EditText edtPass = (EditText) findViewById(R.id.edtPassED);
EditText edtEmail = (EditText) findViewById(R.id.edtEmailED);
EditText edtPh = (EditText) findViewById(R.id.edtPhED);
int id=0;
String where = DataBase_Server.C_ID+"=?";
String[] whereArgs = {id+""};
DataBase_Server database=new DataBase_Server(Edit.this);
SQLiteDatabase db=database.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(DataBase_Server.MOBILE_NO, edtPh.getText().toString());
values.put(DataBase_Server.PASS, edtPass.getText().toString());
values.put(DataBase_Server.EMAIL, edtEmail.getText().toString());
for(int i=0;i<edtName.length();i++)
{
id+=(int)edtName.getText().toString().charAt(i);
}
Toast.makeText(this,id+" hello", Toast.LENGTH_LONG).show();
try
{
db.update(DataBase_Server.TABLE, values,where,whereArgs);
}catch(Exception e){}
Toast.makeText(this,"UPDATE SUCESSFULL !!", Toast.LENGTH_LONG).show();
db.close();
database.close();
}
The code is giving not a single error but it is not even updating anything in the database. The database is not changed at all even after the user edit his details.
You are adding up the values of all the characters in the name:
for(int i=0;i<edtName.length();i++)
id+=(int)edtName.getText().toString().charAt(i);
This is unlikely to be your real ID value. With no record matching the WHERE condition, the update will do nothing, silently.
You must remember the ID value from when you loaded the data, because the name may have changed.
Related
i have created a app with 3 edit text and and 3 buttons,
Edittext:
- id
- name
- date
Button:
submit.
check.
delete.
name to store name of user,
date to store date,
submit to store data in sqlit database,
check to retrive data,
delete to delete the specfied id in database,
id is auto incremented it is used when deleting.
my problem here is if i not enter anything in the edit text it takes null values
and display as shown in below pic instead of taking null i want to show message "PLEASE ENTER DATA"
and whenever i delete data next id value must decrease by 1 please help me.
Before entering the data to database use should check EditTexts are null or not in on click method and if null print toast messages like below
idEditText = (EditText) findViewById(R.id.editText_id);
nameEditText = (EditText) findViewById(R.id.editText_Name);
dateEditText = (EditText) findViewById(R.id.editText_Date);
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().();
String date = dateEditText.getText().toString().();
if(id.length() == 0 || name.length() == 0 || date.length())
{
Toast.makeText(this, "Please fill all details",Toast.LENGTH_SHORT).show();
}
Try this
idEditText =(EditText) findViewById(R.id.editText_id);
nameEditText =(EditText)findViewById(R.id.editText_Name);
dateEditText =(EditText)findViewById(R.id.editText_Date);
private boolean checkEmptyFields() {
boolean check = false
String id = idEditText.getText().toString().trim();
String name = nameEditText.getText().toString().trim();
String date = dateEditText.getText().toString().trim();
if (id.isEmpty()) {
idEditText.setError("Invalid Input");
check = true;
} else {
dEditText.setError(null);
check = false;
}
if (name.isEmpty()) {
nameEditText.setError("Invalid Input");
check = true;
} else {
nameEditText.setError(null);
check = false;
}
if (date.isEmpty()) {
dateEditText.setError("Invalid Input");
check = true;
} else {
dateEditText.setError(null);
check = false;
}
return check;
}
if (!checkEmptyFields())
{
//Add Data to DB
}
When I click on a ListItem, it opens up a custom dialog with 4 EditText fields. The fields are set with current data depending on the row that is clicked. The purpose of the dialog is to allow the user to update the data (it is a financial app). I am having trouble actually applying the update when the user clicks "submit" in the dialog. There are no errors in the app when I run. Here is the onclick method:
protected void onListItemClick(ListView l, View v, int position, long id) {
List<Debt> values = datasource.getAllDebt();
Debt item = values.get(position);
final long boxId = item.getId();
// final String BoxId = String.valueOf(boxId);
final String BoxName = item.getName();
final String BoxBalance = item.getBalance();
final String BoxApr = item.getApr();
final String BoxPayment = item.getPayment();
// set up dialog
final Dialog dialog = new Dialog(manageDebts.this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Edit Debt Details");
dialog.setCancelable(true);
// set up text
EditText et1 = (EditText) dialog.findViewById(R.id.editText1);
EditText et2 = (EditText) dialog.findViewById(R.id.editText2);
EditText et3 = (EditText) dialog.findViewById(R.id.editText3);
EditText et4 = (EditText) dialog.findViewById(R.id.editText4);
et1.setText(BoxName);
et2.setText(BoxBalance);
et3.setText(BoxApr);
et4.setText(BoxPayment);
// set up button
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
datasource.updateDebt(boxId, BoxName, BoxBalance, BoxApr,
BoxPayment);
dialog.dismiss();
}
});
dialog.show();
}
The Update Method in my Database helper class is shown here:
public boolean updateDebt(long updateId, String debt_name, String debt_total, String apr, String payment)
{
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_ID, updateId);
values.put(MySQLiteHelper.COLUMN_DEBT_NAME, debt_name);
values.put(MySQLiteHelper.COLUMN_DEBT_TOTAL, debt_total);
values.put(MySQLiteHelper.COLUMN_APR, apr);
values.put(MySQLiteHelper.COLUMN_PAYMENT, payment);
return database.update(MySQLiteHelper.TABLE_DEBT, values, MySQLiteHelper.COLUMN_ID + " = " + updateId, null) > 0;
}
I have verified that the COLUMN_ID and the updateId are pointing to the correct rows in the ListView and the SQL database.
Does someone see something I am not?
Perhaps you are violating a constraint with your update? Just a guess without seeing the DB code.
EDIT
Lose the single quotes around the row id variable, that is making the DB treat it as a string, and a string compared to a number is a fail.
This will work better:
String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
String[] whereArgs = new String[]{ String.valueOf(updateId) };
return database.update(MySQLiteHelper.TABLE_DEBT,
values, whereClause, whereArgs) > 0;
The String.valueOf() call simply converts the ID to a String value.
I test your code in my computer.
The update method works fine.
So I think you should post more code .
or You should check your logic.
I was missing a step between 2 and 3.
Setting the variable again, inside the onClick method. Once the user pressed the update button, the variable had to be RE-SET to the new value of the EditText field. Like this (simplified version):
String name = null;
EditText et;
name = debt.getName();
et.setText(name);
onclick {
***name = et.getText().toString();***
datasource.updateDebt(name);
}
I have a database with some records in and i have the code i wish to execute on each row but I'm having trouble creating a suitable loop, ive been trying while(movetonext) but it hasnt been working.
cursor = getAppts();
cursor.moveToNext();
String titlefromdb = cursor.getString(3);
if (strTitle.equals(titlefromdb) && cursor.getString(1).equals(dateselforap))
{
// matching code update box
final Dialog matchdiag = new Dialog(CW2Organisor.this);
matchdiag.setContentView(R.layout.apptmatch);
matchdiag.setTitle("View/Edit Appointment");
matchdiag.setCancelable(true);
TextView matchtxt = (TextView) matchdiag.findViewById(R.id.matchtxt);
matchtxt.setText("Appointment \"" + titlefromdb + "\" already exists, please choose a different event title");
Button btnmatchok = (Button) matchdiag.findViewById(R.id.btnmatch);
btnmatchok.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
matchdiag.dismiss();
}
});
matchdiag.show();
}
else
{
addAppt(strTime, strTitle, strDet);
dialog.dismiss();
}
What I would need is for each row of my database i would need the titlefromdb to hold the title field of the current row and the for the if statement to run and then move to the next row.
You could try
cursor.moveToFirst();
loop with some sort of check
cursor.moveToNext();
end loop
... and I would also try to qualify your "it's not working" statement. What's not working?
EDIT <<<<<<<<<
I have discovered that the userName retrieved from below is coming back as null. Anyone tell me why?
public void getUserName() {
getUserNameDialog = new Dialog(MasterMindGameActivity.this);
getUserNameDialog.setContentView(R.layout.retrieveusername);
getUserNameDialog.setTitle("Enter your Name");
getUserNameDialog.setCancelable(false);
TextView viewScore = (TextView) getUserNameDialog
.findViewById(R.id.showScore);
viewScore.setText("Your score was " + numberOfGuesses);
final EditText enterName = (EditText) getUserNameDialog
.findViewById(R.id.userName);
btnConfirm = (Button) getUserNameDialog
.findViewById(R.id.confirmUserName);
btnConfirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
userName = enterName.getText().toString();
getUserNameDialog.cancel();
}
});
getUserNameDialog.show();
}
displays something like
Highscore
1: null - [correct score]
Where [correct score] is the output it should have
Why is the userName not being saved and instead repalced with null?
EDIT 1: FIXED THE DOUBLE SAVE PROBLEM - NOW JUST THE NULL VALUE
You have two insert statements, that is why it is storing twice.
db.insert(HIGHSCORE_TABLE, null, value);
long dbcheck = db.insert(HIGHSCORE_TABLE, null, value);
Why username is null, you didn't assign getUserName() to userName (see my code below). Safe side I would put System.out right before inserting the data and make sure userName is really getting there.
public void saveHighscore() {
scoreSaved = true;
userName=getUserName();
DatabaseOpenHelper database = new DatabaseOpenHelper(this);
database.addHighscore(userName, numberOfGuesses);
database.close();
}
I'm using an AutoCompleteText for searching a location which would be connected to the database and the result would be shown. everything works fine when the inputed text is what is on the autocomplete list. But when i tried to input with a different text, which isnt available on the database, the application will get a force close.
here is my code
DataSPBU helper = new DataSPBU(this);
database = helper.getWritableDatabase();
Cursor dbCursor = database.query(TABLE_NAME, new String[] {SPBU, Alamat, JenisBensin, FasilitasUmum}, Alamat + "=?",new String[] {lokasi}, null, null, null);
if(dbCursor.moveToPosition(0)) {
String namaSpbu = dbCursor.getString(0);
String alamatSpbu = dbCursor.getString(1);
String jenisSpbu = dbCursor.getString(2);
String fasilitasSpbu = dbCursor.getString(3);
namaSpbuEdit.setText(namaSpbu);
alamatEdit.setText(alamatSpbu);
jenisBensinEdit.setText(jenisSpbu);
fasilitasEdit.setText(fasilitasSpbu);
}
else {
notFoundDialog = new AlertDialog.Builder(this)
.setTitle("RESULT NOT FOUND")
.setMessage("Hasil Tidak Ditemukan")
.setNegativeButton("close", new AlertDialog.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.create();
notFoundDialog.show();
}
//
any help would be highly appreciated.
Hey man when ever you enter a new text first enter into database after fetch the data,
then the application does not force close .and the data also shown in autocomplete text view
code::
String s=Edittext.getText().toString();
db.insert(s);
after bind the data with your resource