this is is the code i have but everytime i click on the contact it force closes. and is there a code so that when i get the contact it adds it into a text view?
public static final String TAG = "ContactManager";
private Button mAddAccountButton;
private ListView mContactList;
private boolean mShowInvisible;
private CheckBox mShowInvisibleControl;
/**
* Called when the activity is first created. Responsible for initializing the UI.
*/
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
// Obtain handles to UI objects
mAddAccountButton = (Button) findViewById(R.id.AddContact);
mContactList = (ListView) findViewById(R.id.ContactList);
mShowInvisibleControl = (CheckBox) findViewById(R.id.ShowInvisible);
// Initialize class properties
mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
// Register handler for UI elements
mAddAccountButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "mAddAccountButton clicked");
launchContactAdder();
}
});
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
// Populate the contact list
populateContactList();
}
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, cursor,
fields, new int[] {R.id.TextView01});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* #return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
/**
* Launches the ContactAdder activity to add a new contact to the selected account.
*/
protected void launchContactAdder() {
Intent i = new Intent(this,Class1.class);
startActivity(i);
}
}
based on my experience with the contacts list, you need to design your query based on what is available. In 1.6 there was the simplicity of one table with all the information. However; with the dawn of 2.0, they introduced two tables. Where you get the ID from one table and the query based on this ID to find the phone number. To illustrate this here is a piece of sample code that worked for me, although i'm having some minor problems where some contacts won't return a phone number 2/70 although all 70 users have an ID and Phone number. I hope it helps:
// look up contact via name
String name = contacts.getItem(arg1);
Uri lookup = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI, name);
// look up id
Cursor c = getContentResolver().query(lookup, null, null, null, null);
String id = null;
int id_index = c.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
if (c.moveToFirst())
id = c.getString(id_index);
else
Toast.makeText(getApplicationContext(), "Friend not found",
Toast.LENGTH_SHORT).show();
c.close();
// use id if not null, to find contact's phone number / display name
if (id != null) {
String where = ContactsContract.Data.CONTACT_ID + " = " + id
+ " AND " + ContactsContract.Data.MIMETYPE + " = '"
+ ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "'";
c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
null, where, null, null);
c.moveToFirst();
int iname = c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int iphone = c
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
if (c.getCount() > 0) {
_friend.setName(c.getString(iname));
_friend.setPhone(c.getString(iphone));
If you have any further questions, please don't hesitate to ask, I'll do my best to answer them. For what I can tell without a log cat is that you are attempting a look up of the phone number the proper table structure for the query. If you try to access information from a query that returned 0 rows, then you'll get an exception. Please read that error and display it.
You have to use for all Email, Phone Numbers, Web-Address etc.
Example:
Linkify.addLinks(textView, Linkify.WEB_URLS);
Parameter: textview which you are adding string
Which thing you want to track email,phone or web
For more details:
http://developer.android.com/reference/android/text/util/Linkify.html
Note: for this you no need to implement any onClick etc. Linkif automatically manage it.
Related
I'm messing around with some SQLite databases for an Android app. I have a 'player' table with several players, and a one-to-many 'skill' table which has each player's skill points, like Shooting and Rebounding.
I have one activity in the app for actually filling out textboxes and inserting a player into the database. When the user hits the 'Add Player' button, a row is inserted into the 'player' table and a row is inserted into the 'skills' table which has a foreign key that references the 'player' table. After these inserts, I did a query to check if I could read the 'Shooting' value from the 'skills' table and put it in a Toast notification. That worked fine, and the code I used is here:
SQLiteDatabase db2 = dbHelper.getReadableDatabase();
String[] projection = { "shooting" };
String sortOrder = "shooting" + " DESC";
Cursor c = db2.query(
"skills", // The table to query
projection, // The columns to return
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
sortOrder // The sort order
);
c.moveToFirst();
int shooting = c.getInt(c.getColumnIndexOrThrow("shooting"));
Toast.makeText(this, "" + shooting, Toast.LENGTH_SHORT).show();
After I saw that this was working, I commented it out and put in an Intent to make the app switch to the 'Roster' activity after the player and skills are inserted. On the 'Roster' activity, I want to get each player's 'Shooting' skill. When I use the exact same code from above (which works from the other activity) I get an error which says:
06-16 15:59:42.602: E/AndroidRuntime(31537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silverray.messaround/com.silverray.messaround.Roster}: java.lang.IllegalArgumentException: column 'shooting' does not exist
I can't figure out why it's saying the 'shooting' column doesn't exist when I know I included it in my SQL Create statement, and I was even able to read this exact same column with the same code from another activity.
Thanks for reading. Any ideas?
EDIT: This is the full code for the Roster activity:
public class Roster extends Activity {
int teamID = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roster);
// CHECK ROSTER
DatabaseContract dbContract = new DatabaseContract();
DatabaseContract.DbHelper dbHelper = dbContract.new DbHelper(this);
SQLiteDatabase dbCheck = dbHelper.getReadableDatabase();
Intent intent = getIntent();
int ID = intent.getIntExtra("ID", 1);
teamID = ID;
String stringID = String.valueOf(ID);
String[] projection = { "_id, playerFirstName, playerLastName, playerPosition" };
String sortOrder = "playerFirstName" + " ASC";
Cursor c = dbCheck.query(
"player",
projection,
null,
null,
null,
null,
sortOrder
);
c.moveToFirst();
int rowsAffected = c.getCount();
if (rowsAffected < 1) {
TextView rosterList = (TextView) findViewById(R.id.txtListRoster);
rosterList.setText("Your team doesn't have any players!");
c.close();
dbCheck.close();
} else {
String players = "";
for (int l = 0; l < rowsAffected; l++) {
String playerName = c.getString(c.getColumnIndexOrThrow("playerFirstName"));
String playerLastName = c.getString(c.getColumnIndexOrThrow("playerLastName"));
String position = c.getString(c.getColumnIndexOrThrow("playerPosition"));
int playerID = c.getInt(c.getColumnIndexOrThrow("_id"));
String player_ID = String.valueOf(playerID);
String pos = "";
if (position.equals("Point Guard")) {
pos = "PG";
} else if (position.equals("Shooting Guard")) {
pos = "SG";
} else if (position.equals("Small Forward")) {
pos = "SF";
} else if (position.equals("Power Forward")) {
pos = "PF";
} else if (position.equals("Center")) {
pos = "C";
}
SQLiteDatabase db2 = dbHelper.getReadableDatabase();
String[] projection2 = { "shooting" };
String sortOrder2 = "shooting" + " DESC";
Cursor c2 = db2.query(
"skills",
projection2,
null,
null,
null,
null,
sortOrder2
);
c2.moveToFirst();
//** Everything works until this line:
int shooting = c2.getInt(c.getColumnIndexOrThrow("shooting"));
players += playerName + " " + playerLastName + " (" + pos + ") Shooting: ";
if (l != (rowsAffected - 1)) {
players += "\n";
}
TextView rosterList = (TextView) findViewById(R.id.txtListRoster);
rosterList.setText(players);
if (l != (rowsAffected - 1)) {
c.moveToNext();
}
c2.close();
}
c.close();
dbCheck.close();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.roster, menu);
return true;
}
public void addPlayer(View view) {
Intent goToAddPlayer = new Intent(this, AddPlayer.class);
goToAddPlayer.putExtra("ID", teamID);
this.startActivity(goToAddPlayer);
this.finish();
return;
}
}
int shooting = c2.getInt(c.getColumnIndexOrThrow("shooting"));
should be
int shooting = c2.getInt(c2.getColumnIndexOrThrow("shooting"));
You are now working on 2nd query but trying to get column index from 1st.
I am trying, unsucessfully, to query my database to find the maximum 'area number', in my areas table for a certain inspection, so that I can set the text in a form to the next area number.
The database table consists of four columns; _id, inpsection_link, area_number, area-reference.
I have created the following in my database helper class (using this post as a guide: SQLiteDatabase.query method):
public int selectMaxAreaNumber (long inspectionId) {
String inspectionIdString = String.valueOf(inspectionId);
String[] tableColumns = new String[] {
AREA_NUMBER,
"(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max"
};
String whereClause = INSPECTION_LINK + " = ?";
String[] whereArgs = new String[] {
inspectionIdString
};
Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs,
null, null, null);
int maxAreaNumber = c.getColumnIndex("max");
return maxAreaNumber;
}
Which I then call in the areaEdit class as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
areaId = i.getLongExtra("Intent_AreaID", -1);
if (areaId == -1) {
nextAreaNumber = rmDbHelper.selectMaxAreaNumber(inspectionId) + 1;
Toast.makeText(getApplicationContext(), String.valueOf(nextAreaNumber),
Toast.LENGTH_LONG).show();
}
setContentView(R.layout.edit_area);
setUpViews();
populateFields();
setTextChangedListeners();
}
However, it just returns 1 everytime (even if there are numbers higher than that stored in the database).
Confused.com!! Any help much appreciated.
Your issue is here :
int maxAreaNumber = c.getColumnIndex("max");
You're getting the column index of max, which is 1 because you only have one column in your query.
Instead, do something like this :
int maxAreaNumber = 0;
if(c.moveToFirst())
{
maxAreaNumber = c.getInt(1);
// or cleaner
maxAreaNumber = c.getInt(c.getColumnIndex("max"));
}
else
// no data in cursor
Hi am currently doing a project related to Contact ,Where I am fetching details from the contact*(Email,Number and Contactname)* and it does well.But the problem is it take a long time to fetch the contact details ( 1000+ contact including contact that sync from social network sites).so that I put a Asynchronous Task for this purpose ,and it does well but the problem is due to a long time to complete the fetching process ,when I press the back button it crash during the Asynchronous task .My problem does not crash why this fetching contact take a lot of time.Is there way to get the contact faster. My code to get contact detail is as given below
public void readContact() {
contactname = new ArrayList<String>();
contactnumber = new ArrayList<String>();
companyname_one = new ArrayList<String>();
contactemail = new ArrayList<String>();
people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
PhoneLookup.DISPLAY_NAME);
while (people.moveToNext()) {
int nameFieldColumnIndex = people
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
if (contact == null) {
contactname.add("No contact Set");
} else {
contactname.add(contact);
}
String szId = people.getString(people
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
cursor_one = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
+ szId + "'", null, null);
if (cursor_one.moveToNext()) {
String number = cursor_one.getString(cursor_one
.getColumnIndex(Phone.NUMBER));
contactnumber.add(number);
cursor_one.close();
} else {
contactnumber.add("no number");
cursor_one.close();
}
emails_value = getContentResolver().query(
Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "='"
+ szId + "'", null, null);
if (emails_value.moveToNext()) {
email_sorting = emails_value.getString(emails_value
.getColumnIndex(Email.DATA));
checkAll();
} else {
contactemail.add("no email");
emails_value.close();
}
}
people.close();
System.out.println("noz " + contactnumber);
System.out.println("name" + contactname);
System.out.println("email" + contactemail);
System.out.println("noz size " + contactnumber.size());
System.out.println("name size " + contactname.size());
System.out.println("contactemail size " + contactemail.size());
}
The checkAll() method is patter matching of email as below
public boolean checkAll() {
boolean chkAll = true;
Pattern p1 = Pattern.compile(".+#.+\\.[a-z]+");
Matcher m1 = p1.matcher(email_sorting.trim());
if (!m1.matches()) {
contactemail.add("no email");
contactemail_sort.add("no email");
emails_value.close();
chkAll = false;
} else {
contactemail.add(email_sorting);
contactemail_sort.add(email_sorting);
emails_value.close();
chkAll = true;
}
return chkAll;
}
Ok, I think I finally see the best way for this to work. Instead of pulling all of the contact information out before you create your adapter, you should create a custom CursorAdapter that accepts your people cursor and populates a custom view with your cursor_one query that is executed on a background thread. This should take advantage of the natural lazyloading of the ListView and make this work like you want.
If your using at least Android 3.0, you can use Loaders instead of using an AsyncTask.
I found an example of a custom cursor adapter here which is what I used to make my example. There are probably better ways to implement this in code, but this at least shows you which methods to override and what to put in them.
public class ContactListCursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
// Used to populate new list items
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
getDeatils(v,c);
return v;
}
// Used to bind a new item from the adapter to an existing view
#Override
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
getDetails(v,c);
}
private void populateDetails(View v, Cursor c) {
// Start your AsyncTask or Loader to get the details.
// Be sure to populate the view with the results in the
// appropriate completion callback.
}
}
My suggestion would be to redefine how your structure works. For instance it is really quick to get the basic information about a contact, the display name and other meta data for instance. Show that in a list or whatever and then when they select or view a contact then load the rest of the information.
This will speed it up a lot. However if you must load everything about a contact at once then yes it will be very slow due to how many queries you need to execute.
Another suggestion would be to show them as the completed to the user. That way there is some progress and they can view it. Instead of just putting a dialog up that says "Loading Wait".
I'm trying to get contact data once a contact name has been clicked from my list view. As per the code below, I can log successfully the ID of my contact, but haven't managed to use the ContactsContract to retrieve the data. What's the best way to do this? (have tried Retrieve Contact Phone Number From URI in Android to not much avail)
EDIT 2 : Fixed code, now works
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_manager);
mContactList = (ListView) findViewById(R.id.contactList);
populateContactList();
mContactList.setOnItemClickListener(new OnItemClickListener() {
String strid = Long.toString(id);
Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{strid}, null);
if (result.moveToFirst()) {
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(strid)}, null);
if(c.moveToFirst()){
int phoneColumn = c.getColumnIndex("data1");
String phoneNumber = c.getString(phoneColumn);
Log.d("DATA",phoneNumber);
}
}
});
}
EDIT 1 : forgot some important stuff. The code is adapted from the ContactManager example from the Android dev site.
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* #return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
Assuming you have a valid contactID, you can do this:
Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null,
ContactsContract.Contacts._ID +" = ?",
new String[]{contactID}, null);
if (result.moveToFirst()) {
for(int i=0; i< result.getColumnCount(); i++){
Log.i("CONTACTSTAG", result.getColumnName(i) + ": "
+ result.getString(i));
}
}
You will have to change the ContactsContract.Contacts.CONTENT_URI and the where clause to the table that you are querying. The above code will print out a bunch of general info about a contact.
i am using following code in order to fetch contacts from Phone
i am getting names but not number , help me to find out other fields of contacts.
public class DialActivity extends Activity {
private ListView mContactList;
public String[] fields;
public Cursor cursor;
public boolean mShowInvisible;
public Uri uri;
public String[] projection;
public String[] selectionArgs;
public String selection;
public String sortOrder;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContactList = (ListView) findViewById(R.id.ListView01);
// Populate the contact list
populateContactList();
}
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor,
fields, new int[] {R.id.TextView01});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* #return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
}
In the above code i am using a custom ListRow layout and showing the contact name in each row, but want to populate a list which has names and corresponding number so that i can make a call on those number by clicking on that listItem. I know how to make call but i am not having the numbers.
ContactsContract.Contacts doesn't contain the phone number but you have the id of the contacts so you can now query ContactsContract.Data to get the phone number. A sample query can be found at the documentation: http://developer.android.com/reference/android/provider/ContactsContract.Data.html