Hello I would like a detailed explanation or an example of retrieving two details in the android contact list(name and Number). Once I receive these details I want to write them onto a XML file. As I need to send this XML file to a php server.
I know the permission required to do this is
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
and I need to work with Android Cursor and ContactsContract to do this. But am not being able to find a good example to do the same. If any one could provide a good pointer or a detailed example of what am looking for it will be highly appreciated. Thanks in advance.
private void readContacts() {
String[] projection = {ContactsContract.Contacts.DISPLAY_NAME};
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
Log.i("Demo", "" + cursor.getCount());
String[] strs = new String[cursor.getCount()];
cursor.moveToFirst();
int i = 0;
do {
strs[0] = cursor.getString(0);
}while (cursor.moveToNext());
ArrayAdapter<String> liststr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
getListView().setAdapter(liststr);
}
}
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Related
I have some contacts autocomplete and autosearch algorithm working for my android app.
First some xml to define the text view for the input:
<AutoCompleteTextView
a:id="#+id/recipientBody"
a:layout_width="0dip"
a:layout_height="wrap_content"
a:layout_weight="1.0"
a:nextFocusRight="#+id/smsRecipientButton"
a:hint="#string/sms_to_whom"
a:maxLines="10"
/>
And now I setup the text view
AutoCompleteTextView recip =
(AutoCompleteTextView) findViewById(R.id.recipientBody);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.list_item, getAllContacts());
recip.setAdapter(adapter);
And now the actual algorithm that searches for a contact that matches the input:
private List<String> getAllContacts() {
List<String> contacts = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{contactId}, null);
while (pCursor.moveToNext()) {
String phoneNumber = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(phoneNumber + " ( " + displayName + " )");
}
pCursor.close();
}
}
}
return contacts;
}
This works fine for both contact number and name input. But there is still a problem. The user can input multiple phone numbers. But when one contact is applied to the text view it cannot search again, because the algorithm takes the whole string.
How can I solve that?
EDIT:
Well, I thought about it for a while and spotted a problem with my solution - there's no place where you could insert the contact from the completion list into the TextView.
The solution seems to be MultiAutoCompleteTextView, this thing is designed for solving your problem.
Sorry for confusion!
For me, it looks like you need a custom adapter.
You may extend ArrayAdapter<String> and implement getFilter() - of course you will also need a custom filter (extending Filter) which instance you will return from that method.
Filter's performFiltering method has one parameter - the string for which the list of suggestion is needed. You need to take the part after the last comma (or whatever character are you using as a separator) and return the suggestion list for that substring.
P.S.
For the better user experience, you may also think of styling your AutoCompleteTextView contents with Spans: http://ballardhack.wordpress.com/2011/07/25/customizing-the-android-edittext-behavior-with-spans/
I'm back with another question regarding Android databases.
I'm storing a simple string into my android database and then wish to display all the entries as a list. However, the list shows nothing on my emulator (or my actual device) and neither does LogCat (either when saving entries into the DB or when calling them).
Below you have my code
the save entries function:
public long createEntry(String entry){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ENTRY, entry);
return db.insert(DATABASE_TABLE, null, initialValues);
}
the get entries function:
public Cursor fetchAllEntries(){
return db.query(DATABASE_TABLE,new String [] {KEY_ROW_ID, KEY_ENTRY}, null, null, null, null, null);
}
the List Activity:
entryList = (ListView)findViewById(R.id.recordsList);
Cursor c = dbAdapter.fetchAllEntries();
if(c!=null){
startManagingCursor(c);
String [] from = new String [] {PirelliDbAdapter.KEY_ENTRY};
int [] to = new int [] {R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.entryrow, c, from, to);
entryList.setAdapter(adapter);
}
dbAdapter.close();
And finally the List Activity's XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/recordsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/textViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Thanks a lot for taking the time to look over this and help me out. I'm really stumped and don't know where to go from here...
Firstly, check that your database is created or not then check whether table is created or not , through command line. If all-things are OK then try to print cursor to check it retrieves data from db or not. for printing the cursor values use following logic..
*if(!cursor.isAfterLast())
{
cursor.moveToFirst();
do{
String entry = cursor.getString(cursor.getColumnIndex(KEY_ENTRY));
Log.v("value:",entry);
cursor.moveToNext();
}while(!cursor.isAfterLast());*
Your code has no issue , will you please check in the database file weather the data is there one way is use sqlite browser (or) go through this in command prompt
{$ adb shell $ cd data/data/projectname/databases/
check if your db file is created and then move on to -->
sqlite3 database file name you will find the sqlite prompt- hence you can explore as .tables and select * from table Name }
the above steps are mentioned in view of newbees if you know this earlier please ignore
to explore the db file.
please check this https://stackoverflow.com/questions/2149438/tool-to-see-android-database-tables-and-data
This might be a really simple answer but I cant seem to come up with an answer for it, I'm aiming for android 2.1.
Any help would be great :-)
Try this within your Activity or other application component:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = formatName(cur.getString(cur
.getColumnIndex(People.DISPLAY_NAME)));
// Do something with name.
}
}
First you have to add `ListView` in your layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Now use this code in your Activity :
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME} ,
new int[] {android.R.id.text1});
setListAdapter(adapter);
Can anyone shed a light on how to get contact list from android?.
I just want to get the same list as in the dialer app. But im getting a lots of contacts that are not on the dialer list with the code below.
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Contacts.People.CONTENT_URI, null, null, null, Contacts.ContactMethods.DEFAULT_SORT_ORDER);
startManagingCursor(cursor);
Thanks in advance.
Try this snippet:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.SimpleCursorAdapter;
public class ContactList extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME, Phone.NUMBER}, null, null, null);
startManagingCursor(cursor);
String[] from = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER};
int[] to = new int[] { R.id.name_entry, R.id.number_entry};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_entry, cursor, from, to);
this.setListAdapter(adapter);
}
}
XML file is:
list_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip">
<TextView
android:id="#+id/name_entry"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18dip"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/number_entry"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="18dip"/>
</LinearLayout>
What you have seems fine. Could you elaborate on "getting a lots of contacts that are not on the dialer list"? Is it that Android is making up people? Or is it that you are seeing people with email addresses but no phone numbers (who therefore might not show up in the Dialer)?
Note that Contacts.People is for Android 1.6 and below. That provider is deprecated starting with Android 2.0, replaced by the ContactsContract set of providers.
This is basic implementation of android contact list Activity.
Well, thanks for the answer first. Just to shed a light on this.
I just wanted to get emails only for the contacts on my phone. The "MyContacts" group. I saw this is the group ContactList Activity uses.
I finished doing somethig like this:
c = cr.query(myGroupUri, mEmailsProjection, null, null, null);
....
c.close();
c = cr.query(
Contacts.ContactMethods.CONTENT_URI,
mContactsProjection, contactIds, null, null
);
....
c.close();
Just queried the group first and then the emails table.
try using intent to go to the contact list
startActivityForResult( new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI),1);}
I have just begun working with the Android SDK and I am having problems with my first app. Currently, I am trying to list all of the users in a big list. However, no matter what I try, the app continues to force close. I found the code in the sample files, but this is still giving me issues. Below is the code I am using.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] projection = new String[] {
People._ID,
People._COUNT,
People.NAME,
People.NUMBER
};
//Get the base URI for the People table in the Contacts content provider.
Uri contacts = People.CONTENT_URI;
//Make the query.
Cursor managedCursor = managedQuery(contacts,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
Cursor c = getContentResolver().query(Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.text1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.main, c, columns, names);
setListAdapter(mAdapter);
}
This is directly from the sample file, yet it still errors out. I have found that the line that causes the problem is the "Cursor managedCursor = managedQuery(contacts," line. Has anyone else out there seen this? I am at a loss and have not found any solutions after 2 hours or research.
Also, I have added the following line to my app's manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
Thanks, and if you need more information please let me know.
I believe the example from the SDK docs is out of date. Try getting rid of the People._COUNT column from the cursor projection.
It's probably causing an IllegalArgumentException (see the output from adb logcat)