get the selected contact phone numbers from multi autocomplete textview - android

i am new to android, how to get phone numbers of selected contacts from MultiAutocompleteTextview when clicks on button ?
method to read contacts for multi-autocomplete textview
private void readContactData() {
// TODO Auto-generated method stub
String phoneNumber = "";
String phoneName = "";
phoneValueArr.clear();
nameValueArr.clear();
try{
ContentResolver content = getContentResolver();
cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PEOPLE_PROJECTION, null, null, null);
if(null != cursor && cursor.moveToFirst()){
do{
// Get Phone number
phoneNumber =""+cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneName = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
phoneValueArr.add(phoneNumber.toString());
nameValueArr.add(phoneName.toString());
}while(cursor.moveToNext());
}
//cursor.close();
}catch(Exception e){
Log.i("AutocompleteContacts","Exception : "+ e);
}finally {
//if (null != cursor)
//cursor.close();
}
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
mAuto.setAdapter(adapter);
}
my ContactsListAdapter
public static class ContactListAdapter extends CursorAdapter implements Filterable {
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View retView = inflater.inflate(R.layout.schedule_msg_custcontview,parent,false);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
//((TextView) view).setText(cursor.getString(2));
TextView pname = (TextView)view.findViewById(R.id.ccontName);
TextView pnum = (TextView)view.findViewById(R.id.ccontNo);
pname.setText(cursor.getString(2));
pnum.setText(cursor.getString(1));
}
#Override
public String convertToString(Cursor cursor) {
return cursor.getString(2);
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args,
null);
}
private ContentResolver mContent;
}
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME,
};
and how can i get my selected contact numbers into an object to store in database while pressing the button. And while loading contacts its giving an exception saying that
12-11 12:39:11.422: I/AutocompleteContacts(17735): Exception : android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 188
can any one help me ?
This is my multi auto complete OnItemClick Listener and its always giving index of selected name index -1
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// Get Array index value for selected name
int i = nameValueArr.indexOf(""+parent.getItemAtPosition(position));
// If name exist in name ArrayList
if (i >= 0) {
// Get Phone Number
toNumberValue = phoneValueArr.get(i);
InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// Show Alert
Toast.makeText(getBaseContext(), "Position:"+position+" Name:"+parent.getItemAtPosition(position)+" Number:"+toNumberValue,
Toast.LENGTH_LONG).show();
Log.d("AutocompleteContacts", "Position:"+position+" Name:"+parent.getItemAtPosition(position)+" Number:"+toNumberValue);
}
}

I solved the multi-autocomplete OnItemclickListener issue by changing the code as below
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView temp = (TextView) view.findViewById(R.id.ccontNo);
TextView temp2 = (TextView) view.findViewById(R.id.ccontName);
final String selectedNumber = temp.getText().toString();
final String selectedName = temp2.getText().toString();
if (selectedNumber != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
// Show Alert
Toast.makeText(getBaseContext(), " Name:"+selectedName+" Number:"+selectedNumber, Toast.LENGTH_LONG).show();
}
}
Now i am getting the selected contact name and number. Then store those values in HashMap and while button click split the selected contacts by "," and iterate the loop for each name to get the contact number.
In this way i solved my problem hope it is helpful !! if so up Vote the answer!!!

Related

Get and select contacts in android

I'm trying get all contacts and select it.
I completed get all contacts in my phone. But when i try to select a few contacts and get their names or numbers, I faced nullpointer error.
public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> {
private CursorAdapter mAdapter;
final HashMap<String,String> hashMap = new HashMap<String,String>();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
private Uri uriContact;
private String contactID;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create adapter once
Context context = getActivity();
int layout = android.R.layout.simple_list_item_multiple_choice;
Cursor c = null; // there is no cursor yet
int flags = 0; // no auto-requery! Loader requeries.
mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// each time we are started use our listadapter
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// and tell loader manager to start loading
getLoaderManager().initLoader(0, null, this);
***getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursorID = getActivity().getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(contactID);
}
});
}***
// columns requested from the database
private static final String[] PROJECTION = {
Contacts._ID, // _ID is always required
Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
};
// and name should be displayed in the text1 textview in item layout
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(),
contentUri,
PROJECTION,
null,
null,
null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
mAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// on reset take any old cursor away
mAdapter.swapCursor(null);
}
I think problem is my onItemClickListener.
How can i fix this?
Thanks from now :)
The main problem is that uriContact is null when you make the query.
The other problem is that you are only using ContactsContract.Contacts._ID as the projection, so the ID is the only thing returned.
I got it working by using null for the projection so that it returns all rows.
I also added functionality to find the currently selected contact, and display a Toast with their phone number.
This is not optimal code, since it just queries all rows and then iterates through them until it finds the currently selected contact, but it works:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cur = mAdapter.getCursor();
cur.moveToPosition(position);
String curName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
System.out.println(curName);
uriContact = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursorID = getContentResolver().query(uriContact,
null,
null, null, null);
for (cursorID.moveToFirst(); !cursorID.isAfterLast(); cursorID.moveToNext() ) {
String testName = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
if (testName != null && testName.equals(curName)) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
if (contactID != null) {
System.out.println(contactID);
Toast.makeText(MainActivity.this, "contact Phone: " + contactID, Toast.LENGTH_LONG).show();
}
}
});
Edit: I got a more optimized version working, which uses the selection and selectionArgs in the query to return just the current contact info:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cur = mAdapter.getCursor();
cur.moveToPosition(position);
String curName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
System.out.println(curName);
uriContact = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor cursorID = getContentResolver().query(uriContact,
null,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " = ?", new String[]{curName}, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
if (contactID != null) {
System.out.println(contactID);
Toast.makeText(MainActivity.this, "contact Phone: " + contactID, Toast.LENGTH_LONG).show();
}
}
});

