sree sree - 6 days ago 6
Android Question

contacts application using android

Contacts() class:
public class Contacts {

int _id;
String _name;
String _mobile;
String _home;
String _office;
String _email;
String _companyName;
String _jobRole;
String _address;


public Contacts( String name, String mobile, String home, String office, String address, String email,String companyName, String jobRole ) {

this._name = name;
this._mobile = mobile;
this._home = home;
this._office = office;
this._address = address;
this._email = email;
this._companyName = companyName;
this._jobRole = jobRole;

}
public Contacts(int id, String name, String mobile, String home, String office,String address,String email,String companyName, String jobRole ) {

this._id = id;
this._name = name;
this._mobile = mobile;
this._home = home;
this._office = office;
this._address = address;
this._email = email;
this._companyName = companyName;
this._jobRole = jobRole;

}

public Contacts() {

}


public String getCompanyName() {
return _companyName;
}

public void setCompanyName(String companyName) {
this._companyName = companyName;
}

public String getEmail() {
return _email;
}

public void setEmail(String email) {
this._email = email;
}

public String getHome() {
return _home;
}

public void setHome(String home) {
this._home = home;
}

public int getId() {
return _id;
}

public void setId(int id) {
this._id = id;
}

public String getJobRole() {
return _jobRole;
}

public void setJobRole(String jobRole) {
this._jobRole = jobRole;
}

public String getMobile() {
return _mobile;
}

public void setMobile(String mobile) {
this._mobile = mobile;
}

public String getName() {
return _name;
}

public void setName(String name) {
this._name = name;
}

public String getOffice() {
return _office;
}

public void setOffice(String office) {
this._office = office;
}

public String getAddress() {

return _address;
}

public void setAddress(String address) {
this._address = address;
}


}

Example4() class:
public class Example4 extends AppCompatActivity{

DbHandler dbHandler;
ListView listView;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example4);



dbHandler = new DbHandler(this);
List<Contacts> listContacts = dbHandler.getAllContacts();
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,listContacts);

listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Bundle bundle = new Bundle();
int id_search = i ;
bundle.putInt("id",id_search);

Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
intent.putExtras(bundle);
startActivity(intent);

}
});






Log.d("Insert: ", "Inserting ..");
dbHandler.addContact(new Contacts("Ravi", "9100000000","044-123456","","","r@gmail.com","qbrik","developer"));
dbHandler.addContact(new Contacts("Srinivas", "9199999999","044-123456","","","r@gmail.com","qbrik","developer"));
dbHandler.addContact(new Contacts("Tommy", "9522222222","044-123456","","","r@gmail.com","qbrik","developer"));
dbHandler.addContact(new Contacts("Karthik", "9533333333","044-123456","","","r@gmail.com","qbrik","developer"));

Log.d("Reading: ", "Reading all contacts..");

for (Contacts cn : listContacts) {
String log = "Id: "+cn.getId()+" ,Name: " + cn.getName() + " ,Mobile: " + cn.getMobile() + " ,Home: " + cn.getHome() + " ,Office: " + cn.getOffice() + " ,Email: " + cn.getEmail() + " ,Company Name: " + cn.getCompanyName() + " ,Job Role: " + cn.getJobRole();
Log.d("Name: ", log);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.item1:Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);

Intent intent = new Intent(getApplicationContext(),DisplayContacts.class);
intent.putExtras(dataBundle);

startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}

}


}

DisplayContacts() class:

public class DisplayContacts extends Activity {

TextView name;
TextView mobile;
TextView home;
TextView office;
TextView companyName;
TextView jobRole;
TextView email;
TextView address;
private DbHandler dbHandler;
private int id_update =0;
private Contacts contacts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example4_display);

name = (TextView) findViewById(R.id.editName);
mobile = (TextView) findViewById(R.id.editTextMobile);
home = (TextView) findViewById(R.id.editTextHome);
office = (TextView) findViewById(R.id.editTextOffice);
companyName = (TextView) findViewById(R.id.editTextComapanyName);
jobRole = (TextView) findViewById(R.id.editTextJobRole);
email = (TextView) findViewById(R.id.editEmail);
address = (TextView) findViewById(R.id.editAddress);

