I'm extremely beginner to android development, I'm making a simple app that has a registration form, but after the fields filled with data and when the save button pressed the app closed immediately.
I've included the code because I don't know where's the faulty part:
public void AddNew(View v)
{
if(Image_path.isEmpty())
{
Toast.makeText(this,"please choose a profile picture",Toast.LENGTH_SHORT).show();
return;
}
EditText ed_full_name = findViewById(R.id.full_name);
EditText ed_mobie = findViewById(R.id.phone);
EditText ed_id_num = findViewById(R.id.id_num);
EditText ed_address = findViewById(R.id.address);
EditText ed_section_id = findViewById(R.id.straction_id);
EditText ed_card_id = findViewById(R.id.card_id);
EditText ed_dis_id = findViewById(R.id.dis_id);
String full_name = ed_full_name.getText().toString();
String mobile = ed_mobie.getText().toString();
String id_num = ed_id_num.getText().toString();
String address = ed_address.getText().toString();
String card_id = ed_card_id.getText().toString();
String section_id = ed_section_id.getText().toString();
String dis_id = ed_dis_id.getText().toString();
if(full_name.isEmpty() || mobile.isEmpty() || id_num.isEmpty() || address.isEmpty() || card_id.isEmpty() || section_id.isEmpty())
{
Toast.makeText(this,"Please fill all fields",Toast.LENGTH_SHORT).show();
return;
}
DBHelper mHelper = new DBHelper(this);
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("full_name", full_name);
values.put("mobile", mobile);
values.put("id_num", id_num);
values.put("address", address);
values.put("section_id", section_id);
values.put("card_id", card_id);
values.put("image_path", Image_path);
values.put("dis_id", dis_id);
db.insertWithOnConflict("Clients",
null,
values,
SQLiteDatabase.CONFLICT_REPLACE);
db.close();
Toast.makeText(this, "Saved successfully! ", Toast.LENGTH_SHORT).show();
finish();
You are calling finish(); on your Activity, that will cause it to close. If it is the only Activity on your App's backstack, the App will also close.
Related
I am trying to do an app where you have an account and you have to login to use the app. So obviously you need to register for the app. The problem is when I enter the sign up form and enter all the details, the data doesn't go into the database. Any help?
Reference used: https://www.youtube.com/watch?v=KxlLsk5j3rY
This is the method to insert the contact into the database.
public void insertContact(Account a)
{
ContentValues values = new ContentValues();
String query = "select * from users";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_FirstName, a.getFirstname());
values.put(COLUMN_LastName, a.getLastname());
values.put(COLUMN_Username, a.getUsername());
values.put(COLUMN_Password, a.getPassword());
values.put(COLUMN_Email, a.getEmail());
values.put(COLUMN_ClubName, a.getEmail());
values.put(COLUMN_Address, a.getAddress());
db.insert(TABLE_NAME, null, values);
Log.e("Contact Entered", "DATABASE");
db.close();
}
The Register Button event
public void onRegisterClick(View v)
{
if(v.getId() == R.id.btnregister) {
EditText name = (EditText) findViewById(R.id.txtname);
EditText lastname = (EditText) findViewById(R.id.txtlastname);
EditText username = (EditText) findViewById(R.id.txtusername);
EditText password = (EditText) findViewById(R.id.txtpassword);
EditText email = (EditText) findViewById(R.id.txtemail);
EditText clubname = (EditText) findViewById(R.id.txtclubname);
EditText address = (EditText) findViewById(R.id.txtaddress);
//Text entered in the textview is assigned to a variable and to string
String namestr = name.getText().toString();
String lastnamestr = lastname.getText().toString();
String userstr = username.getText().toString();
String passstr = password.getText().toString();
String emailstr = email.getText().toString();
String clubnamestr = clubname.getText().toString();
String addresssrt = address.getText().toString();
Account a = new Account();
a.setFirstname(namestr);
a.setLastname(lastnamestr);
a.setUsername(userstr);
a.setPassword(passstr);
a.setEmail(emailstr);
a.setClubname(clubnamestr);
a.setAddress(addresssrt);
datab.insertContact(a);
Log.e("insertContact(a)", "DATABASE");
Toast temp = Toast.makeText(CreateAccount.this, "Account created!", Toast.LENGTH_SHORT);
temp.show();
Intent i = new Intent(CreateAccount.this, MainActivity.class);
startActivity(i);
}
}
You can simply use SuperDatabase to control your database with pure SQL. I've created a library to make things easier on Android. Import library to Gradle and use SuperDatabase.
Documentations here : https://github.com/sangeethnandakumar/TestTube
This worked for me. Adding BeginTransaction() and SetTransactionSuccessful helped.
public void insertContact(Account a)
{
ContentValues values = new ContentValues();
db.beginTransaction();
String query = "select * from users";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID,count);
values.put(COLUMN_FirstName, a.getFirstname());
values.put(COLUMN_LastName, a.getLastname());
values.put(COLUMN_Username, a.getUsername());
values.put(COLUMN_Password, a.getPassword());
values.put(COLUMN_Email, a.getEmail());
values.put(COLUMN_ClubName, a.getEmail());
values.put(COLUMN_Address, a.getAddress());
db.insert(TABLE_NAME, null, values);
Log.e("Contact Entered", "DATABASE");
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
This question already has an answer here:
What does "Incompatible types: void cannot be converted to ..." mean?
(1 answer)
Closed 4 years ago.
Im trying to create a simple login for my mobile app but im getiing stock with an error :
Login.java
public void onButtonClick(View v) {
if (v.getId() == R.id.BLogin) {
EditText a = (EditText) findViewById(R.id.TFusername);
String str = a.getText().toString();
EditText b = (EditText) findViewById(R.id.TFpassword);
String pass = b.getText().toString();
String password = helper.searchPass(str);
if (pass.equals(password)) {
Intent i = new Intent(LogIn.this, Display.class);
i.putExtra("Username", str);
startActivity(i);
} else {
Toast temp = Toast.makeText(LogIn.this, "Username and password don't match!", Toast.LENGTH_SHORT);
temp.show();
}
}
if (v.getId() == R.id.BSignup) {
Intent i = new Intent(LogIn.this, Signup.class);
startActivity(i);
}
}
and the DatabaseHandler
public void searchPass(String uname)
{
db = this.getReadableDatabase();
String query = " select uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do {
a = cursor.getString(0);
if(a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}
Im getting stuck with the error:
Error:(32, 48) error: incompatible types: void cannot be converted to String
String password = helper.searchPass(str);
anyone know what im missing?
Replace return type as String in your method searchPass
public String searchPass(String uname)
{
db = this.getReadableDatabase();
String query = " select uname, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do {
a = cursor.getString(0);
if(a.equals(uname))
{
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
return b;
}
I had some problem about get data from sqlite different database.One database that save user's name and phone and they can update as they like. While another is house information but also contain user's name and phone.
My problem is i just realize that the phone that had been update in user database is not same with phone that previous store in house database. So i wanna call the user's phone from user database and use for phone call function and message function. But it does not work.And i trying to match the fullname that in user database with house database to call the phone data but still cant.
here is my call and message function
public class HouseDetail extends AppCompatActivity {
EditText etAddress,etDetail,etPhone;
TextView txType,txProperty,txPrice,txState,txTitle,txOther,txSize,txFullname;
ImageButton bPhone,bMessage;
String type,property,price,state,address,title,other,size,detail,fullnames,fullname,phone;
HousesDB db = new HousesDB(this);
Houses houses;
DatabaseOperations Udb = new DatabaseOperations(this);
PersonalData profileInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_house_detail);
txType = (TextView) findViewById(R.id.txTypes);
txProperty = (TextView) findViewById(R.id.txProperty);
txPrice = (TextView) findViewById(R.id.txPrice);
txState = (TextView) findViewById(R.id.txState);
etAddress = (EditText) findViewById(R.id.etAddress);
txTitle = (TextView) findViewById(R.id.txTitle);
txOther = (TextView) findViewById(R.id.txOther);
txSize = (TextView) findViewById(R.id.txSize);
txFullname = (TextView) findViewById(R.id.txFullname);
etPhone = (EditText) findViewById(R.id.etPhone);
etDetail = (EditText) findViewById(R.id.etDetail);
bPhone = (ImageButton) findViewById(R.id.bPhone);
bMessage = (ImageButton) findViewById(R.id.bMessage);
fullnames = txFullname.getText().toString();
type = txType.getText().toString();
property = txProperty.getText().toString();
price = txPrice.getText().toString();
state = txState.getText().toString();
address = etAddress.getText().toString();
title = txTitle.getText().toString();
other = txOther.getText().toString();
size = txSize.getText().toString();
detail = etDetail.getText().toString();
phone = etPhone.getText().toString();
Intent i = getIntent();
property = i.getStringExtra("house_property");
txProperty.setText(property);
houses = db.getInfo(property);
txType.setText(houses.getTypes());
txFullname.setText(houses.getFullname());
txPrice.setText(houses.getPrice());
txState.setText(houses.getState());
etAddress.setText(houses.getAddress());
txTitle.setText(houses.getTitle());
txOther.setText(houses.getOther());
txSize.setText(houses.getSize());
etDetail.setText(houses.getDetail());
this.fullnames = fullname;
profileInfo = Udb.getNumber(fullname);
etPhone.setText(profileInfo.get_phone());
bPhone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + etPhone));
startActivity(callIntent);
}
});
bMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + etPhone)));
}
});
}
Here is my user database that get the phone number
public PersonalData getNumber(String fullname)
{
SQLiteDatabase SQ = this.getWritableDatabase();
String query = "select FullName,Phone from "+ Table_NAME;
Cursor CR = SQ.rawQuery(query,null);
String a;
PersonalData info = new PersonalData();
if (CR.moveToFirst())
{
do {
a = CR.getString(0);
if (a.equals(fullname)) {
info._phone = CR.getString(1);
break;
}
}while (CR.moveToNext());
}
return info;
}
try this:
public PersonalData getNumber(String fullName) {
SQLiteDatabase database = this.getReadableDatabase();
String table = "YOUR TABLE NAME";
String[] columns = {
"FullName" ,
"Phone"
};
Cursor cursor = database.query(table , columns , null , null , null , null , null);
cursor.moveToPosition(-1);
PersonalData info = new PersonalData();
while (cursor.moveToNext()) {
String name = cursor.getString(0);
if (name.equals(fullName)) {
info._phone = cursor.getString(1);
break;
}
}
cursor.close();
return info;
}
Replace
a = CR.getString(0);
if (a.equals(fullname)) {
info._phone = CR.getString(1);
break;
}
with
a = CR.getString(CR.getColumnIndex("Fullname"));
if (a.equals("FullName")) {
info._phone = CR.getString(CR.getColumnIndex("Phone"));
break;
}
your data gotten by query maybe not be ordered as you want,in your case that is, the result maybe not in "Fullname,Phone",so you should use getColumnIndex() to get data for each column.
I really don't know what is happening to my code. At first, i was able to insert record to my table, then in my second try it just don't run. Please Help. I passed the username through intent which pretty works.
First
String pass = txtpass.getText().toString();
String repass = txtpass2.getText().toString();
String secA = txtans.getText().toString();
String secQ = txtQ.getSelectedItem().toString();
Message.message(this, pass+"and"+repass);
if(pass.equals(repass))
{
long id = mh.insertNewAccount(username, pass, secQ, secA);
if(id < 0)
Message.message(this, "Unsuccesful");
else
{
Message.message(this, "You may now logged in! Enjoy your experience!");
Intent intent = new Intent(this, MainScreen.class);
Bundle bnd=new Bundle();
bnd.putString("username", username);
intent.putExtras(bnd);
txtpass.setText("");
txtpass2.setText("");
txtans.setText("");
startActivity(intent);
}
}
else
{
Message.message_short(this, "Password didn't match!");
txtpass.setText("");
txtpass2.setText("");
}
}
Second - insertNewAccount() Function
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(MyHelper.USER_ID, "");
contentValues.put(MyHelper.USER_USERNAME, name);
contentValues.put(MyHelper.USER_PASSWORD, pass);
contentValues.put(MyHelper.USER_SECANS, secA);
contentValues.put(MyHelper.USER_SECQUE, secQ);
long id = db.insert(MyHelper.TABLE_USERS, null, contentValues);
db.close();
return id;
I need to make my app such that, only when the user click on the re_b1 button, it should show the progress Dialog until the Button's progress finished (because sometimes this activity freezes until the progress finished). Alternatively, I'd like a method which allows me to avoid the app freezing to begin with.
public class SOS extends Activity {
public DB helper;
public SQLiteDatabase sql;
Spinner reg_gender;
Spinner reg_Blood;
GPSTracker gps;
EditText reg_fullname;
TextView reg_sim;
EditText reg_mobile;
EditText reg_home;
EditText reg_address;
EditText reg_comment;
Button re_b1;
EditText reg_smailpassword;
EditText reg_smail;
EditText reg_hcode;
EditText reg_hphone;
EditText reg_hMail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sos);
helper=new DB(this);
sql= helper.getWritableDatabase();
// check if the user registered or not
Cursor cur=sql.query("users", null, null, null, null, null, null);
if(cur.moveToNext()){
sql.close();
Intent call = new Intent(SOS.this, MainSos.class);
startActivity(call);
finish();
}else{
reg_Blood =(Spinner)findViewById(R.id.reg_Blood);
reg_gender =(Spinner)findViewById(R.id.reg_gender);
ArrayAdapter<CharSequence> gender1 = ArrayAdapter.createFromResource(this, R.array.gender, android.R.layout.simple_spinner_dropdown_item);
reg_gender.setAdapter(gender1);
ArrayAdapter<CharSequence> blood1 = ArrayAdapter.createFromResource(this, R.array.blood, android.R.layout.simple_spinner_dropdown_item);
reg_Blood.setAdapter(blood1);
reg_mobile =(EditText)findViewById(R.id.reg_mobile);
reg_fullname =(EditText)findViewById(R.id.reg_fullname);
reg_sim =(TextView)findViewById(R.id.reg_sim);
TelephonyManager myt;
myt = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String mob = myt.getSimSerialNumber().toString();
reg_sim.setText(mob);
reg_home =(EditText)findViewById(R.id.reg_home);
reg_address =(EditText)findViewById(R.id.reg_address);
reg_comment =(EditText)findViewById(R.id.reg_comment);
reg_smailpassword = (EditText)findViewById(R.id.reg_smailpassword);
reg_smail = (EditText)findViewById(R.id.reg_smail);
reg_hcode = (EditText)findViewById(R.id.reg_hcode);
reg_hphone = (EditText)findViewById(R.id.reg_hphone);
reg_hMail = (EditText)findViewById(R.id.reg_hMail);
reg_smailpassword.setText("administrator#!~");
reg_smail.setText("ahmedelbadry1982#gmail.com");
re_b1 =(Button)findViewById(R.id.re_b1);
re_b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = reg_fullname.getText().toString();
String simn = reg_sim.getText().toString();
String mobile = reg_mobile.getText().toString();
String home = reg_home.getText().toString();
String address = reg_address.getText().toString();
String comment = reg_comment.getText().toString();
String smailpassword = reg_smailpassword.getText().toString();
String smail = reg_smail.getText().toString();
String hcode = reg_hcode.getText().toString();
String hphone = reg_hphone.getText().toString();
String hMail = reg_hMail.getText().toString();
// EditText validation
if (hMail.length() <= 0 || hphone.length() <= 0 || hcode.length() <= 0 || smail.length() <= 0|| smailpassword.length() <= 0 || name.length() <= 0 || simn.length() <= 0 || mobile.length() <= 0 || home.length() <= 0 || address.length() <= 0 ){
Toast.makeText(SOS.this, "All field are required", Toast.LENGTH_LONG).show();
} else {
// insert new user in our table "users" in DB
long blo = reg_Blood.getSelectedItemId();
long gen = reg_gender.getSelectedItemId();
String blo1 = reg_Blood.getSelectedItem().toString();
String gen1 = reg_gender.getSelectedItem().toString();
ContentValues cv=new ContentValues();
cv.put("full_name", name);
cv.put("gender", gen);
cv.put("blood_type", blo);
cv.put("simno", simn);
cv.put("mobile", mobile);
cv.put("home", home);
cv.put("address", address);
if (comment.length() > 0 ) cv.put("comment", comment);
cv.put("email", smail);
cv.put("email_pass", smailpassword);
cv.put("code", hcode);
cv.put("phone_numb", hphone);
cv.put("email2", hMail);
sql.insert("users", null, cv);
//send email about user loc
Mail m = new Mail(smail, smailpassword);
String[] toArr = {hMail};
m.setTo(toArr);
m.setFrom(smail);
m.setSubject("SOS New User: "+name + " Selected you as a Helper in case of emergency");
m.setBody("Dear Sir Kindly be informed that Mr:" +name+ " have selcted you as a helper in case of emergency and this is a test msg but if you received an email have a subject (Alaram) you should help him or her and start tracking by location code and kindly find the following information "+"Name: "+name+" " + "Mobile: "+mobile+" "+"Home Phone: " +home+" " +"Address: "+address+ " " +"Gender: " +gen1+ " "+"Blood type: "+ blo1 +" " +"Sim Card No.: "+ simn+ " "+ "Comment: "+comment);
try {
// m.addAttachment("/sdcard/filelocation");
if(m.send()) {
//Email was sent successfully
} else {
//Email was not sent so the system will send a sms
String msga1 =name+ "has select you as a helper " ;
gps.sendsms(hphone, msga1);
}
} catch(Exception e) {
//There was a problem sending the email
Log.e("MailApp", "Could not send email", e);
}
Toast.makeText(SOS.this, "Registration is done", Toast.LENGTH_LONG).show();
Intent call = new Intent(SOS.this, MainSos.class);
startActivity(call);
startService(new Intent(SOS.this ,SosSms.class));
finish();
}
}
});
}
}
}
Here is a good example of using progress dialog Normally you would use this in an AsyncTask but I don't see any network stuff going on so runOnUiThread may work for you. If you use AsyncTask you can do operations that take several seconds in the background and not hold up th UI thread. Hope this helps