Pick a number from calllog without repeating the same number

I want user to select a number from calllog and that number get selected and come in the activity. So I created custom calllog list. I used this code but it is not showing the call log list in right order
first thing it is showing the callhistory of the first number fully that it gets in the calllog list
second I wnt to show the name also, I tried a lot but I am not able to do
Can anyone tell what amendments i make in this code to make it right
The code I used is:
String[] callLogFields = { android.provider.CallLog.Calls._ID,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.CACHED_NAME };
String viaOrder = android.provider.CallLog.Calls.DATE + " DESC";
String WHERE = android.provider.CallLog.Calls.NUMBER + " >0"; /*filter out private/unknown numbers */
final Cursor callLog_cursor = this.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, callLogFields,
WHERE, null, viaOrder);
AlertDialog.Builder myversionOfCallLog = new AlertDialog.Builder(this);
android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int item) {
callLog_cursor.moveToPosition(item);
Log.v("number", callLog_cursor.getString(callLog_cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER)));
callLog_cursor.close();
}
};
myversionOfCallLog.setCursor(callLog_cursor, listener,
android.provider.CallLog.Calls.NUMBER);
myversionOfCallLog.setTitle("Choose from Call Log");
myversionOfCallLog.create().show();
You can add the Contact Numbers in a Set, which will prevent adding duplicate contact numbers. Then add the Set's data to listview as you want.
Set<String> setNumbers = new HashSet<String>();
String callNumber = cursor.getString(cursor.getColumnIndex(
android.provider.CallLog.Calls.NUMBER));
setNumbers.add(callNumber);
Hope this helps.
For saving numbers without duplicates, as MysticMagic suggested, use 'Set' as per the link given in the comment.
For getting the contact name from the phone number, use code :
(Reference)
private String getContactName(Context context, String number) {
String name = null;
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
Log.v(TAG, "Started uploadcontactphoto: Contact Found # " + number);
Log.v(TAG, "Started uploadcontactphoto: Contact name = " + name);
} else {
Log.v(TAG, "Contact Not Found # " + number);
}
cursor.close();
}
return name;
}
Also refer here for another method to fetch name in phone call history
Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);
String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going
Finally this is the code that worked with the help of MysticMagic and Nishanthi Grashia
Set setA;
setA = new HashSet();
public void getCallLog() {
try {
final String[] projection = null;
final String selection = null;
final String[] selectionArgs = null;
final String sortOrder = "DATE DESC";
final Cursor cursor = this.getContentResolver().query(
Uri.parse("content://call_log/calls"), projection,
selection, selectionArgs, sortOrder);
if (cursor != null) {
// Loop through the call log.
while (cursor.moveToNext()) {
// Common Call Log Items
String callNumber = cursor
.getString(cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
setA.add(callNumber);
}
generateList();
}
} catch (Exception e) {
}
}
#SuppressLint("NewApi")
private void generateList() {
// TODO Auto-generated method stub
try {
Object[] calllist = new String[setA.size()];
calllist = setA.toArray();
String scalllist[] = Arrays.copyOf(calllist, calllist.length,
String[].class);
for (int i = 0; i < scalllist.length; i++) {
scalllist[i] = scalllist[i] + " "
+ getContactName(this, scalllist[i]);
}
final Dialog d = new Dialog(this);
d.setContentView(R.layout.dialog1);
d.setTitle("Choose from Call Log...");
final ListView lv1 = (ListView) d.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
scalllist);
lv1.setAdapter(adapter);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String clickednumber[] = (lv1.getItemAtPosition(arg2)
.toString()).split(" ");
usernumber.setText(clickednumber[0]);
try {
username.setText(clickednumber[1]);
} catch (ArrayIndexOutOfBoundsException e) {
username.setText(" ");
}
d.dismiss();
}
});
d.show();
} catch (Exception e) {
}
}
private String getContactName(Context context, String number) {
String name = null;
try {
// define the columns I want the query to return
String[] projection = new String[] {
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID };
// encode the phone number and build the filter URI
Uri contactUri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
// query time
Cursor cursor = context.getContentResolver().query(contactUri,
projection, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
} else {
name = " ";
}
cursor.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return name;
}

Retrieve whatsapp contacts from phonebook in android

When i was looking through my contacts in phone i see whatsapp number also for those who have whatsapp account.The thing which i want to do is filter my contact list and display numbers which are synced with whatsapp.I am successfully retrieving all the contact id,number and account associated with it but not displaying whatsappaccount.Only associated google account is getting displayed.Is there any way to get whatsapp contact from local phonebook itself? I used the following code:
ContentResolver cr1 = getContentResolver();
Cursor cur = cr1.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0)
{
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr1.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext())
{
//phoneContactList.add(name);
Log.i("Contact List", name);
Log.i("Contact List", id);
getContactAccount(id,cr1);
}
pCur.close();
}
}
}
public void getContactAccount(String id,ContentResolver contentResolver){
Cursor cursor = null;
try {
cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
ContactsContract.RawContacts.CONTACT_ID +"=?",
new String[]{String.valueOf(id)},
null);
if (cursor != null && cursor.getCount() >0){
cursor.moveToFirst();
System.out.println("Account name is"+cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME)));
System.out.println("Account type is"+cursor.getString(cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE)));
cursor.close();
}
} catch (Exception e) {
System.out.println(""+this.getClass().getName()+","+ e.getMessage());
} finally{
cursor.close();
}
}
Use following code to get WhatsApp contacts from phonebook with associated WhatsApp number :
private void displayWhatsAppContacts() {
final String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE,
"account_type",
ContactsContract.Data.DATA3,
};
final String selection = ContactsContract.Data.MIMETYPE + " =? and account_type=?";
final String[] selectionArgs = {
"vnd.android.cursor.item/vnd.com.whatsapp.profile",
"com.whatsapp"
};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(
ContactsContract.Data.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
String number = c.getString(c.getColumnIndex(ContactsContract.Data.DATA3));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("WhatsApp", "name " +name + " - number - "+number);
}
Log.v("WhatsApp", "Total WhatsApp Contacts: " + c.getCount());
c.close();
}
The MIME type for whatsapp is: "vnd.android.cursor.item/vnd.com.whatsapp.profile"
Use the following code`:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements LoaderCallbacks<Cursor>,AdapterView.OnItemClickListener {
/*
* Defines an array that contains column names to move from
* the Cursor to the ListView.
*/
private final static String[] FROM_COLUMNS = {
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Data.DISPLAY_NAME :
Data.DISPLAY_NAME
};
/*
* Defines an array that contains resource ids for the layout views
* that get the Cursor column contents. The id is pre-defined in
* the Android framework, so it is prefaced with "android.R.id"
*/
private final static int[] TO_IDS = {
android.R.id.text1
};
// Define global mutable variables
// Define a ListView object
ListView mContactsList;
// Define variables for the contact the user selects
// The contact's _ID value
long mContactId;
// The contact's LOOKUP_KEY
String mContactKey;
// A content URI for the selected contact
Uri mContactUri;
// The column index for the _ID column
private static final int CONTACT_ID_INDEX = 0;
// The column index for the LOOKUP_KEY column
private static final int LOOKUP_KEY_INDEX = 1;
// Defines the text expression
private static final String mime_type ="vnd.android.cursor.item/vnd.com.whatsapp.profile";
private static final String SELECTION =
Data.MIMETYPE + " = '" + mime_type + "'";
// Defines a variable for the search string
private String mSearchString="Gourav";
// Defines the array to hold values that replace the ?
private String[] mSelectionArgs = { mSearchString };
private static final String[] PROJECTION =
{
Contacts._ID,
Contacts.LOOKUP_KEY,
Data.DISPLAY_NAME,
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Data.MIMETYPE :
Data.MIMETYPE
};
// An adapter that binds the result Cursor to the ListView
private SimpleCursorAdapter mCursorAdapter;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView) getActivity().findViewById(R.id.list);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contact_list,
null,
FROM_COLUMNS, TO_IDS,
0);
// Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// Get the Cursor
Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
// Move to the selected contact
cursor.moveToPosition(position);
// Get the _ID value
mContactId = cursor.getLong(CONTACT_ID_INDEX);
// Get the selected LOOKUP KEY
mContactKey = cursor.getString(LOOKUP_KEY_INDEX);
// Create the contact's content Uri
mContactUri = Contacts.getLookupUri(mContactId, mContactKey);
/*
* You can use mContactUri as the content URI for retrieving
* the details for a contact.
*/
}
#Override
public android.support.v4.content.Loader<Cursor> onCreateLoader(
int arg0, Bundle arg1) {
// TODO Auto-generated method stub
/*
* Makes search string into pattern and
* stores it in the selection array
*/
CursorLoader cursor = null;
mSelectionArgs[0] = "%" + mSearchString + "%";
Log.d("ON CREATE LOADER", "ONCLREATE LOADER CALLLEd");
try
{
cursor=new CursorLoader(
getActivity(),
Data.CONTENT_URI,
PROJECTION,
SELECTION,
null,
null
);
}
catch(Exception e)
{
e.printStackTrace();
}
// Starts the query
return cursor;
}
#Override
public void onLoadFinished(
android.support.v4.content.Loader<Cursor> arg0, Cursor cursor) {
// TODO Auto-generated method stub
Log.d("onLoadFinished", String.valueOf(cursor.getCount()));
mCursorAdapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(android.support.v4.content.Loader<Cursor> arg0) {
// TODO Auto-generated method stub
mCursorAdapter.swapCursor(null);
}
}
}

Android: Redundancy issue in datahelper class

I have two tables with names disease_table and sysmptoms_table. I retrieved the data from disease_table from the DB and displayed on the listview and when the listitem is clicked, I have to select and display disease category symptoms accordingly and I did that successfully but my code has redundancy, I had to write two methods in the datahelper class to retrieve the symptoms as per the disease in another listview. and I am retrieving the symptom data in list view with the query with the condition of WHERE "disease_id=1" with foreign key reference
the code for the methods is as follows,
//getting pain symptom names in a arraylist and then display in listview
//this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,symptompain));
public List<String> getAllSymptomPain() {
List<String> symptompain = null;
cr = db.query(SYMPTOM_TABLE_NAME, new String[] {"symname"}, "diseaseid=1", null, null, null, null);
if(null != cr){
symptompain = new ArrayList<String>();
if (cr.moveToFirst()) {
do {
symptompain.add(cr.getString(0));
} while (cr.moveToNext());
}
if (cr != null && !cr.isClosed()) {
cr.close();
}
}
return symptompain;
}
//getting colorchange symptom names in a arraylist and then display in listview
//this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,symptomcolorchange));
public List<String> getAllSymptomColorChange() {
List<String> symptomcolorchange = null;
cr = db.query(SYMPTOM_TABLE_NAME, new String[] {"symname"}, "diseaseid=2", null, null, null, null);
if(null != cr){
symptomcolorchange = new ArrayList<String>();
if (cr.moveToFirst()) {
do {
symptomcolorchange.add(cr.getString(0));
} while (cr.moveToNext());
}
if (cr != null && !cr.isClosed()) {
cr.close();
}
}
return symptomcolorchange;
}
How can I write these two in a single method and then call it in class which extends listactivity under onListItemclick method?
And my OnListItemClick() method is as follows :
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String item=(String)getListAdapter().getItem(position);
if(item.equals("Pain in Teeth")){
// passing the method here
}
else if(item.equals("Pain in Gums")){
// passing the method here
}
else if(item.equals("Pain in Mucosa")){
// passing the method here
}
else if(item.equals("Pain in TMJoint")){
// passing the method here
}
else if(item.equals("Non-Specific Pain")){
// passing the method here
}
}
Try this:
public List<String> getSymptomsByDiseaseId(long diseaseId) {
List<String> symptomsList = new ArrayList<String>();
String selection = "diseaseid=?";
String[] selectionArgs = { String.valueOf(diseaseId) };
Cursor cursor = db.query(false, SYMPTOM_TABLE_NAME, null, selection, selectionArgs, null, null, null, null);
if (cursor.moveToFirst()) {
do {
symptomsList.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
return symptomsList;
}

Android - Custom AutoCompleteTextView CursorAdaptor - Suggestion Behavior

I am trying to implement a custom AutoCompleteTextView for choosing a contact's phone number from a list of suggestions that display the contact name, phone number type, and phone number. I created a custom CursorAdapter that defines and sets my Layout and TextViews for each suggestion and queries contacts based on the user-entered text via runQueryOnBackgroundThread. I'm running into an issue where the suggestions seem correct for the first two values entered (e.g. "ab" suggests "abcd" and "abyz") but not for anything beyond that (e.g. "abc" suggests "abyz"). For the latter, when the "abyz" suggestion is selected, the values for "abcd" are returned.
Code for the main activity:
final ContactInfo cont = new ContactInfo(ctx);
Cursor contacts = cont.getContacts2(null);
startManagingCursor(contacts);
ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts);
mPersonText.setAdapter(adapter);
mPersonText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
mPersonNum.setText(number);
}
});
Code for my contacts class that returns a cursor for all contacts:
public Cursor getContacts2(String where)
{
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
return people;
}
Code for my CursorAdapter:
public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {
private TextView mName, mType, mNumber;
private ContentResolver mContent;
public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater mInflater = LayoutInflater.from(context);
final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);
mName = (TextView) ret.findViewById(R.id.name);
mType = (TextView) ret.findViewById(R.id.phonetype);
mNumber = (TextView) ret.findViewById(R.id.phonenum);
return ret;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIdx);
int type = cursor.getInt(typeIdx);
String number = cursor.getString(numberIdx);
mName.setText(name);
if (type == 1) {mType.setText("Home");}
else if (type == 2) {mType.setText("Mobile");}
else if (type == 3) {mType.setText("Work");}
else {mType.setText("Other");}
mNumber.setText(number);
}
#Override
public String convertToString(Cursor cursor) {
int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameCol);
return name;
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
// this is how you query for suggestions
// notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
"UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
}
As I said above, when the user enters "ab" into the AutoCompleteTextView the suggestions are "abcd" and "abyz", however when the user types "abc" the suggestion is just "abyz". When the user selects "abyz" in that case, the values for "abcd" are returned. Here are two screenshots that show what I'm trying to describe:
I've read every question I could find here and elsewhere but can't seem to figure this out. I'm fairly new to Android development so I apologize in advance if my mistake is a simple one. Thanks in advance!
I seem to have answered my own question after more research. Moving the setting of the views for my textViews from the newView function to the bindView function seems to have done the trick, which I think makes sense...
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater mInflater = LayoutInflater.from(context);
final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);
return ret;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIdx);
int type = cursor.getInt(typeIdx);
String number = cursor.getString(numberIdx);
mName = (TextView) view.findViewById(R.id.name);
mType = (TextView) view.findViewById(R.id.phonetype);
mNumber = (TextView) view.findViewById(R.id.phonenum);
mName.setText(name);
if (type == 1) {mType.setText("Home");}
else if (type == 2) {mType.setText("Mobile");}
else if (type == 3) {mType.setText("Work");}
else {mType.setText("Other");}
mNumber.setText(number);
}
you have already public Cursor runQueryOnBackgroundThread function in your adapter so you do not need call second time cursor in activity
you do not need to use getContacts2 function
Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_send);
Cursor contacts = null;
mAdapter= new ContactsAutoCompleteCursorAdapter(this, contacts);
mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
mTxtPhoneNo.setAdapter(mAdapter);
mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
mTxtPhoneNo.setText(number);
}
});
}
Adapter
public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {
private TextView mName, mType, mNumber;
private ContentResolver mContent;
public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater mInflater = LayoutInflater.from(context);
final View ret = mInflater.inflate(R.layout.custcontview, null);
return ret;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIdx);
int type = cursor.getInt(typeIdx);
String number = cursor.getString(numberIdx);
mName = (TextView) view.findViewById(R.id.ccontName);
mType = (TextView) view.findViewById(R.id.ccontType);
mNumber = (TextView) view.findViewById(R.id.ccontNo);
mName.setText(name);
if (type == 1) {mType.setText("Home");}
else if (type == 2) {mType.setText("Mobile");}
else if (type == 3) {mType.setText("Work");}
else {mType.setText("Other");}
mNumber.setText(number);
}
#Override
public String convertToString(Cursor cursor) {
int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameCol);
return name;
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
// this is how you query for suggestions
// notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
if (constraint==null)
return null;
if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
"UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '%" + constraint.toString().toUpperCase() + "%' or UPPER(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") LIKE '%" + constraint.toString().toUpperCase() + "%' ", null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
}
i also add query for phone number search in query

Categories

Resources