Your MainActivity.java like this;
package com.example.contactdisplay;
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
EditText edt_text;
Button btn_add;
Context context = this;
ListView listview1;
ArrayAdapter<String> adapter;
ArrayList<String> arraylist;
ArrayList<String> searchedName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_text = (EditText) findViewById(R.id.edt_search);
listview1 = (ListView) findViewById(R.id.list);
arraylist = new ArrayList<String>();
searchedName = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Name", name);
arraylist.add(name);
// For get Email address
/*
* Cursor
* emailcur=cr.query(ContactsContract.CommonDataKinds.Email
* .CONTENT_URI
* ,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID
* +"="+id, null, null);
*
* while (emailcur.moveToNext()) {
*
* // This would allow you get several email addresses String
* emailAddress = emailcur .getString(emailcur
* .getColumnIndex(ContactsContract
* .CommonDataKinds.Email.DATA));
*
* Log.e("email==>", emailAddress); arraylist.add(emailAddress);
* // emaildata
* +=" "+emailAddress+" "+"--------------------------------------"
* ; }
*/
}
Log.i("Total Contacts", "" + arraylist.size());
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, arraylist);
listview1.setAdapter(adapter);
}
// You can use phone number also to display in ListView through below
// code
/*
* cursor.moveToFirst(); do {
*
* String phoneNumber = cursor.getString(phoneNumberIdx);
*
* Log.i("Name is........", phoneNumber); arraylist.add(phoneNumber);
*
* } while (cursor.moveToNext());
*/
edt_text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.i("Text changed", s.toString());
getFilterContacts(s);
}
});
}
public void getFilterContacts(CharSequence nameKey) {
if (nameKey.toString().length() == 0) {
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, arraylist);
listview1.setAdapter(adapter);
return;
}
searchedName = new ArrayList<String>();
for (int i = 0; i < arraylist.size(); i++) {
String contactName = arraylist.get(i);
searchedName.add(contactName);
}
// searchedName;
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, searchedName);
listview1.setAdapter(adapter);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:hint="search here.." />
<Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
package com.example.contactdisplay;
import java.util.ArrayList;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
EditText edt_text;
Button btn_add;
Context context = this;
ListView listview1;
ArrayAdapter<String> adapter;
ArrayList<String> arraylist;
ArrayList<String> searchedName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt_text = (EditText) findViewById(R.id.edt_search);
listview1 = (ListView) findViewById(R.id.list);
arraylist = new ArrayList<String>();
searchedName = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("Name", name);
arraylist.add(name);
// For get Email address
/*
* Cursor
* emailcur=cr.query(ContactsContract.CommonDataKinds.Email
* .CONTENT_URI
* ,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID
* +"="+id, null, null);
*
* while (emailcur.moveToNext()) {
*
* // This would allow you get several email addresses String
* emailAddress = emailcur .getString(emailcur
* .getColumnIndex(ContactsContract
* .CommonDataKinds.Email.DATA));
*
* Log.e("email==>", emailAddress); arraylist.add(emailAddress);
* // emaildata
* +=" "+emailAddress+" "+"--------------------------------------"
* ; }
*/
}
Log.i("Total Contacts", "" + arraylist.size());
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, arraylist);
listview1.setAdapter(adapter);
}
// You can use phone number also to display in ListView through below
// code
/*
* cursor.moveToFirst(); do {
*
* String phoneNumber = cursor.getString(phoneNumberIdx);
*
* Log.i("Name is........", phoneNumber); arraylist.add(phoneNumber);
*
* } while (cursor.moveToNext());
*/
edt_text.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Log.i("Text changed", s.toString());
getFilterContacts(s);
}
});
}
public void getFilterContacts(CharSequence nameKey) {
if (nameKey.toString().length() == 0) {
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, arraylist);
listview1.setAdapter(adapter);
return;
}
searchedName = new ArrayList<String>();
for (int i = 0; i < arraylist.size(); i++) {
String contactName = arraylist.get(i);
searchedName.add(contactName);
}
// searchedName;
adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, searchedName);
listview1.setAdapter(adapter);
}
}
Your activity_main like this;
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:hint="search here.." />
<Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD" />
</LinearLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
No comments:
Post a Comment