I've been struggling with this for a bit now and I would like to see
if anyone could point me in the right direction. First off, I'm
developing this with 1.5 as a target so I will be using some
depreciated methods. I have been working through the book "Android
Apps for Absolute Beginners" not because I don't know how to program,
but because it's the only book I could get my hands on. Anyhow, when I
got to chapter 10, the one where they start talking about connecting
to a database to quere, add, update, and delete records, I decide to
get creative.
Right now, I have a basic ui conisiting of four buttons; query
(displays all the contacts in toast messages), add (puts a contact
into the database with the name in the "contactName" edit text and
phone number in the "contactPhone" edit text), update (takes the name
of a contact from the "contactName" edit text and attempts to update
the number when a contact is found with that name), and delete (finds
a contact with the name from the "contactName" editText and deletes
the record).
So far, I have query and add working properly. However, my update
button does not work and throws an error. I've tried a few different,
what I thought was, creative solutions. So far I have nothing.
I'll post my latest attempt to get this to work and any feedback would
be greatly appreciated.
<code>
package content.providers;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.telephony.PhoneNumberUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.net.Uri;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.content.ContentUris;
import android.content.ContentValues;
public class DatabaseProviders extends Activity
{
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState)
{
// make instance of database providers and call main
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create instances of the phone objects
// buttons
Button queryButton = (Button)findViewById(R.id.queryButton);
Button addContactButton =
(Button)findViewById(R.id.addContactButton);
Button updateContactButton =
(Button)findViewById(R.id.updateContactButton);
Button deleteContactButton =
(Button)findViewById(R.id.deleteContactButton);
// edit text
final EditText contactName =
(EditText)findViewById(R.id.contactName);
final EditText contactPhone =
(EditText)findViewById(R.id.contactPhone);
// on queryButton click
queryButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
queryContacts();
}
});
// on addContactButton click
addContactButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
// format the number to be proper NANP
PhoneNumberUtils.formatNanpNumber(contactPhone.getEditableText());
addContact(contactName.getText().toString(),
contactPhone.getText().toString());
}
});
// on updateContactButton click
updateContactButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
// format the number to be proper NANP
PhoneNumberUtils.formatNanpNumber(contactPhone.getEditableText());
updateContact(contactName.getText().toString(),
contactPhone.getText().toString());
}
});
// on deleteContactButton click
deleteContactButton.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
deleteContact(contactName.getText().toString());
}
});
}
protected void queryContacts()
{
// load projection and base uri for people table in contact
database
String[] cols = new String[] {People.NAME, People.NUMBER};
// run query
Cursor mqCur =
managedQuery(People.CONTENT_URI,cols,null,null,null);
// move to first record if database isn't empty
if (mqCur.moveToFirst())
{
// Declare values to hold field values
String myname = null;
String mynumber = null;
do
{
// get field values
myname =
mqCur.getString(mqCur.getColumnIndex(People.NAME));
mynumber =
mqCur.getString(mqCur.getColumnIndex(People.NUMBER));
// show the record
Toast.makeText(this, myname + " " + mynumber,
Toast.LENGTH_SHORT).show();
} while (mqCur.moveToNext()); // repeat loop if more
records exist
}
}
protected void addContact(String newName, String newPhone)
{
// declare ContentValues and ContentUri
ContentValues values = new ContentValues();
Uri phoneUri = null;
// Add contactName to the ContentValues then add ContentValues
to
the database
values.put(People.NAME, newName);
Uri contentUri = getContentResolver().insert(People.CONTENT_URI,
values);
/** Add a phone number for the Contact. Begin with the URI for
the new record just returned by insert(); it ends with
the _ID
of the new record, so we don't have to add the ID
ourselves.
Then append the designation for the phone table to this
URI,
and use the resulting URI to insert the phone number. */
phoneUri = Uri.withAppendedPath(contentUri,
People.Phones.CONTENT_DIRECTORY);
// clear ContentValues then Add contactNumber to the
ContentValues
values.clear();
values.put(People.Phones.TYPE, People.TYPE_MOBILE);
values.put(People.Phones.NUMBER, newPhone);
// add content values to the database
getContentResolver().insert(phoneUri, values);
// display success and contact
Toast.makeText(this, "Success! Added Record: " + newName + " " +
newPhone, Toast.LENGTH_SHORT).show();
}
protected void updateContact(String contactName, String newPhone)
{
// Declare columns to return
String[] cols = new String[] {People._ID, People.NAME,
People.NUMBER};
// create an sql string then run sql query
String sqlQuery = "Name = '" + contactName + "'";
Cursor mqCur =
managedQuery(People.CONTENT_URI,cols,sqlQuery,null,null);
// move to first record if database isn't empty
if (mqCur.moveToFirst())
{
// declare ContentValues and ContentUri
ContentValues values = new ContentValues();
// Declare values to hold field values
int myID = 0;
do
{
// get field values
myID =
Integer.parseInt(mqCur.getString(mqCur.getColumnIndex(People._ID)));
// create a phoneUri which contains the proper
_ID for the record
we wish to change
Uri myPerson =
ContentUris.withAppendedId(People.CONTENT_URI,
myID);
// clear ContentValues then Add contactNumber
to the ContentValues
values.clear();
values.put(People.Phones.TYPE,
People.TYPE_MOBILE);
values.put(People.Phones.NUMBER, newPhone);
// update the record
getContentResolver().update(myPerson, values,
null, null);
// show the record
Toast.makeText(this, "Updated Record: " + myID,
Toast.LENGTH_SHORT).show();
Toast.makeText(this, contactName + " " +
newPhone,
Toast.LENGTH_SHORT).show();
} while (mqCur.moveToNext()); // repeat loop if more
records exist
}
else
{
// no records found
Toast.makeText(this, "No records found",
Toast.LENGTH_SHORT).show();
}
}
protected void deleteContact(String contactName)
{
// TODO Auto-generated method stub
}
}
</code>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en