dbHandler = new DbHandler(this);
contacts = new Contacts();
Bundle bundle = getIntent().getExtras();
if(bundle != null) {

int value = bundle.getInt("id");
if(value > 0) {

Cursor cs = (Cursor) dbHandler.getContact(value);
id_update = value;
cs.moveToFirst();


String nam = cs.getString(cs.getColumnIndex(DbHandler.KEY_NAME));
String mob = cs.getString(cs.getColumnIndex(DbHandler.KEY_MOBILE));
String hom = cs.getString(cs.getColumnIndex(DbHandler.KEY_HOME));
String off = cs.getString(cs.getColumnIndex(DbHandler.KEY_OFFICE));
String com = cs.getString(cs.getColumnIndex(DbHandler.KEY_COMPANYNAME));
String job = cs.getString(cs.getColumnIndex(DbHandler.KEY_JOBROLE));
String emai = cs.getString(cs.getColumnIndex(DbHandler.KEY_EMAIL));
String addr = cs.getString(cs.getColumnIndex(DbHandler.KEY_ADDRESS));

if(!cs.isClosed()) {
cs.close();

Button b = (Button) findViewById(R.id.button1);
b.setVisibility(View.INVISIBLE);


name.setText(nam);
name.setFocusable(false);
name.setClickable(false);

mobile.setText(mob);
mobile.setFocusable(false);
mobile.setClickable(false);

home.setText(hom);
home.setFocusable(false);
home.setClickable(false);

office.setText(off);
office.setFocusable(false);
office.setClickable(false);

companyName.setText(com);
companyName.setFocusable(false);
companyName.setClickable(false);

jobRole.setText(job);
jobRole.setFocusable(false);
jobRole.setClickable(false);

email.setText(emai);
email.setFocusable(false);
email.setClickable(false);

address.setText(addr);
address.setFocusable(false);
address.setClickable(false);
}


}

}






}

public void run(View view) {

Bundle bundle = getIntent().getExtras();
if(bundle != null) {

int value =bundle.getInt("id");
if(value > 0) {
if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {

Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(),Example4.class);
startActivity(intent);
} else{
Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show();
}
} else{
if(dbHandler.addContact(new Contacts(name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {
Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(getApplicationContext(),Example4.class);
startActivity(intent);
}
}


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

Bundle bundle = getIntent().getExtras();
if(bundle !=null) {
int value = bundle.getInt("id");
if(value > 0) {
getMenuInflater().inflate(R.menu.display_contact,menu);
}else {
getMenuInflater().inflate(R.menu.menu_main, menu);
}
}

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);

switch(item.getItemId()) {
case R.id.Edit_Contact:

Button b = (Button) findViewById(R.id.button1);
b.setVisibility(View.VISIBLE);

name.setEnabled(true);
name.setFocusableInTouchMode(true);
name.setClickable(true);

mobile.setEnabled(true);
mobile.setFocusableInTouchMode(true);
mobile.setClickable(true);

home.setEnabled(true);
home.setFocusableInTouchMode(true);
home.setClickable(true);

office.setEnabled(true);
office.setFocusableInTouchMode(true);
office.setClickable(true);

companyName.setEnabled(true);
companyName.setFocusableInTouchMode(true);
companyName.setClickable(true);

jobRole.setEnabled(true);
jobRole.setFocusableInTouchMode(true);
jobRole.setClickable(true);

email.setEnabled(true);
email.setFocusableInTouchMode(true);
email.setClickable(true);

address.setEnabled(true);
address.setFocusableInTouchMode(true);
address.setClickable(true);

return true;
case R.id.Delete_Contact:

return true;
default:
return super.onOptionsItemSelected(item);
}


}


}

DbHandler() class:

public class DbHandler extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "contact"; //database name
public static final int DATABASE_VERSION = 1; //database version
public static final String TABLE_CONTACTS = "contacts";//table name
public static final String KEY_ID = "id"; //column names for contacts table
public static final String KEY_NAME = "name";
public static final String KEY_MOBILE = "mobile";
public static final String KEY_HOME = "home";
public static final String KEY_OFFICE = "office";
public static final String KEY_ADDRESS = "address";
public static final String KEY_EMAIL = "email";
public static final String KEY_COMPANYNAME = "companyName";
public static final String KEY_JOBROLE = "jobRole";



public DbHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



//to create taables
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_MOBILE + " TEXT,"
+ KEY_HOME + " TEXT,"
+ KEY_OFFICE + " TEXT,"
+ KEY_ADDRESS + " TEXT,"
+ KEY_EMAIL + " TEXT,"
+ KEY_COMPANYNAME + " TEXT,"
+ KEY_JOBROLE + " TEXT" + ")";
sqLiteDatabase.execSQL(CREATE_CONTACTS_TABLE);

}
//upgrading database i.e., if further we add any columns to the existing table ,it will modify automatically
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

