0

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();
nawaab saab
  • 1,892
  • 2
  • 20
  • 36
  • what is the order you want to see?? most recent first or most recent last?? – jaimin Aug 05 '14 at 07:50
  • You can try like [this](http://stackoverflow.com/a/8416250/1777090). Add numbers in Set. And As Set doesn't allow duplicates, you will get unique numbers – MysticMagicϡ Aug 05 '14 at 07:53
  • the order must be recent last call, secondlast, third last call and so on that i got in the call log @jaimin – nawaab saab Aug 05 '14 at 08:13

3 Answers3

1
  1. For saving numbers without duplicates, as MysticMagic suggested, use 'Set' as per the link given in the comment.

  2. 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
Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
1

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.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
0

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;

    }
nawaab saab
  • 1,892
  • 2
  • 20
  • 36