How to get Distinct list of Call log with descending order of date in android using Treeset

StackOverflow https://stackoverflow.com/questions/23359619

  •  11-07-2023
  •  | 
  •  

Question

In my application, i just want call log with distinct and sorted with descending of date. I tried below code and not getting exact result

my code as follows

    public class MainActivity extends Activity {
    //  List<CLogItem> clogitems = new ArrayList<CLogItem>();
        Set clogitems = new TreeSet();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        @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;
        }

        public void onClick(View v){
            getCallDetails(this);

            TextView tv1 = (TextView)findViewById(R.id.tv1);
            String str = "";

            Iterator sitr=clogitems.iterator();
            while(sitr.hasNext())
            {
                CLogItem clitem=(CLogItem) sitr.next();
                str = str + "P: " + clitem.No + "\n";
                str = str + "N: " + clitem.Name + "\n";
                str = str + "--------------------------------------------\n";
            }
            tv1.setText(str);
        }

        private void getCallDetails(Context context) {
            StringBuffer stringBuffer = new StringBuffer();
            Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
                    null, null, null, CallLog.Calls.DATE + " DESC");
            int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
            int date = cursor.getColumnIndex(CallLog.Calls.DATE);
            int iname = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
            while (cursor.moveToNext()) {
                CLogItem clitem = new CLogItem();
                clitem.No = cursor.getString(number);
                clitem.Name = cursor.getString(iname);
                clitem.lDate = Long.valueOf(cursor.getString(date));
                clogitems.add(clitem);
            }
            cursor.close();
        }

        public static class CLogItem implements Comparable {
            String No;
            String Name;
            long lDate;

            public CLogItem(){
                this.No = "";
                this.Name = "";
                this.lDate = 0;
            }

            @Override
            public int compareTo(Object arg0) {
                CLogItem l = (CLogItem)arg0;

                 int fnResult = this.No.compareTo(l.No);
                 if( fnResult == 0 ){
                        if (lDate > l.lDate) {
                            return -1;
                        }
                        else if (lDate <  l.lDate) {
                            return 1;
                        }
                        else {
                            return 0;
                        }

                 }
                 return fnResult;
            }
        }


    }

please help me to sort out this issue

thanks in advance

Was it helpful?

Solution

Finally i got a correct solution.

    private void getCallDetails(Context context) {
        StringBuffer stringBuffer = new StringBuffer();
        Cursor cursor = context.getContentResolver().query(CallLog.Calls.CONTENT_URI,
                null, null, null, CallLog.Calls.NUMBER + "," + CallLog.Calls.DATE + " DESC");
        int number = cursor.getColumnIndex(CallLog.Calls.NUMBER);
        int date = cursor.getColumnIndex(CallLog.Calls.DATE);
        int iname = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
        String curNo = "";
        while (cursor.moveToNext()) {
            String No = cursor.getString(number);
            if( !No.equalsIgnoreCase(curNo) ){
                CLogItem clitem = new CLogItem();
                clitem.No = cursor.getString(number);
                clitem.Name = cursor.getString(iname);
                clitem.lDate = Long.valueOf(cursor.getString(date));
                clogitems.add(clitem);
                curNo = No;
            }
        }
        cursor.close();
        Collections.sort(clogitems);
    }

OTHER TIPS

Can't we achive this using GROUP BY while querying itself. Because in this case we should get all the values and then checking the possibilities. If I want to get the top 2 distinct items,still i need to get all the values.So,I think GROUP BY will help..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top