寻找适当的逻辑

时间:2013-12-26 09:23:38

标签: java android database

实际上我使用调用日志作为数据库的输入,然后我以一种在显示它时无法获得任何重复值的方式获取它,如果我在数据库中有任何重复值那么应该采取它作为整数值计数。例如:约翰(6)。 这里john必须在数据库中输入6次。别误会我的意思。我不需要代码。我需要帮助。这是代码:

  ContentResolver cr = getContentResolver();
     Cursor cur = cr.query(android.provider.CallLog.Calls.CONTENT_URI,null, null, null, null);
     if (cur.getCount() > 0) {

         while (cur.moveToNext()) {
     String rawContactId = cur.getString(cur.getColumnIndex(android.provider.CallLog.Calls._ID));
    Cursor callLogCursor = getContentResolver().query(
            android.provider.CallLog.Calls.CONTENT_URI, new String[]{

                    android.provider.CallLog.Calls.CACHED_NAME,
                    android.provider.CallLog.Calls.CACHED_NUMBER_LABEL,
                    android.provider.CallLog.Calls.NUMBER,
                    android.provider.CallLog.Calls.DATE,
                    android.provider.CallLog.Calls.DURATION,
                    android.provider.CallLog.Calls.TYPE
            },android.provider.CallLog.Calls._ID + "=?", new String[] { rawContactId }, null);;

      if (callLogCursor != null) {
        while (callLogCursor.moveToNext()) {
            //String id = callLogCursor.getString(0);
            String name = callLogCursor.getString(0);
            String cacheNumber = callLogCursor.getString(1);
            String number = callLogCursor.getString(2);
            long dateTimeMillis = callLogCursor.getLong(3);
            long durationMillis = callLogCursor.getLong(4);
            int callType = callLogCursor.getInt(5);

            String duration = getDuration(durationMillis * 1000);

            String dateString = getDateTime(dateTimeMillis);


            if (cacheNumber == null)
                cacheNumber = number;
            if (name == null)
                name = "Unknown";
            Uri image = null;

            try {
                String conId = fetchContactIdFromPhoneNumber(cacheNumber);
                long contId = Long.parseLong(conId);
                image = getPhotoUri(contId);
            }catch(Exception e) {
                Log.e("Exception", e.getMessage());
            }


                //CallLogModel callLogModel = new CallLogModel(image, name, cacheNumber,
                    //  duration, dateString);
                ContentValues values = new ContentValues();
                values.put(NAME, name);
                values.put(NUMBER, cacheNumber);
                values.put(DATE, dateString);
                values.put(DURATION,duration );
                database.insert(CALL_LOG_TABLE, null, values);

                Cursor cursor = database.query(CALL_LOG_TABLE, new String [] {LOG_ID, NAME, NUMBER, DATE, DURATION}, null, null, null, null, null);

                int row =0;
                if(!cursor.isAfterLast()) {
                    cursor.moveToFirst();

                    do{

                        int pId=cursor.getInt(0);
                        String pName = cursor.getString(1);
                        String pNumber = cursor.getString(2);
                        String pDate = cursor.getString(3);
                        String pDuration = cursor.getString(4);
                        int value = 0;

                        CallLogModel callLogModel = new CallLogModel(image, name, cacheNumber, duration, dateString);

                        if (callType == CallLog.Calls.OUTGOING_TYPE) {
                            for(int i=0;i<outgoingList.size();i++){
 ------------------------------Actually i want Logic here what should i do here--------------

                                }
                            }
                            outgoingList.add(callLogModel);

                        } else if (callType == CallLog.Calls.INCOMING_TYPE) {
                            incomingList.add(callLogModel);

                        } else if (callType == CallLog.Calls.MISSED_TYPE) {
                            missedcallList.add(callLogModel);

                        }

                        cursor.moveToNext();
                    } while (!cursor.isAfterLast());
                }





        }

        callLogCursor.close();
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在散列映射中为传出调用建模,例如:

    Map<String, Integer> outgoingCallsMap = new HashMap<String, Integer>();

    for (int i = 0; i < outgoingList.size(); i++) {
        String nameOfCallee = outgoingList.get(i);
        if (!outgoingCallsMap.containsKey(nameOfCallee)) {
            outgoingCallsMap.put(nameOfCallee, 1);
        } else {
            //Increment calls to this person
            outgoingCallsMap.put(nameOfCallee, outgoingCallsMap.get(nameOfCallee) + 1);
        }

    }

答案 1 :(得分:0)

删除outGoingList中的重复项,方法是迭代它并将结果放到地图中,联系人姓名为密钥,CallLogModel对象列表为值。 你可以参考这个方法。

private void convertToOutGoingMap(List<CallLogModel > outGoingList) {
        HashMap<String,List<CallLogModel>> outGoingMap = new HashMap<String, List<CallLogModel>>();//map which has CallLogModel.name as key and List<CallLogModel> as value.
        for(CallLogModel model : outGoingList){//Iterate thru the list.
            if(outGoingMap.containsKey(model.name))
            {
                outGoingMap.get(model.name).add(model);//if map contains key, add model to the list.
            } else {
                List<CallLogModel> modelList = new ArrayList<CallLogModel>();//if it does not contains, initialize a list and add model to it.
                modelList.add(model);
                outGoingMap.put(model.name, modelList);
            }
        }
    }

}

此地图的密钥集为您提供唯一的呼叫记录名称,相应的值列表提供所有出现次数,其大小为您提供出现次数。

希望这对你有所帮助。