sqLiteDatabase.execSQL("DROP TABLE IF EXISTS" + TABLE_CONTACTS); //drop older table if exists
onCreate(sqLiteDatabase); //to create table again

}

public boolean addContact(Contacts contacts) {

SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME,contacts.getName());
contentValues.put(KEY_MOBILE,contacts.getMobile());
contentValues.put(KEY_HOME,contacts.getHome());
contentValues.put(KEY_OFFICE,contacts.getOffice());
contentValues.put(KEY_ADDRESS,contacts.getAddress());
contentValues.put(KEY_EMAIL,contacts.getEmail());
contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
contentValues.put(KEY_JOBROLE,contacts.getJobRole());


sqLiteDatabase.insert(TABLE_CONTACTS,null,contentValues);
sqLiteDatabase.close();
return true;
}


public Contacts getContact(int id) {

SQLiteDatabase sqLiteDatabase =this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.query(TABLE_CONTACTS,new String[] {KEY_ID,KEY_NAME,KEY_MOBILE,KEY_HOME,KEY_OFFICE,KEY_ADDRESS,KEY_EMAIL,KEY_COMPANYNAME,KEY_JOBROLE},KEY_ID + "=?",new String[] {String.valueOf(id)},null,null,null,null);
if(cursor != null)
cursor.moveToFirst();
Contacts contacts = new Contacts(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6),cursor.getString(7),cursor.getString(8));

return contacts;


}

public List<Contacts>getAllContacts() {

List<Contacts> contactsList = new ArrayList<Contacts>();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(selectQuery,null);

if (cursor.moveToFirst()){

do {

Contacts contacts = new Contacts();
contacts.setId(Integer.parseInt(cursor.getString(0)));
contacts.setName(cursor.getString(1));
contacts.setMobile(cursor.getString(2));
contacts.setHome(cursor.getString(3));
contacts.setOffice(cursor.getString(4));
contacts.setAddress(cursor.getString(5));
contacts.setEmail(cursor.getString(6));
contacts.setCompanyName(cursor.getString(7));
contacts.setJobRole(cursor.getString(8));

contactsList.add(contacts);


}while (cursor.moveToNext());

}
return contactsList;

}

public int getContactsCount() {

String countQuery = " SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery(countQuery,null);
cursor.close();

return cursor.getCount();

}

public boolean updateContact(Contacts contacts) {

SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME,contacts.getName());
contentValues.put(KEY_MOBILE,contacts.getMobile());
contentValues.put(KEY_HOME,contacts.getHome());
contentValues.put(KEY_OFFICE,contacts.getOffice());
contentValues.put(KEY_ADDRESS,contacts.getAddress());
contentValues.put(KEY_EMAIL,contacts.getEmail());
contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
contentValues.put(KEY_JOBROLE,contacts.getJobRole());
sqLiteDatabase.update(TABLE_CONTACTS,contentValues,KEY_ID + "= ?",new String[] {String.valueOf(contacts.getId())});
return true;


}

public void deleteContact(Contacts contacts) {

SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
sqLiteDatabase.delete(TABLE_CONTACTS,KEY_ID + "= ?",new String[]{String.valueOf(contacts.getId())});
sqLiteDatabase.close();

}


}

but this code is not working, can u please update the code

Answer

probably you want ....

if(dbHandler.updateContact(new Contacts(id_update,name.getText().toString(),mobile.getText().toString(),home.getText().toString(),office.getText().toString(),companyName.getText().toString(),jobRole.getText().toString(),email.getText().toString(),address.getText().toString()))) {}

You also need to change addContact/updateContact to return boolean like this:

        public Boolean addContact(Contacts contacts) {

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_NAME,contacts.getName());
        contentValues.put(KEY_MOBILE,contacts.getMobile());
        contentValues.put(KEY_HOME,contacts.getHome());
        contentValues.put(KEY_OFFICE,contacts.getOffice());
        contentValues.put(KEY_ADDRESS,contacts.getAddress());
        contentValues.put(KEY_EMAIL,contacts.getEmail());
        contentValues.put(KEY_COMPANYNAME,contacts.getCompanyName());
        contentValues.put(KEY_JOBROLE,contacts.getJobRole());


        sqLiteDatabase.insert(TABLE_CONTACTS,null,contentValues);
        sqLiteDatabase.close();

        return true;
    }